Fit a equation to a scatter plot in log log scale with the given parameters

1 view (last 30 days)
I want to fit a curve (equation is known) to a scatter plot (attached image). But, I don't see any curve overlapping with the scatter plot after running the code. It is so easy to do in excel but in MATLAB I am not able to replicate the same. Here is the code with the equation and the parameters:
A=readmatrix('Data_1.xlsx');
x=A(:,1);
y=A(:,2);
z=A(:,3);
q=1.12;
m=400;
z=(1-(1-q)*x*m).^1./(1-q);
sz=15;
scatter(x,y,sz,'MarkerEdgeColor','k','MarkerFaceColor',[1 0 0],'LineWidth',1)
hold on
plot(x,z)
You can also change the values of 'q' and 'm' to obtain the desired curve fitting the scatter plot.
Data and the image is attached.

Answers (1)

dpb
dpb on 10 Apr 2023
Moved: dpb on 10 Apr 2023
fn='https://www.mathworks.com/matlabcentral/answers/uploaded_files/1350879/Data_1.xlsx';
A=readmatrix(fn);
x=A(:,1);
y=A(:,2);
%z=A(:,3);
q=1.12;
m=400;
z=(1-(1-q)*x*m).^1./(1-q);
whos z
Name Size Bytes Class Attributes z 25919x1 207352 double
%sz=15;
%scatter(x,y,sz,'MarkerEdgeColor','k','MarkerFaceColor',[1 0 0],'LineWidth',1)
%hold on
plot(x,z)
hold on
plot(x,A(:,3))
legend('functional','raw data')
You redefine z and your values computed with the specific parameters are far removed from the actual data valus -- and are in fact, negative. Those won't be able to be plotted on a log axis; you undoubtedly got a warning message about that problem.
If have Curve Fitting TB, you could define the model and fit it to estimate the parameters although you may need better starting estimates to at least make the results positive. I've got another engagement at the moment so can't try it jut now...
  1 Comment
dpb
dpb on 10 Apr 2023
fn='https://www.mathworks.com/matlabcentral/answers/uploaded_files/1350879/Data_1.xlsx';
A=readmatrix(fn);
x=A(:,1);
y=A(:,2);
q=1.12;
m=400;
ft=fittype('(1-(1-q)*x*m).^1/(1-q)');
[f,~,o]=fit(x,y,ft)
Warning: Start point not provided, choosing random start point.
f =
General model: f(x) = (1-(1-q)*x*m).^1/(1-q) Coefficients (with 95% confidence bounds): m = 72.43 (71.83, 73.03) q = -0.4885 (-0.4939, -0.4831)
o = struct with fields:
numobs: 25919 numparam: 2 residuals: [25919×1 double] Jacobian: [25919×2 double] exitflag: 3 firstorderopt: 2.0669e-04 iterations: 6 funcCount: 21 cgiterations: 0 algorithm: 'trust-region-reflective' stepsize: 2.4108e-04 message: 'Success, but fitting stopped because change in residuals less than tolerance (TolFun).' bestfeasible: [] constrviolation: []
plot(f)
hold on
scatter(x,y)
Your model doesn't look to be able to put any curvature to speak of into the result -- not knowing the origins of the data nor the reasons for the particular model form (and other time pressures), I didn't do any further investigation as to what might be a suitable model.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!