non linear Least square fitting

3 次查看(过去 30 天)
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 中查找有关 Nonlinear Regression 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by