Exponential curve fitting with nonlinearleastsquares methood

61 views (last 30 days)
I want to fit exponential curve on my data with nonlinearleastsquares function but Matlab gives me a straight line! Can anyone help me?
ft = fittype( 'A * (1 - B * (exp(-x/T1)))', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.Lower = [-Inf 0 0];
opts.StartPoint = [10 10 10];
opts.Upper = [Inf 1 8000];
% Fit model to data.
[fitresult, gof] = fit( Xk(1:5), Yk(1:5), ft, opts );
% Plot fit with data.
figure( 'Name', 'T1 Mapping Fit' );
h = plot( fitresult, Xk, Yk ,'*');
legend( h, 'Intensity vs. TR', 'T1 Mapping Fit', 'Location', 'NorthWest' );
% Label axes
xlabel TR
ylabel Intensity
grid on
title(['T1 Value = ' (fitresult.T1) ' R Value = ' num2str(1/fitresult.T1)])
% xlim([0 120])

Answers (2)

Matt J
Matt J on 31 Jul 2022
Edited: Matt J on 31 Jul 2022
Use fminspleas from the File Exchange, which only requires an initial guess of T1.
Xk = [800; 1000; 1490; 2000; 2510];
Yk = [590; 720; 970; 1190; 1310];
[T1,ab]=fminspleas({1,@(T,x)-exp(-x/T)} ,1000,Xk,Yk,0,8000 )
T1 = 1.5439e+03
ab = 2×1
1.0e+03 * 1.6755 1.8257
A=ab(1);
B=ab(2)/A;
fun=@(x)A * (1 - B * (exp(-x/T1)));
x=linspace(Xk(1),Xk(end));
plot(Xk,Yk,'o',x,fun(x))
Then, if you really must have the results as a native cfit object, just use the results as a StartPoint for fit();
ft = fittype( 'A * (1 - B * (exp(-x/T1)))', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.Lower = [-Inf 0 0];
opts.StartPoint = [A,B,T1];
opts.Upper = [Inf 1 8000];
% Fit model to data.
[fitresult, gof] = fit( Xk(1:5), Yk(1:5), ft, opts )
fitresult =
General model: fitresult(x) = A * (1 - B * (exp(-x/T1))) Coefficients (with 95% confidence bounds): A = 1895 (1603, 2187) B = 1 (fixed at bound) T1 = 2088 (1577, 2600)
gof = struct with fields:
sse: 913.7171 rsquare: 0.9975 dfe: 3 adjrsquare: 0.9967 rmse: 17.4520
% Plot fit with data.
figure( 'Name', 'T1 Mapping Fit' );
h = plot( fitresult, Xk, Yk ,'*');
legend( h, 'Intensity vs. TR', 'T1 Mapping Fit', 'Location', 'NorthWest' );
% Label axes
xlabel TR
ylabel Intensity
grid on
title(['T1 Value = ' (fitresult.T1) ' R Value = ' num2str(1/fitresult.T1)])

Star Strider
Star Strider on 31 Jul 2022
Use a different vector for ‘StartPoint’ and then experiment —
Xk = [800; 1000; 1490; 2000; 2510];
Yk = [590; 720; 970; 1190; 1310];
ft = fittype( 'A * (1 - B * (exp(-x/T1)))', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.Lower = [-Inf 0 0];
% opts.StartPoint = [10 10 10];
opts.StartPoint = [100 0.5 5000];
opts.Upper = [Inf 1 8000];
% Fit model to data.
[fitresult, gof] = fit( Xk(1:5), Yk(1:5), ft, opts )
fitresult =
General model: fitresult(x) = A * (1 - B * (exp(-x/T1))) Coefficients (with 95% confidence bounds): A = 1895 (1603, 2187) B = 1 (fixed at bound) T1 = 2088 (1577, 2600)
gof = struct with fields:
sse: 913.7171 rsquare: 0.9975 dfe: 3 adjrsquare: 0.9967 rmse: 17.4520
% Plot fit with data.
figure( 'Name', 'T1 Mapping Fit' );
h = plot( fitresult, Xk, Yk ,'*');
legend( h, 'Intensity vs. TR', 'T1 Mapping Fit', 'Location', 'NorthWest' );
% Label axes
xlabel TR
ylabel Intensity
grid on
title(['T1 Value = ' (fitresult.T1) ' R Value = ' num2str(1/fitresult.T1)])
% xlim([0 120])
NOTE — This ‘StartPoint’ vector also conforms to the parameter bound options.
.

Community Treasure Hunt

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

Start Hunting!