How to pass extra parameters while using lsqcurvefit?

8 visualizaciones (últimos 30 días)
Jay
Jay el 26 de Mayo de 2011
Hello,
I am currently using the lsqcurvefit function in one of my programs. I need to pass some extra parameters.
Currently i defining the extra parameters as global variables. But i would like to use the Anonymous Functions to pass these variables. I am having difficulty understanding the syntax explained in the help file.
Can someone please let me know how to code the following lines using anonymous function instead of global variables?I am looking for reformatted code.
% ----- MAIN PROGRAM
global Kg Ag Bg Cg Dg;
Kg=0;
Ag=-0.337742e-1;
Bg=0.306562e-1;
Cg=-0.356286e-2;
Dg=-0.535575;
initialConditions1 = [-2.19276,0];
[newParameters1,resnorm, residual] = lsqcurvefit(@AsphereLSQSub,initialConditions1,X,Y);
% --------SUB PROGRAM FUNCTION
function output = AsphereLSQSub(param,xData)
global Kg Ag Bg Cg Dg;
CC=param(1);
zShift=param(2);
output =((CC.*xData.^2./(1+sqrt(1-(Kg+1)*CC^2.*xData.^2)))+Ag*xData.^4+Bg*xData.^6+Cg*xData.^8+Dg*xData.^10)+zShift;
Thank you.

Respuesta aceptada

Walter Roberson
Walter Roberson el 26 de Mayo de 2011
Kg=0;
Ag=-0.337742e-1;
Bg=0.306562e-1;
Cg=-0.356286e-2;
Dg=-0.535575;
initialConditions1 = [-2.19276,0];
[newParameters1,resnorm, residual] = lsqcurvefit(@(x,data) AsphereLSQSub(x,data,Kg,Ag,Bg,Cg,Dg),initialConditions1,X,Y);
% --------SUB PROGRAM FUNCTION
function output = AsphereLSQSub(param,xData,Kg,Ag,Bg,Cg,Dg)
CC=param(1);
zShift=param(2);
output =((CC.*xData.^2./(1+sqrt(1-(Kg+1)*CC^2.*xData.^2)))+Ag*xData.^4+Bg*xData.^6+Cg*xData.^8+Dg*xData.^10)+zShift;

Más respuestas (1)

Laura Proctor
Laura Proctor el 26 de Mayo de 2011
Create your subfunction to accept the input parameters:
function output = AsphereLSQSub(param,xData,Kg,Ag,Bg,Cg,Dg);
CC=param(1);
zShift=param(2);
output =((CC.*xData.^2./(1+sqrt(1-(Kg+1)*CC^2.*xData.^2)))+Ag*xData.^4+Bg*xData.^6+Cg*xData.^8+Dg*xData.^10)+zShift;
Then define your parameters
Kg=0;
Ag=-0.337742e-1;
Bg=0.306562e-1;
Cg=-0.356286e-2;
Dg=-0.535575;
Create your anonymous function with two inputs, and call your predefined function with the pre-defined parameters
f=@(x,y)AsphereLSQSub(x,y,Kg,Ag,Bg,Cg,Dg);
Call LSQCURVEFIT with the newly created function handle
initialConditions1 = [-2.19276,0];
[newParameters1,resnorm, residual] = lsqcurvefit(f,initialConditions1,X,Y);

Categorías

Más información sobre Systems of Nonlinear Equations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by