# Exponential curve fitting with nonlinearleastsquares methood

15 views (last 30 days)
mahak on 15 Aug 2022
Answered: John D'Errico on 15 Aug 2022
Hello :)
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
xlabel TE
ylabel Intensity
grid on
title(['T2 Value = ' num2str(-1/fitresult.b) ' R Value = ' num2str(-fitresult.b)])
% xlim([0 120])
Chunru on 15 Aug 2022
In your second figure, you also fit the data into exponential function. Howerver, the data is very much linear. You can observe that the parameter b in a*exp(b*x) for the second case is very small such that it approximates to a linear function for your data range.

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?
fplot(@sin,[.7,.75]) 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.
fplot(@sin,[0 2*pi])
vline([0.7 0.75],'r') 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.