[Scilab-users] Help with Jacobian call to Optim

Stéphane Mottelet stephane.mottelet at utc.fr
Wed Jan 8 13:41:05 CET 2020


Hello,

If you want to use optim for your least squares problem you have to 
consider the minimization of norm(fun(p))^2, which gradient is 
2*dfun(p)'*fun(p), i.e. costf must be written like this:

function [f,g,ind]=costf(p,ind)
     f=norm(fun(p))^2;
     g=2*dfun(p)'*fun(p);
endfunction

After this modification you should obtain:

--> [fopt,popt,gopt]=optim(costf,p0)
  gopt  =

   -1.563D-14  -2.598D-14  -8.216D-15

  popt  =

    1.   0.4054651  -0.5

  fopt  =

    0.3125

S.

Le 06/01/2020 à 19:14, David Brant a écrit :
> Hi, i am having problems with the below code.
>
> It is a variation of an example listed in the optimization chapter of the
> Modelling and Simuation in Scilab-Scicos book (pages 109-110 &114). I can
> configure the code to work for leastsq and lsqrsolve, but not optim. Any
> advice on mods would be very much appreciated. Regards, Dave
>
> Code:
> function z=fun(p)
> 	z=DAT(:,2)-p(1)*exp(p(2)*DAT(:,1))-p(3)*ones(DAT(:,1))
> endfunction
> function dz=dfun(p)
> 	var=exp(p(2)*DAT(:,1))
> 	dz=[-var..
> 	-p(1)*DAT(:,1).*var..
> 	-ones(var)]
> endfunction
> function [f,g,ind]=costf(p,ind)
> 	f=fun(p); g=dfun(p)
> endfunction
> DAT=[0 0;0 1;1 1;2 1.5;2 2]
> p0=[0 0 0];
> [fopt,popt]=optim(costf,p0)
> [fopt,popt,gopt]=optim(list(NDcost,fun),p0')
>
> Rsponse:
> !--error 98
> Variable returned by scilab argument function is incorrect.
>
>
>
> --
> Sent from: https://antispam.utc.fr/proxy/1/c3RlcGhhbmUubW90dGVsZXRAdXRjLmZy/mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html
> _______________________________________________
> users mailing list
> users at lists.scilab.org
> https://antispam.utc.fr/proxy/1/c3RlcGhhbmUubW90dGVsZXRAdXRjLmZy/lists.scilab.org/mailman/listinfo/users

-- 
Stéphane Mottelet
Ingénieur de recherche
EA 4297 Transformations Intégrées de la Matière Renouvelable
Département Génie des Procédés Industriels
Sorbonne Universités - Université de Technologie de Compiègne
CS 60319, 60203 Compiègne cedex
Tel : +33(0)344234688
http://www.utc.fr/~mottelet




More information about the users mailing list