MATLAB Answers

non linear Least square fitting

조회 수: 6(최근 30일)
Xin 19 Nov 2016
Commented: Star Strider 19 Nov 2016
I have an equation:
z = A*(y^B)*exp(C*x)
where x,y,z are my data points, and A B C are the unknowns that I want to fit.
Is there a way to fit these unknowns with least square function.

  댓글 수: 0

로그인 to comment.

채택된 답변

Star Strider
Star Strider 19 Nov 2016
The ‘trick’ here is to create a matrix of your ‘x’ and ‘y’ data vectors and give them to your objective function as a single argument. The objective function can then refer to the appropriate columns of that matrix to use ‘x’ and ‘y’ correctly in your equation. I created random ‘x’, ‘y’, and ‘z’ vectors to test my code, so substitute your data for them. My code would otherwise not change. The estimated parameters are the ‘B’ output of the fminsearch call, the second ‘resnorm’ output being the norm of the residuals. (The ‘resnorm’ output is not necessary, but is helpful in assessing how good the fit is.)
The Code
% % % z = A*(y^B)*exp(C*x) % Original Equation
% % % MAPPING: b(1) = A, b(2) = B, b(3) = C
z_fit = @(b, xy) b(1) .* xy(:,2).^b(2) .* exp(b(3) .* xy(:,1));
x = randi(99, 1, 10); % Create Data
y = randi(99, 1, 10); % Create Data
z = randi(99, 1, 10); % Create Data
xy = [x(:), y(:)]; % Create Column Vectors & Concatenate In One Matrix To Form Independent Variable Argument
NCF = @(b) norm(z - z_fit(b,xy)); % Norm Cost Function
B0 = [1; 2; 3]; % Initial Parameter Estimates
[B,resnorm] = fminsearch(NCF, B0); % Estimate Parameters
You can plot the result using scatter3 or stem3 for your data, and plot3 for the objective function fit. Use the hold function to plot them on the same axes, and grid on to show the grid lines.

  댓글 수: 2

Xin 19 Nov 2016
Thank you so much for your amazing answer. I tried it, and it works. Just it is really sensitive to my initial guess. Is it because of the data or the non-linear fitting approach? Also do you know any way to get the error of the fitted A B C value, e.g. 68% or 95% confidence level. Thanks a lot.
Star Strider
Star Strider 19 Nov 2016
My pleasure.
Sensitivity to the initial parameter estimates is characteristic of nonlinear fitting, even with the derivative-free approach used by fminsearch. The easiest way to get the parameter confidence intervals is to use the Statistics (and Machine Learning) Toolbox nlinfit function, and then the nlparci function. See the documentation for them to see what you need from nlinfit to use nlparci.
Use my ‘z_fit’ function with nlinfit (it will work without modification), and the parameters estimated by my code here as the initial parameter estimates for nlinfit.
As always, my pleasure.

로그인 to comment.

More Answers (0)

이 질문에 답변하려면 로그인을(를) 수행하십시오.

Translated by