Convert Array of symbolic expression to array of symbolic function handle

8 次查看(过去 30 天)
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.

回答(2 个)

Torsten
Torsten 2022-11-7
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 个评论
Yara
Yara 2022-11-7
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

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2022-11-7
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 个评论
Torsten
Torsten 2022-11-7
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
Walter Roberson 2022-11-7
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by