Nonlinear curve fitting of multiple peaks
16 views (last 30 days)
Show older comments
Hi,
I have an home-made equation as y=f(x1,x2,x3). I would like to fit peaks using this formula. However, I want to us the same x1,x2,x3 to fit 3 peaks. How can I do it? Also, my equation is kind of comlicated. It contains several home-made functions. In short, the function is not as obvious as a simple formula. I don't know if this matters. Thanks!
0 Comments
Answers (2)
Bjorn Gustavsson
on 2 Jun 2020
General question, general answer.
If you have a 1-Dimensional function , f(pars,x), that depends on a couple of parameters and you want to fit a sum of that function with different sets of parameters you can do something like this:
function fx = sum_of_function(f,pars,x)
fx = 0;
for i1 = 1:size(pars,1)
fx = fx + f(pars(i1,:),x);
end
end
Then you can fit your sum-of-functions something like this:
par0 = [1,2;pi,exp(1);sqrt(2),sqrt(3)]; % whatever suitable initial parameters might be
best_par = fminsearch(@(pars) sum((curve2fit-sum_of_functions(f,pars,x)).^2),par0);
HTH
2 Comments
Bjorn Gustavsson
on 2 Jun 2020
So you want to fit one function to three different curves? Something like this?
best_par = fminsearch(@(pars) sum(sum((curves2fit-[1;1;1]*f(pars(1),pars(2),pars(3))).^2)),par0);
This should work if curves2fit is a [3 x nX] double array and f(p1,p2,p3) returns an [1 x nX] array.
Unfortunately your description is still confusing, if this is not what you want, then I suggest that you make a
clear description of what you want.
Image Analyst
on 2 Jun 2020
See my attached demo that can fit any number of Gaussians to some data. Adapt the formula from Gaussian to your special formulas and it should work. The main part is here:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% HEAVY LIFTING DONE RIGHT HERE:
% Run optimization
[parameter, fval, flag, output] = fminsearch(@(lambda)(fitgauss(lambda, tFit, y)), startingGuesses, options);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0 Comments
See Also
Categories
Find more on Nonlinear Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!