non linear Least square fitting
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Star Strider
on 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.
4 Comments
NN
on 12 Jun 2020
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!
More Answers (0)
See Also
Categories
Find more on Linear and Nonlinear Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!