How can I store symbolic variables and symbolic functions in a array?
77 次查看(过去 30 天)
显示 更早的评论
How can I store symbolic variables and symbolic functions in a array?
0 个评论
采纳的回答
Walter Roberson
2020-9-13
编辑:Walter Roberson
2020-9-17
You cannot mix symbolic expressions and symbolic functions in a single array unless it is a composite data structure such as a cell array or structure array.
You also cannot store multiple symbolic functions in a single array unless it is a composite data structure such as a cell array.
If you attempt to store multiple symbolic functions in a single non-composite array, and they do not all use the same set of variables as arguments, then MATLAB will issue an error. If they do all use the same set of variables as arguments, then instead of creating an array of functions, MATLAB will create a single function that returns an array.
MATLAB uses the same syntax of round brackets for indexing and function calls. If you had an array of functions, should A(5) be extracting the 5th function from the array or should it be calling a function passing in 5? A(5)(7) to hypothetically extract the 5th function and invoke it with parameter 7 is not valid syntax in MATLAB.
MATLAB handles this by saying that () of a symbolic function is always invoking, never indexing. Therefore to be able to separate the functions you must use a cell array as A{5}(7) is valid.
11 个评论
Walter Roberson
2020-9-17
>> r = 1; m = 3; u = sym('u', [1 m]); g1{r}(u) = sym(0)
g1 =
1×1 cell array
{1×1 symfun}
Notice that
sym('u', [1,m])
is an expression, not an assignment. The expression causes the smbolic engine to internally create variables u1, u2, u3, and put them into a vector, and then to return an internal reference to the vector that looks like '_symans_[[32,0,9705]]' ... but if you do not assign it to a variable, then that internal reference just gets discarded, and there would be no connection between the MATLAB-level variable u and the variables that live inside the symbolic engine.
When you do either of
u = sym('u', [1 m])
or
syms u [1 m]
then the MATLAB-level variable u would store that internal reference that the symbolic engine returned, and then each time the MATLAB-level variable was used in calculations, MATLAB would pass that internal reference '_symans_[[32,0,9705]]' back to the symbolic engine, which would look it up in its tables and find the associated vector of values that lives inside the symbolic engine.
The only difference (other than error checking) between
u = sym('u', [1 m])
and
syms u [1 m]
is that syms is a function that inside the function would do
assignin('caller', 'u', sym('u', [1,m]))
If the calling function happened to have a static workspace because it had an "end" that matched its "function" statement (perhaps because it was in a script or perhaps because it was a method for an object), then the assignin('caller') would generate a run-time error but the assignment form would work.
You can look at the code for syms.m and you will see the assignin('caller') about 15 lines from the end of the file: syms is just sym() with some error checking and a nicer syntax and syms does the assignment on your behalf; if you do not use syms then you need to do the assignment yourself.
更多回答(2 个)
Ameer Hamza
2020-9-13
You can create such array just like any other array in MATLAB. For example
syms x y f(x) g(x,y)
V = [x y f x];
or
syms x y f(x) g(x,y)
V(1) = x;
V(2) = y;
V(3) = f;
V(4) = g;
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!