Exponential curve fitting with nonlinearleastsquares methood
2 views (last 30 days)
Show older comments
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])
0 Comments
Answers (2)
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 )
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 )
% 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)])
0 Comments
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 )
% 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.
.
0 Comments
See Also
Categories
Find more on Linear and Nonlinear Regression 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!