MATLAB Answers

non linear Least square fitting

9 views (last 30 days)
Xin on 19 Nov 2016
Commented: Xin on 12 Jun 2020
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.


Sign in to comment.

Accepted Answer

Star Strider
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.


Show 1 older comment
Star Strider
Star Strider on 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.
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!
Xin on 12 Jun 2020
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by