Exponential curve fitting with nonlinearleastsquares methood
16 views (last 30 days)
I want to Fit my points with nonlinearleastsquare method,
I used this code before and my result was as shown bellow.
but now I want to fit another points but Matlab gives me a figure bellow.
I want to fit like first figure , I dont want to fit linear. How can I fit exponential like fist figure?
Can anyone help me?
% Set up fittype and options.
ft = fittype( ' a*exp(b*x)', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.Lower = [-inf -inf];
opts.StartPoint = [0.0838330167826827 0.0962959306873294];
opts.Upper = [inf,inf];
% Fit model to data.
[fitresult, gof] = fit( Xk(1:16), Yk(1:16), ft, opts );
% Plot fit with data.
figure( 'Name', 'T2 Mapping Fit' );
h = plot( fitresult, Xk, Yk ,'*');
legend( h, 'Intensity vs. TE', 'T2 Mapping Fit', 'Location', 'NorthWest' );
% Label axes
title(['T2 Value = ' num2str(-1/fitresult.b) ' R Value = ' num2str(-fitresult.b)])
% xlim([0 120])
John D'Errico on 15 Aug 2022
You DID get an exponential fit. However, your data is relatively noisy. And it does not span a wide enough interval so that the exponential nature of the fit is apparent. So what you see is something that LOOKS virtually linear. Over a small interval, ANY smooth function looks linear. For example, plot the function sin(x), over a sufficiently small interval, and what will you see?
Yes, we know that sin(x) IS a nonlinear function. But over that interval, it does look pretty well linear, even though over a larger interval we would see the difference.
If you want to see something that looks more like an exponential function, then you need better data. And you may need data over a wider interval. Or you need to plot the resulting fit function over a wider interval.