Man Scilab

leastsq
Scilab Function

leastsq - Solves non-linear least squaresproblems

Calling Sequence

[f,xopt]=leastsq([imp,] fun [,Dfun],x0)
[f,[xopt,[gradopt]]]=leastsq(fun [,Dfun],[contr],x0,['algo'],[df0,[mem]],
,[stop],['in'])

Parameters

Description

Non-linear optimization routine for programs without constraints or with bound constraints:

min sum(f(x)).^2 w.r.t x , Here, f is a function from R^n to R^m which returns f(x) , a real vector ( value of function at x ).

fun gives the definition of the function f(x) . It is an "external" i.e function, or list or C or Fortran routine (see "external").

This external must return a vector y such as ( y(j)=f(x) ) for a given x .

If fun is a Scilab function, the calling sequence for fun must be: y=fun(x, [optional parameters])
If fun is defined by a Fortran or C routine first argument must be a list: list(fun_name,m,p1,..pl)

fun_name is then a character string, it refers to the name of the routine which must be linked to Scilab. Here, the generic calling sequences are:

In Fortran


          subroutine fun(n,m,x,td,y)
          integer n,m
          double precision x(n), td(*), y(m)
         
            

In C


          void fun(int *n,int *m, double *x,double *params, double *y)
       
            

n is the dimension of vector x , m is the dimension of vector y , td is a vector which contains the parameters p1,..pl

Dfun Gives the definition of the function df(x)/dx . It is an "external".This external must return a matrix g such as ( g(i,j)=dfi/dxj ) for a given x .
If Dfun is a function, the calling sequence for fun must be: g=Dfun(x, [optional parameters]) .
If Dfun is defined by a Fortran or C routine first argument must be a list: list(fun_name,m,...)

fun_name is a character string, it refers to the name of the routine which must be linked to Scilab. This function has the same calling sequence as fun

Examples


a=rand(3,2);b=[1;1;1];x0=[1;-1];
deff('f=fun(x,a,b)','f=a*x-b');
deff('g=dfun(x,a,b)','g=a');

[f,xopt]=leastsq(fun,x0)      //Simplest call
xopt-a\b  //compare with linear algebra solution

[f,xopt]=leastsq(fun,dfun,x0)      //specify gradient

[f,xopt]=leastsq(list(fun,[1 2;3 4],[1;2]),x0)    

deff('f=fun(x,a,b)','f=exp(a*x)-b');
deff('g=dfun(x,a,b)','g=a.*(exp(a*x)*ones(1,size(a,2)))');

[f,xopt]=leastsq(list(fun,[1 2;3 4],[1;2]),x0)  
   
  

See Also

external ,   quapro ,   linpro ,  

Back