I didn't exactly understand how you went from your function to the code, so I re-implemented it. Apparently there is a functional difference, because this code does run correctly. One of the changes I made is calculating the sum as a stand-alone array. This does use implicit expansion, so if you're using R2016a or earlier you need to expand the arrays yourself first.
xdata=[0;86400;259200000;604800000;12096000000.0000;24192000000.0000];
ydata=[1.06180000000000;0.498230000000000;0.270470000000000;0.225110000000000;0.229290000000000;0.251620000000000];
x0=[1*10^-10,3];
x = lsqcurvefit(@sorptio,x0,xdata,ydata);
function ydata=sorptio(x,xdata)
D=x(1);
h=x(2);
t=xdata(:);
n=0:1000;
S=(2*n+1).^(-2) .* exp( (-(2*n+1).^2 * pi^2 * D .*t)/(h^2) );
S=sum(S,2);
ydata=1-S*8/(pi^2);
ydata=reshape(ydata,size(xdata));
end
Best Answer