I want to find the parameter x using nonlinear solver lsqnonlin.
I think the two functions are the same, but ErrorFunc2 doesn't seem to work properly with the optimization process.
Can you tell me what the problem is?
d = linspace(0,3);y = exp(-1.3*d) + 0.05*randn(size(d)); ErrorFunc = @(r)exp(-d*r)-y;x0 = 4;x = lsqnonlin(ErrorFunc,x0);x2 = lsqnonlin(@ErrorFunc2,x0);figure()hold onplot(d,y,'ko') % DATA
plot(d,exp(-x*d),'b-') % BEST FIT
plot(d,exp(-x2*d)) % BEST FITlegend('Data','x1 fit', 'x2 fit')xlabel('t')ylabel('exp(-tx)')function [error] = ErrorFunc2(x) d = linspace(0,3); y = exp(-1.3*d) + 0.05*randn(size(d)); error = exp(-d*x) -y;end
Best Answer