How to pass extra parameters while using lsqcurvefit?

조회 수: 20 (최근 30일)
Jay
Jay 2011년 5월 26일
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.

채택된 답변

Walter Roberson
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;

추가 답변 (1개)

Laura Proctor
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);
  댓글 수: 1
Jay
Jay 2011년 5월 26일
Laura, Thanks for the detailed explanation.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Systems of Nonlinear Equations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by