[Scilab-users] Help with Jacobian call to Optim

classic Classic list List threaded Threaded
5 messages Options
David Brant David Brant
Reply | Threaded
Open this post in threaded view
|

[Scilab-users] Help with Jacobian call to Optim

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: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Christophe Dang Ngoc Chan Christophe Dang Ngoc Chan
Reply | Threaded
Open this post in threaded view
|

Re: {EXT} Help with Jacobian call to Optim

Hello,

> De : David Brant
> Envoyé : lundi 6 janvier 2020 19:15
>
> Rsponse:
> !--error 98
> Variable returned by scilab argument function is incorrect.

I personally have the following error:

costf: Wrong type for output argument #1: Real scalar expected.

If I understand well, fun() returns a 5 × 1 matrix which is probably the problem.

Regards

--
Christophe Dang Ngoc Chan
Mechanical calculation engineer

General
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error), please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
--
Christophe Dang Ngoc Chan
Mechanical calculation engineer
mottelet mottelet
Reply | Threaded
Open this post in threaded view
|

Re: Help with Jacobian call to Optim

In reply to this post by David Brant
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
> [hidden email]
> 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

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
David Brant David Brant
Reply | Threaded
Open this post in threaded view
|

Re: Help with Jacobian call to Optim

Many thanks Stéphane.

Is it also possible to do this if the gradient is not known or impractical
to obtain using NDcost as i had intended with something like

[fopt,popt,gopt]=optim(list(NDcost,fun),p0') ?

Regards, Dave



--
Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
mottelet mottelet
Reply | Threaded
Open this post in threaded view
|

Re: Help with Jacobian call to Optim

In reply to this post by mottelet

There is no added value when using NDcost, you can directly use numderivative like this

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

S.

Le 08/01/2020 à 16:45, David Brant a écrit :
Many thanks Stéphane.

Is it also possible to do this if the gradient is not known or impractical
to obtain using NDcost as i had intended with something like

[fopt,popt,gopt]=optim(list(NDcost,fun),p0') ?

Regards, Dave



--
Sent from: https://antispam.utc.fr/proxy/1/c3RlcGhhbmUubW90dGVsZXRAdXRjLmZy/mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html
_______________________________________________
users mailing list
[hidden email]
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

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users