How do I dynamically generate a model function for fitting or minimization?
Show older comments
For fitting response data with lsqcurvefit() or fitnlm(), I need a model function such as this Gaussian
G = @(A,X) A(1).*exp(-(X-A(2)).^2/(2*A(3).^2));
I work with response data with several features that I would like to fit. If I know the number of features beforehand, I can build a more complex model function "by hand", for example this one:
G6 = @(A,X) ...
G(A(1:3),X)+...
G(A(4:6),X)+...
G(A(7:9),X)+...
G(A(10:12),X)+...
G(A(13:15),X)+...
G(A(16:18),X);
However, I would like to be able to generate model functions with an arbitrary number of terms programatically. Typically my terms will be Gaussians or Lorentzians. I imagine calling a function to generate a function handle
modelfun = sum_of_n_terms(T,n)
, where T would be the function handle of a term (e.g., G in the above example), and n the number of times that this term should appear in modelfun. I read through the documentation on function handles and nested functions, but can't figure out how to apply this information to my problem, where the number of terms changes (rather than just some parameters of the same term). Any suggestions would be greatly appreciated.
Accepted Answer
More Answers (0)
Categories
Find more on Get Started with Curve Fitting Toolbox 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!