# How to fit to an infinite series function when the independent variable contains an undetermined parameter?

16 views (last 30 days)
Qili Hu on 12 Apr 2021 at 6:01
Answered: Alan Stevens on 12 Apr 2021 at 10:26
qt is a dependent variable; t is an independent variable; qe B h are undetermined parameters.
Procedure code is as follows.
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.^(-h)),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.^(-h)),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)));

Alan Stevens on 12 Apr 2021 at 10:26
fminsearch seems to do ok:
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];
b0 = [5, 1, -1]; % b = [qe, B, h]
b = fminsearch(@(b) fn(b,x,y), b0);
disp(b)
t = 0:120;
qt = qt_fn(t,b);
plot(x,y,'bo',t,qt), grid
xlabel('t'),ylabel('qt')
function F = fn(b,x,y)
q = qt_fn(x, b);
F = norm(y - q);
end
function qt = qt_fn(t, b)
N = 100;
term = 0;
qt = 1;
err = 1;
n = 1;
while (err>10^-8) & (n<=N)
oldterm = term;
term = -(6/pi^2)*exp(-(b(2)*n^2.*t.^(-b(3))));
qt = term + qt;
n = n+1;
err = abs(term-oldterm);
end
qt = b(1)*qt;
end