How to pass extra parameters while using lsqcurvefit?
21 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Walter Roberson
2011-5-26
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;
0 个评论
更多回答(1 个)
Laura Proctor
2011-5-26
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);
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!