13 views (last 30 days)

Hello,

Actually, I am trying to fit a differential equation () to some data using lsqcurvefit. I enetered my initial value in a way that completely fit to the data and I know that it should easily fit because I tried it with cftool. But my code cannot fit the result of the differential equation to my data and gives the following message in command window:

Optimization completed because the size of the gradient at the initial point is less than the value of the optimality tolerance.

Could you please help me to make my differential equation fit to the experimental data?

clc

clear all;

close all;

[d,s,r] = xlsread('Temp.csv');

x = d(:,1);

for i=1:size(x,1)

f1(i)= 0.3148*exp(-((x(i)-67.02)/2.439).^2);

end

f1=f1';

for i=1:size(f1,1)

%converting temperature to time

Time(i)=(x(i)-21)/1.5;

end

Time=Time';

B0 = [0.521e6,294.15,1.5];

% [B,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat] = lsqcurvefit(@firstorderDSC1,B0,Time,x);

% [B,resnorm,residual] = lsqcurvefit(@firstorderDSC1,B0,Time,f1);

[B,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat] = lsqcurvefit(@firstorderDSC1,B0,Time,f1);

figure;

plot(Time,f1,Time,firstorderDSC1(B,Time))

legend('gaussian','miles')

The following is "firstorderDSC1" function;

function dndt2 = firstorderDSC1(B, t)

[time,n] = ode45(@firstorderDSC, t, 0);

for i=1:size(n,1)

dndt2(i,1) = firstorderDSC(n(i), time(i));

end

function denaturate = firstorderDSC(n, t)

% T = (21 + 1.5*t)+273.15;

a= exp(183.8); %(1/s)

% delE= 0.521*10^6; %J/mol

% R=8.3144598; %J/(K*mol)

% k = a*exp(-delE/(R*T));%+beta*heaviside(T-(55+273.15));

dndt=a*exp(-B(1)/(B(2)+B(3)*t))*(1-n);

denaturate=dndt;

end

end

I have also attached "Temp.csv"

Alan Weiss
on 25 Feb 2020

My interpretation of your reported lsqcurvefit result is that the solver agrees that you started with the optimal point. It didn't take a step because there was nowhere better to go.

To check whether I am correct, start lsqcurvefit from a different point than the optimal one and see if it takes you back to the optimal point. For example, try

B0 = 0.95*B0;

before you call lsqcurvefit.

Alan Weiss

MATLAB mathematical toolbox documentation

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/507268-trying-to-fit-a-differential-equation-to-some-data-using-lsqcurvefit-but-getting-bad-results#comment_801915

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/507268-trying-to-fit-a-differential-equation-to-some-data-using-lsqcurvefit-but-getting-bad-results#comment_801915

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/507268-trying-to-fit-a-differential-equation-to-some-data-using-lsqcurvefit-but-getting-bad-results#comment_802014

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/507268-trying-to-fit-a-differential-equation-to-some-data-using-lsqcurvefit-but-getting-bad-results#comment_802014

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/507268-trying-to-fit-a-differential-equation-to-some-data-using-lsqcurvefit-but-getting-bad-results#comment_802901

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/507268-trying-to-fit-a-differential-equation-to-some-data-using-lsqcurvefit-but-getting-bad-results#comment_802901

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/507268-trying-to-fit-a-differential-equation-to-some-data-using-lsqcurvefit-but-getting-bad-results#comment_805473

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/507268-trying-to-fit-a-differential-equation-to-some-data-using-lsqcurvefit-but-getting-bad-results#comment_805473

Sign in to comment.