Convert Array of symbolic expression to array of symbolic function handle

3 views (last 30 days)
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.

Answers (2)

Torsten
Torsten on 7 Nov 2022
You mean
syms x
f = [x^2;x^3];
f1 = matlabFunction(f(1))
f1 = function_handle with value:
@(x)x.^2
f2 = matlabFunction(f(2))
f2 = function_handle with value:
@(x)x.^3
  1 Comment
Yara
Yara on 7 Nov 2022
Yes, but the functions have 4 variables (doesn't matter in my question). Is it possible to convert them in matlabFunction in 1 step, instead of taking each function alone? Thanks

Sign in to comment.


Walter Roberson
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.
Possibly vpaintegral might work for your purposes.
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 = 
F = matlabFunction(f, 'vars', [x, y])
F = function_handle with value:
@(x,y)[y.*cos(x),atan2(y,x)]
as_char = func2str(F)
as_char = '@(x,y)[y.*cos(x),atan2(y,x)]'
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)
FH = 1×2 cell array
{@(x,y)y.*cos(x)} {@(x,y)atan2(y,x)}
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
Walter Roberson
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by