MATLAB: Problem with nonlinear curve fitting (lsqcurvefit and Multistart)

lsqcurvefitmultistartnonlinear curvefitting

Hello all,
Actually, I am dealing with an issue that I can't get desirable results out of my curve-fitting problem. I used lsqcurvefit in the first place. Then, I added MultiStart to my code as well but my result didn't change. I don't know why MATLAB gives the unknown model coefficient the same value as I entered for MATLAB. I bring my code in the following along with the results. I would really appreciate it if you can help me fix this problem and get better results:
clc
clear all;
close all;
%xdata
T = 294:0.1:361;
T=T';
%ydata
for i=1:size(T,1)
f1(i)= 0.2099*exp(-((T(i)-340.2)/2.439).^2);
end
f1=f1';
%Initial value for unknown coefficient
B0 =[-6.1e+04];
%find the local fit using lsqcurvefit
options = optimoptions('lsqcurvefit','FiniteDifferenceType','central','OptimalityTolerance',1e-12,'FunctionTolerance',1e-12, 'MaxFunctionEvaluations',1500);
lb = [-Inf];
ub = [Inf];
[B,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat] = lsqcurvefit(@firstorderDSC1,B0,T,f1,lb,ub,options);
% Set up the problem for MultiStart
problem = createOptimProblem('lsqcurvefit','x0',B0,'objective',@firstorderDSC1,...
'lb',lb,'ub',ub,'xdata',T,'ydata',f1);
ms = MultiStart('PlotFcns',@gsplotbestf);
[xmulti,errormulti] = run(ms,problem,50);
%plot the result
figure;
plot(T,f1,T,firstorderDSC1(xmulti,T),'LineWidth',3)
legend('Data','Fitted result')

Best Answer

I am not sure why you wrote the function firstorderDSC1 like that, but reviewing it briefly, it appears that the ode function you have written inside it to fit the data is wrong. Check the differences of your code with this code
clc
clear all;
close all;
%xdata
T = 294:0.1:361;
T=T';
%ydata
for i=1:size(T,1)
f1(i)= 0.2099*exp(-((T(i)-340.2)/2.439).^2);
end
f1=f1';
%Initial value for unknown coefficient
B0 =rand(1,3);
%find the local fit using lsqcurvefit
options = optimoptions('lsqcurvefit','FiniteDifferenceType','central','OptimalityTolerance',1e-12,'FunctionTolerance',1e-12, 'MaxFunctionEvaluations',1500);
lb = [-Inf];
ub = [Inf];
[B,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat] = lsqcurvefit(@firstorderDSC1,B0,T,f1,lb,ub,options);
% Set up the problem for MultiStart
problem = createOptimProblem('lsqcurvefit','x0',B0,'objective',@firstorderDSC1,...
'lb',lb,'ub',ub,'xdata',T,'ydata',f1);
ms = MultiStart('PlotFcns',@gsplotbestf);
[xmulti,errormulti] = run(ms,problem,50);
%plot the result
figure;
plot(T,f1,T,firstorderDSC1(xmulti,T),'LineWidth',3)
legend('Data','Fitted result')
function dndt2 = firstorderDSC1(B, T)
%my differential equation
function denaturate = firstorderDSC(T, n)
a= (exp(183.8)./1.5); %(1/s)
dndT=B(1)*exp(-((T-B(2))/B(3)).^2);
denaturate=dndT;
end
%solving my differential equation
options = odeset('AbsTol', 1e-8,'RelTol',1e-8);%,'OutputFcn',@odeplot);
[temp,num] = ode23s(@firstorderDSC, T, 0, options);
dndt2 = firstorderDSC(temp,num);
end
Both lines overlap.