Man Scilab

lsqrsolve
Scilab Function

lsqrsolve - minimize the sum of the squares of nonlinear functions, levenberg-marquardt algorithm

Calling Sequence

[x [,v [,info]]]=lsqrsolve(x0,fct,m [,stop [,diag]])
[x [,v [,info]]]=lsqrsolve(x0,fct,m ,fjac [,stop [,diag]])

Parameters

Description

minimize the sum of the squares of m nonlinear functions in n variables by a modification of the levenberg-marquardt algorithm. the user must provide a subroutine which calculates the functions. the jacobian is then calculated by a forward-difference approximation.

minimize sum(fct(x,m).^2) where fct is function from R^n to R^m

fct should be :

a Scilab function whose calling sequence is v=fct(x,m) given x and m .
a character string which refers to a C or Fortran routine which must be linked to Scilab.

Fortran calling sequence should be fct(m,n,x,v,iflag) where m , n , iflag are integers, x a double precision vector of size n and v a double precision vector of size m .

C calling sequence should be fct(int *m, int *n, double x[],double v[],int *iflag)

fjac is an external which returns v=d(fct)/dx (x) . it should be :

a Scilab function whose calling sequence is J=fjac(x,m) given x and m .
a character string it refers to a C or Fortran routine which must be linked to Scilab.

Fortran calling sequence should be fjac(m,n,x,jac,iflag) where m , n , iflag are integers, x a double precision vector of size n and jac a double precision vector of size m*n .

C calling sequence should be fjac(int *m, int *n, double x[],double v[],int *iflag)

return -1 in iflag to stop the algoritm if the function or jacobian could not be evaluated.

Examples


// A simple example with lsqrsolve 
a=[1,7;
   2,8
   4 3];
b=[10;11;-1];
function y=f1(x,m),y=a*x+b;endfunction
[xsol,v]=lsqrsolve([100;100],f1,3)
xsol+a\b


function y=fj1(x,m),y=a;endfunction
[xsol,v]=lsqrsolve([100;100],f1,3,fj1)
xsol+a\b

// Data fitting problem
// 1 build the data
a=34;b=12;c=14;
deff('y=FF(x)','y=a*(x-b)+c*x.*x');
X=(0:.1:3)';Y=FF(X)+100*(rand()-.5);

//solve
function e=f1(abc,m)
  a=abc(1);b=abc(2),c=abc(3),
  e=Y-(a*(X-b)+c*X.*X);
endfunction
[abc,v]=lsqrsolve([10;10;10],f1,size(X,1));
abc
norm(v)

 
  

See Also

external ,   quapro ,   linpro ,   optim ,   fsolve ,  

Back