Convert Array of symbolic expression to array of symbolic function handle
15 views (last 30 days)
Show older comments
I used symbolic math toolbox to perfom expression operations easily. Now I have an array of symbolic expressions, and I want to convert them into ARRAY of function handles. When I use 'matlabFunction', I get a 1 by 1 function in which I have all the expressions of the symbolic array seperated by comas. How can I get the converted expressins seperately after conversion?
Please note that I'm converting to function handle to integrate the functions fast. 'int' from symbolic toolbox takes a lot of time.
0 Comments
Answers (2)
Torsten
on 7 Nov 2022
You mean
syms x
f = [x^2;x^3];
f1 = matlabFunction(f(1))
f2 = matlabFunction(f(2))
Walter Roberson
on 7 Nov 2022
Please not that I'm converting to function handle to integrate the functions fast. 'int' from symbolic toolbox takes a lot of time.
When I use 'matlabFunction', I get a 1 by 1 function in which I have all the expressions of the symbolic array seperated by comas. How can I get the converted expressins seperately after conversion?
syms x y
f = [cos(x) * y, atan2(y,x)]
F = matlabFunction(f, 'vars', [x, y])
as_char = func2str(F)
and then you could parse the character vector. I do not recommend this, as you have the mess of worrying about worrying about balanced () since commas can occur as function arguments. It is possible with regexp(), but not something I would want to write if I did not have to.
So, what would I do? Well, I would first consider whether I was integrating all the functions over the same range, and if I was then I would integrate() with 'arrayvalued' without bothering to split the handles. But if not, if they were being integrated over separate ranges, then
FH = arrayfun(@(X) matlabFunction(X, 'vars', [x, y]), f, 'uniform', 0)
the functions have 4 variables
Ah, I guess you might be using integralN which does not support 'arrayvalued' https://www.mathworks.com/matlabcentral/fileexchange/47919-integraln-m
3 Comments
Torsten
on 7 Nov 2022
With integral(), you can't keep variables constant as symbolic constants, but you can assign values to the variables you want to keep constant and evaluate the resulting integral thereafter.
Walter Roberson
on 7 Nov 2022
If you are going to be doing symbolic integration over multiple variables then consider using the relatively new "hold" option to formulate the nesting, and then release() to do the symbolic calculation. It is not uncommon that you know that you cannot get anything useful from integrating over a single variable, but that there might be a useful result from integration over several variables; the hold option prevents it from wasting time trying to find a formula for an integration level you expect not to be productive.
See Also
Categories
Find more on Assumptions 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!