How to fit to an infinite series function?

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

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)));

8 Comments

Dear Sovkov,
Thanks for your solution. After running, it does not work. So can you help me again?
Best wishes.
Yours sincerely,
Qili Hu
What messages do you get? I have rerun this exact code with my Matlab 2019b at Windows-10 system, and it has worked fairly well
I want to get this figure as you give. But I does not obtain it after running. I does not know why. I run the following code with my Matlab 2016a at Windows-10 system, and it does not work.
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];
beta1=beta0;
delta = 1e-8;
R0=Inf;
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)));
I have tried Matlab 2017a. It works the same way as Matlab 2019b, no problem. Maybe, you should update your Matlab installation to a newer version.
When I am in St. Petersburg University, I use Matlab 2019b; when I am in Shanxi University, I use Matlab 2017a. Both work fairly well.
The other way could be replacing the "sum" construction with an equivalent loop but this would slow down the computation significantly.
Dear Sovkov,
Thank you very much. I have solved this problem. Thanks for your help again.
Dear Sovkov
Excuse me. Thank you for answering how to solve the an infinite series function for me earlier. I have another question about how to obtain the standard deviation of the fitting parameters. I haven't learned how to obtain it yet. I hope you can help me continue to answer it. Thank you very much!
These are the program codes you helped me answer earlier.
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)));
Hi! The mathematics of how to estimate the standard deviations in LSF using the local linear approximation is described in many textbooks. Briefly, it requires estimating derivatives of your computed function over its parameters, construction from them the design matrix (Jacoby matrix), and applying some standard equations to find the covariance matrix of the parameters; its diagonal elements are the squares of the sought standard deviation. Programming of all this "from scratch" is somewhat complucated, and I do not have time to do it for you now. However, you can download my package Optimizer from https://sourceforge.net/projects/optimizer-sovkov/, adopt the interface of your program to its regulations, and this would privide you with those estimates automatuclly. If you believe that the linear appriximation is not enough in your case, the Monter-Carlo-type computation can be used, in which you generate pseudo-randomly the cloud in your parameter space and estimate its spread within the spread of the computed data around your experimental curve.
Sorry for the misprints, I was in a hurry. Hope, it is still understandable.

Sign in to comment.

More Answers (0)

Categories

Find more on Parallel Computing 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!