Hi,
you can use this script:
x0 = [12 3];
x = lsqcurvefit(@(x,t)equation_fit(x,t),x0,t,S)
S_calculated = equation_fit(x,t);
plot(t,S,t,S_calculated)
function S_calc = equation_fit(x,t)
n = 1:250;
a = 1;
w2n = n.^2.*pi^2./4000;
Cs = x(1);
k = x(2);
sum_parts = ((1-(-1).^n)./(n.^2)).*(exp(-w2n.*t)+...
(k.*(1-(1+w2n.*t).*exp(-w2n.*t)))./(w2n+k.*(1-exp(-w2n.*t))));
S_calc = 1 + a.*Cs.*(1 - (4/pi^2 .* sum(sum_parts,2)));
end
But the resulting fit appears not to be a very good result to me:
Note that the last two lines of your data file contain invalid informations - i deleted them and attached the needed .mat-file to this answer, so that if you load the .mat-file to workspace you can execute the script and check the results by yourself. The result of sum_parts is a matrix with 89425x250 (number of samples of S(t) x n). In the second step the sum is build, so that you sum up from n=1...250 and the result is the S_calc with the needed dimenstion of 89425x1. This is the way you can solve this summation problem for curve fitting in Matlab.
Best regards
Stephan
Best Answer