fit using lsqcurvefit

孤者浪人 提交于 2019-12-21 20:45:17

问题


I want to fit some data to a lorentz function but I figure problems with fitting when I use parameters which are of different orders of magnitude.

This my lorentz function:

function [ value ] = lorentz( x,x0,gamma,amp )
    value = amp * gamma^2 ./ ((x-x0).^2 + gamma^2);
end

Now the script to generate sample data:

x = linspace(2e14,6e14,200);
x0 = 4.525e14;
gamma = 0.5e14;
amp = 2e-14;

y = lorentz(x,x0,gamma,amp);

And the script for fitting lorentz to the sample data:

params = [4.475e14;0.4e14;1.8e-14];
opts = optimset('TolFun',1e-60,'TolX',1e-50,'Display','Iter');
fitfunc = @(params,x) lorentz(x,params(1),params(2),params(3));
fitparams = lsqcurvefit(fitfunc,params,x,y,[],[],opts)

figure(1);hold on;
plot(x,y,'.');
plot(x,lorentz(x,params(1),params(2),params(3)),'--');
plot(x,lorentz(x,fitparams(1),fitparams(2),fitparams(3)));
hold off;

This only varies the last parameter (the smallest, which is the amplitude). If I leave all exponentials out it works as expected. I assume there is some finetuning of opts to be done, but I don't know how. Any ideas how to do this?


回答1:


As you suggested, you're going to run into numerical issues whenever you have parameters that vary over 28 (!) orders of magnitude. LSQCURVEFIT, for example, will try and estimate proper gradient steps, and those calculations may be sensitive to numerical stability (depending on the actual implementation - see http://en.wikipedia.org/wiki/Levenberg%E2%80%93Marquardt_algorithm for a summary of how all this is done).

In my experience, you need to find a way to normalize the input parameters that make them more comparable. For example, you can take the log of all the values, and then exp() them inside you're objective function.

params = log([4.475e14;0.4e14;1.8e-14])

and

function [ value ] = lorentz( x,x0,gamma,amp )
    gamma = exp(gamma); 
    amp = exp(amp);
    x0 = exp(x0);
    value = amp * gamma^2 ./ ((x-x0).^2 + gamma^2);
end

That may introduce other instabilities, but it should get you started.



来源:https://stackoverflow.com/questions/14282864/fit-using-lsqcurvefit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!