[Scilab-users] Least squares problem call to fmincon - HELP

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

[Scilab-users] Least squares problem call to fmincon - HELP

How may i (if possible?) reconfigure the least-squares problem to be solved
with a minimalist call to fmincon (from the fossee toolbox)?

I tried everything, but must be (seriously?) overlooking something! Perhaps
something like:

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

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

DAT=[0 0;0 1;1 1;2 1.5;2 2];
p0=[0 0 0];
A=[]; b=[];

[popt,fopt]=fmincon(costf,p0,A,b);
disp(popt,'popt',fopt,'fopt');
fmincon.sce <http://mailinglists.scilab.org/file/t490392/fmincon.sce>  
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: Least squares problem call to fmincon - HELP


Le 09/01/2020 à 22:32, David Brant a écrit :
How may i (if possible?) reconfigure the least-squares problem to be solved
with a minimalist call to fmincon (from the fossee toolbox)?

I tried everything, but must be (seriously?) overlooking something! Perhaps
something like:

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

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

DAT=[0 0;0 1;1 1;2 1.5;2 2];
p0=[0 0 0];
A=[]; b=[];

[popt,fopt]=fmincon(costf,p0,A,b);
disp(popt,'popt',fopt,'fopt');
fmincon.sce <https://antispam.utc.fr/proxy/1/c3RlcGhhbmUubW90dGVsZXRAdXRjLmZy/mailinglists.scilab.org/file/t490392/fmincon.sce>  
Dave

By default, fmincon expects costf to return only the objective function. Hence you should define it like this

function f=costf(p)
    f = norm(fun(p))^2;
endfunction

There is a way to provide the exact gradient of f, as described here: https://scilab.in/fossee-scilab-toolbox/optimization-toolbox/functions/fmincon

function g = grad(p)
   g = 2*dfun(p)'*fun(p);
endfunction
options = list("GradObj",grad)
xopt = fmincon(f,x0,A,b,[],[],[],[],[],options)

S.




--
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: Least squares problem call to fmincon - HELP

Many thanks for getting back Stéphane. I tried your suggestion with the
gradient option, but got a problem:

 !--error 26
Too complex recursion! (recursion tables are full)
at line       2 of function fun called by :  
at line       2 of function fGrad called by :  
sample_fGrad=fGrad(x0)
at line     649 of exec file called by :    
init=fun(x0)

The minimalist call also returned a problem:

y=numderivative(fun,x)
                       !--error 4
Undefined variable: 10000000000000G*1000100
at line      29 of function flag1 called by :  
at line      16 of function fGrad1 called by :  
init=fun(x0)
at line     842 of exec file called by :    
API Error:
in error: bad call to getVarAddressFromPosition! (1rst argument).

Probably a stupid error on my part, but cannot spot it!

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=costf(p)
        f = norm(fun(p))^2;
endfunction

//function g=grad(p)
// g = 2*dfun(p)'*fun(p);
//endfunction

DAT=[0 0;0 1;1 1;2 1.5;2 2];
p0=[0 0 0];

xopt = fmincon(costf,p0,[],[])

//options = list("GradObj",grad)
//xopt = fmincon(costf,p0,[],[],[],[],[],[],[],options)

fmincon.sce <http://mailinglists.scilab.org/file/t490392/fmincon.sce>  



--
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: Least squares problem call to fmincon - HELP

Did you try one of the examples provided in the help page of fmincon (as
a sanity test of FOT) ?

I don't see any stupid error in your code.

S.

Le 10/01/2020 à 15:25, David Brant a écrit :

> Many thanks for getting back Stéphane. I tried your suggestion with the
> gradient option, but got a problem:
>
>   !--error 26
> Too complex recursion! (recursion tables are full)
> at line       2 of function fun called by :
> at line       2 of function fGrad called by :
> sample_fGrad=fGrad(x0)
> at line     649 of exec file called by :
> init=fun(x0)
>
> The minimalist call also returned a problem:
>
> y=numderivative(fun,x)
>                         !--error 4
> Undefined variable: 10000000000000G*1000100
> at line      29 of function flag1 called by :
> at line      16 of function fGrad1 called by :
> init=fun(x0)
> at line     842 of exec file called by :
> API Error:
> in error: bad call to getVarAddressFromPosition! (1rst argument).
>
> Probably a stupid error on my part, but cannot spot it!
>
> 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=costf(p)
> f = norm(fun(p))^2;
> endfunction
>
> //function g=grad(p)
> // g = 2*dfun(p)'*fun(p);
> //endfunction
>
> DAT=[0 0;0 1;1 1;2 1.5;2 2];
> p0=[0 0 0];
>
> xopt = fmincon(costf,p0,[],[])
>
> //options = list("GradObj",grad)
> //xopt = fmincon(costf,p0,[],[],[],[],[],[],[],options)
>
> fmincon.sce <https://antispam.utc.fr/proxy/1/c3RlcGhhbmUubW90dGVsZXRAdXRjLmZy/mailinglists.scilab.org/file/t490392/fmincon.sce>
>
>
>
> --
> 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: Least squares problem call to fmincon - HELP

Yes, i have run all 7 examples in 'documentation' and the spring & circular
tank design problems in the 'examples' without issue.



--
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