How can I store symbolic variables and symbolic functions in a array?

95 views (last 30 days)
How can I store symbolic variables and symbolic functions in a array?

Accepted Answer

Walter Roberson
Walter Roberson on 13 Sep 2020
Edited: Walter Roberson on 17 Sep 2020
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 Comments
Walter Roberson
Walter Roberson on 17 Sep 2020
>> 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.
Surath Ghosh
Surath Ghosh on 19 Sep 2020
m=3;
for r=1:m-1;
g1{r}(u) = sym(0);
for i = 1 : m+1
g1{r}(u)=g1{r}(u)+(u{i}-u1(i))./(tau)*chebyshevT(i-1,2*x(r)-1);
end
end
In the above code, I got an error as Symbolic function expected 3 inputs and received 1 in the line no 5(g1{r}(u)=g1{r}(u)+(u{i}-u1(i))./(tau)*chebyshevT(i-1,2*x(r)-1);). Please help me Sir.
Thank you in advance Sir.

Sign in to comment.

More Answers (2)

Ameer Hamza
Ameer Hamza on 13 Sep 2020
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;

Surath Ghosh
Surath Ghosh on 17 Sep 2020
Thanks a lot Sir.

Community Treasure Hunt

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

Start Hunting!