function [x,xk,Rk]=TrustRegionLinear(f,fp,x0,tol,maxiter,R0,rs,rv,gi,gd)
% TRUSTREGIONLINEAR trust region method with linear model
%   [x,xk,Rk]=TrustRegionLinear(f,fp,x0,tol,maxiter,r0,rs,rv,gi,gd)
%   finds an approximate minimum of the function f with gradient fp,
%   starting at the initial guess x0. The remaining parameters are
%   optional and default values are used if they are omited. xk
%   contains all the iterates of the method and rk the trust region
%   radii of all iterations.

if nargin<10, gd=0.5; end;
if nargin<9, gi=2; end;
if nargin<8, rv=0.9; end;
if nargin<7, rs=0.1; end;
if nargin<6, R0=1; end;
if nargin<5, maxiter=100; end;
if nargin<4, tol=1e-6; end;

x=x0;
xk=x0;
R=R0;
Rk=R0;
p=-feval(fp,x);
k=0;
while norm(p)>tol & k<maxiter
  s=R*p/norm(p);
  r=(feval(f,x)-feval(f,x+s))/(s'*p);   
  if r>=rs
    x=x+s;
    p=-feval(fp,x);
    if r>-rv
      R=gi*R;
    end
  else
    R=gd*R
  end
  k=k+1;
  xk(:,k+1)=x;
  Rk(:,k+1)=R;
end;
