How to fit to an infinite series function?

16 views (last 30 days)
Qili Hu
Qili Hu on 24 Jan 2021
Commented: Qili Hu on 25 Jan 2021
qt is a dependent variable; t is an independent variable; qe B are undetermined parameters.
syms n t;
x=[0 5 10 15 20 30 45 60 75 90 105 120];
y=[0 3.87 4.62 4.98 5.21 5.40 5.45 5.50 5.51 5.52 5.54 5.53];
plot(x,y,'bo');
hold on
beta0=[39,0.002];
fun=@(beta,xdata) beta(1)*(1-6/(pi^2)*symsum((1/n^2)*exp(-beta(2)*(n^2)*t),n,1,inf))
betafit = nlinfit(x,y,fun,beta0);
plot(x,y,fun,beta0)
However, it does not work well. How to do it in MATLAB? Help me. Many thanks.

Accepted Answer

Vladimir Sovkov
Vladimir Sovkov on 24 Jan 2021
An iterative solution instead of the symbolic one can be more productive this case, like this one
x=[0 5 10 15 20 30 45 60 75 90 105 120];
y=[0 3.87 4.62 4.98 5.21 5.40 5.45 5.50 5.51 5.52 5.54 5.53];
plot(x,y,'bo');
hold on
pause(0.1);
beta0=[39,0.002];
% syms n t
% fun=@(beta,t) beta(1)*(1-6/(pi^2)*symsum((1./n.^2).*exp(-beta(2)*(n.^2).*t),n,1,Inf));
% betafit = nlinfit(x,y,fun,beta0);
beta1=beta0;
delta = 1e-8; % desired objective accuracy
R0=Inf; % initial objective function
for K=1:10000
fun=@(beta,t) beta(1)*(1-6/(pi^2)*sum((1./(1:K)'.^2).*exp(-beta(2)*((1:K)'.^2).*t),1));
[betafit,R] = nlinfit(x,y,fun,beta1);
R = sum(R.^2);
if abs(R0-R)<delta
break;
end
beta1=betafit;
R0 = R;
end
plot(x,fun(betafit,x),'.-r');
xlabel('x');
ylabel('y');
legend('experiment','model');
title(strcat('\beta=[',num2str(betafit),'];----stopped at--','K=',num2str(K)));
  5 Comments
Qili Hu
Qili Hu on 25 Jan 2021
Dear Sovkov,
Thank you very much. I have solved this problem. Thanks for your help again.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!