How to calculate and plot trendline (fit curve) in matlab?
3 views (last 30 days)
Show older comments
I have one question. I have a X-Y plot. I would like to set the fit curve of this two data (X, Y). But I would like this curve/trendline to be logaritmic. But I did not find it in this link. https://www.mathworks.com/help/curvefit/fit.html
Could you help me please?
0 Comments
Answers (1)
Mathieu NOE
on 19 Mar 2021
hello
you can do a curve fit on your data converted to log scale ; here a demo below :
x = [7.94, 16.23, 32.92, 66.8, 135.52, 274.93, 557.78, 1131.59, 2295.72, 4657.46];
y = [134000, 102000, 31000, 11000, 2600, 990, 40, 10.41, 3.48, 1.037];
Bp = polyfit(log10(x), log10(y), 1);
Yp_log = polyval(Bp,log10(x));
figure(1),loglog(x, y, 'pg')
hold on
loglog(x, 10.^Yp_log, '-r')
hold off
grid on
expstr = @(x) [x(:).*10.^ceil(-log10(abs(x(:)))) floor(log10(abs(x(:))))]; % Mantissa & Exponent
Bp2 = expstr(10^Bp(2));
eqn = sprintf('y = %.2f\\times10^{%d}\\cdotx^{%.2f}',Bp2(1), Bp2(2), Bp(1));
legend('Data', eqn)
text(10, 10, strcat('Slope in Log-Log Space =', num2str(Bp(1))), 'Interpreter', 'none');
0 Comments
See Also
Categories
Find more on Fit Postprocessing 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!