Problem with fitting curve with a user defined function directly

1 view (last 30 days)
I want to fit my data with following function:
I can't fit directly. P and γ have fixed values. How can I do that?

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 1 Feb 2019
Well replace P and γ in your equation with those fixed values - that should leave you with A, β and w, as free parameters to fit for? Then you get to write your error-function either for optimization with fminsearch:
function err = my_errfcn(pars,X,Y)
A = pars(1);
beta = pars(2);
w = pars(3);
P = your-fixed-value;
gamma = your-other-fixed-value;
err = sum((Y(:)-A*P./((w^2*(1+beta*Y(:))-X(:).^2).^2+gamma^2*X(:).^2)).^2);
end
Then you'd be nicely set up for parameter fitting with fminsearch. Or for lsqnonlin:
function err = my_residualfcn(pars,X,Y)
A = pars(1);
beta = pars(2);
w = pars(3);
P = your-fixed-value;
gamma = your-other-fixed-value;
err = (Y(:)-A*P./((w^2*(1+beta*Y(:))-X(:).^2).^2+gamma^2*X(:).^2));
end
That function should be possible to use with lsqnonlin. I inserted an additional ')' into your equation since it has 2 '(' and 3 ')'...
HTH

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!