--- solvopt.m 2010-07-13 18:25:53.000000000 +0300 +++ solvoptWithParams.m 2010-07-15 19:28:26.000000000 +0300 @@ -1,4 +1,10 @@ -function [x,f,options]=solvopt(x,fun,grad,options,func,gradc) +function [x,f,options]=solvoptWithParams(x,funAndPars,gradAndPars,options,func,gradc) +% This version tweaked by nieminen 2008-12-15: Changed the fun and +% grad to accept parameters; changed names to funAndPars, +% gradAndPars. Changed the respective feval calls to give the params. +% +% +% gradPar arguments % Usage: % [x,f,options]=solvopt(x,fun,grad,options,func,gradc) % The function SOLVOPT performs a modified version of Shor's r-algorithm in @@ -96,10 +102,19 @@ options(9)=-1; disp(errmes); disp(error1); return end if nargin<3, app=1; % No user-supplied gradients -elseif isempty(grad), app=1; +elseif isempty(gradAndPars), app=1; else, app=0; % Exact gradients are supplied end +% nieminen 2008-12-15: +fun = funAndPars.fun; +funPar = funAndPars.pars; +if app==0 + grad = gradAndPars.fun; + gradPar = gradAndPars.pars; +end + + % OPTIONS ----{ doptions=[-1,1.e-4,1.e-6,15000,0,1.e-8,2.5,1e-11]; if nargin<4, options=doptions; @@ -206,8 +221,8 @@ % ----} End of the preamble % COMPUTE THE FUNCTION ( FIRST TIME ) ----{ - if trx, f=feval(fun,x'); - else, f=feval(fun,x); end + if trx, f=feval(fun,x',funPar); + else, f=feval(fun,x ,funPar); end options(10)=options(10)+1; if isempty(f), if dispwarn,disp(errmes);disp(error30);end options(9)=-3; if trx, x=x';end, return @@ -246,8 +261,8 @@ else, if trx, g=apprgrdn(x',f,fun,deltax',1); else, g=apprgrdn(x ,f,fun,deltax,1); end end, options(10)=options(10)+n; - else, if trx, g=feval(grad,x'); - else, g=feval(grad,x); end + else, if trx, g=feval(grad,x', gradPar); + else, g=feval(grad,x , gradPar); end options(11)=options(11)+1; end if size(g,2)==1, g=g'; end, ng=norm(g); @@ -363,8 +378,8 @@ if constr, FP1=FP; fp1=fp; end x=x+hp*g0; % FUNCTION VALUE - if trx, f=feval(fun,x'); - else, f=feval(fun,x ); end + if trx, f=feval(fun,x', funPar); + else, f=feval(fun,x , funPar); end options(10)=options(10)+1; if h1*f==Inf if dispwarn, disp(errmes); disp(error5); end @@ -453,8 +468,8 @@ else, g=apprgrdn(x ,f,fun,deltax ,1); end end, options(10)=options(10)+n; else - if trx, g=feval(grad,x'); - else, g=feval(grad,x ); end + if trx, g=feval(grad,x', gradPar); + else, g=feval(grad,x , gradPar); end options(11)=options(11)+1; end if size(g,2)==1, g=g'; end, ng=norm(g); @@ -593,8 +608,8 @@ g0=-h*g0/2; for i=1:10, x=x+g0; - if trx, f=feval(fun,x'); - else, f=feval(fun,x ); end + if trx, f=feval(fun,x',funPar); + else, f=feval(fun,x ,funPar); end options(10)=options(10)+1; if abs(f)==Inf if dispwarn, disp(errmes); disp(error32); end @@ -610,8 +625,8 @@ else, g=apprgrdn(x ,f,fun,deltax ,1); end options(10)=options(10)+n; else - if trx, g=feval(grad,x'); - else, g=feval(grad,x ); end + if trx, g=feval(grad,x',gradPar); + else, g=feval(grad,x ,gradPar); end options(11)=options(11)+1; end if size(g,2)==1, g=g'; end, ng=norm(g); @@ -641,8 +656,8 @@ for j=idx, y=x(j); f2=fm; if y==0, x1(j)=1; elseif abs(y)<1, x1(j)=sign(y); else, x1(j)=y; end for i=1:20, x1(j)=x1(j)/1.15; - if trx, f1=feval(fun,x1'); - else, f1=feval(fun,x1 ); end + if trx, f1=feval(fun,x1',funPar); + else, f1=feval(fun,x1 ,funPar); end options(10)=options(10)+1; if abs(f1)~=Inf & ~isnan(f1), if h1*f1>h1*fm, y=x1(j); fm=f1; @@ -660,8 +675,8 @@ else, gt=apprgrdn(x1 ,fm,fun,deltax ,1); end options(10)=options(10)+n; else - if trx, gt=feval(grad,x1'); - else, gt=feval(grad,x1 ); end + if trx, gt=feval(grad,x1',gradPar); + else, gt=feval(grad,x1 ,gradPar); end options(11)=options(11)+1; end if size(gt,2)==1, gt=gt'; end, ngt=norm(gt);