non linear Least square fitting

조회 수: 2 (최근 30일)
Xin
Xin 2016년 11월 19일
댓글: Xin 2020년 6월 12일
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.
Thanks

채택된 답변

Star Strider
Star Strider 2016년 11월 19일
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.
  댓글 수: 4
NN
NN 2020년 6월 12일
Hi, I have the following data set for the same equation as Xin:
x=[1020 2550 5100];
y=[358.15 365.15 373.15];
z=[5105 1790 104; 3425 962 328; 4689 482 103];
When I run the code it gives me the following error:
Exiting: Maximum number of function evaluations has been exceeded
- increase MaxFunEvals option.
Current function value: Inf
Can you help me with a way around this? Thank you!
Xin
Xin 2020년 6월 12일
You need to also provide the function you are trying to fit but based on what I see, you have a problem with the maximum value exceeded, which you can modify using the following line:
options = optimoptions('fmincon','Display','iter','Algorithm','sqp');
options.MaxFunEvals = 1e5;
then you can use options into fmincon or fminsearch.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by