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

 Accepted Answer

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

Thank you so much for your kind reply.
Please check pdf file. Actully I want to write those symbolic functions in a for loop in matlab. Right hand side of those functions are not same, the coefficients of the unknown variables will be different. So, functions are different.
All of your functions have the same set of parameters, to . You could create a single array
F(u1, u2, up to um) = [body_for_f1, body_for_f2, body_for_f3 etc]
but then you would not be able to evaluate the functions separately.
What you should probably do use use a cell array,
syms u [1 m]
F = cell(m, 1);
for n = 1 : m
F{n}(u) = appropriate body for f_n;
end
Thank you so much Sir.
Please tell me what should be comment for g1{r}(u) = 0.
Actually I want to write the code as below. But there is a error in the second line. How can I fix that?
for r=1:m-1
g1{r}(u) = 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
g1{r}(u) = 0;
What datatype is u? What size is u?
g1{r}(u)=g1{r}(u)+(u{i}-u1(i))./(tau)*chebyshevT(i-1,2*x(r)-1);
You have u{i} so u must be a cell array or table or string object. But it is not valid to use a cell array or table or string object as an index, such as for g1{r}(u) = 0
m=3;
syms u [1 m]
Yes Sir. u is a cell array. I want to store 0 into g1{r}(u). Is there any option to do that?
syms u [1 m]
does not create a cell array. It is equivalent to:
for K = 1 : m
u(K) = sym(sprintf('u%d',K));
end
so u = [u1 u2 u3] would be the result, and that is not a cell array.
Anyhow, the answer you are looking for is
g1{r}(u) = sym(0);
Thank you Sir.
In my code I have written u as
sym('u',[1,m])
.
But g1{r}(u) = sym(0) is not working.
>> r = 1; m = 3; syms u [1 m]; g1{r}(u) = sym(0)
g1 =
1×1 cell array
{1×1 symfun}
Works for me.
Please write sym('u',[1,m]) instead of syms u [1 m] and check that.
It is not working.
>> 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.
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)

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!