Using solutions from Symbolic Math Toolbox as fit functions

9 views (last 30 days)
Using the Symbolic Math Toolbox, I am calculating the eigenvalues (or better the function for the eigenvalues) of an NxN matrix (2x2 for now for simplicity), e.g. :
M = [f a; a b];
Where f is a function that depends on my x-dataset and contains two parameters to fit. a is another fit parameter and b is a constant that I define earlier, so I have three fitting parameters in total.
I managed to let MatLab solve this, but now I want to convert the N solutions (2 in the above case) to simultaniously fit to my N (2) data sets. Fitting two data sets to two functions is possible by using for example "lsqcurvefit" following this thread ( https://de.mathworks.com/matlabcentral/answers/419277-simultaneous-fitting-of-two-plots-with-two-functions ).
Now I am missing the link between having a solution from the Symbolic Math Toolbox and automatically converting this to a usable fitting function. I am aware of the function "matlabFunction", but this results in having many parameters and for lsqcurvefit I need to somehow replace all parameters with x(1),x(2),x(3) (at least to my knowledge).
Any help is much appreciated!
  1 Comment
Torsten
Torsten on 1 Sep 2022
Don't use the symbolic toolbox to calculate eigenvalues for bigger values of N.
If you don't want to wait for hours during the fitting process, use the numerical function "eig" instead.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 1 Sep 2022
Use the matlabFunction 'vars' option. Pass it a cell array of symbolic variables arrays. Each entry in the cell array will be bundled into one parameter, with the individual variables extracted from rows or columns of the input to the anonymous function.
The generated anonymous function will use obscure names similar to in2(:, 3) for y(3). The code is intended for execution, not for pretty presentation.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!