How can i create array of symbolic expressions?

50 views (last 30 days)
syms t y(t)
d= diff(y)
for i=1:5
d= diff(d)
end
This outputs multiple differentials like this
But I need it as follows
How to do it?
syms t y(t)
d= diff(y)
d=[]
for i=1:5
d= diff(d)
d(i)=d;
end
This doesnt work since we cant input symbolic expression in array and it must be evaluated first with subs, but I need them in symbolic expression :/
Thanks for any advice.

Answers (3)

Shadaab Siddiqie
Shadaab Siddiqie on 4 Mar 2021
The "matlabFunction" function allows you to specify the desired input arguments using the "Vars" option. The following example will generate the desired function:
>> syms a b c
>> symExp = a .* b + c;
>> vectorFcn = matlabFunction(symExp, 'Vars', {symvar(symExp)});
The "symvar" function takes a symbolic expression and returns an array of the symbolic variables use in that expression in alphabetical order.
This array is then placed in a cell array when specifying the input arguments to the generated function. This indicates that the specified arguments are meant to be passed as a single vector.
The newly generated function can be called as follows:
>> vectorFcn([1 2 3])
ans =
5
This method will work for any symbolic expression. However, remember that the order of elements in the vector must correspond to the symbolic variables of the expression in alphabetical order.
If instead you would like to directly specify an order, you can simply replace the call to "symvar" with an array of symbolic variables:
>> vectorFcn = matlabFunction(symExp, 'Vars', {[a b c]});
  1 Comment
Jan Mares
Jan Mares on 4 Mar 2021
I am sorry to say that. Although I am glad for your answer, I am still unable to achieve my desired result. I am quiet new in symbolic matlab so I was unable to convert your answer to solving my problem :(

Sign in to comment.


Thomas
Thomas on 19 Jun 2023
Edited: Walter Roberson on 20 Jun 2023
does this help?
function array_of_symbolicExpressions(mx)
syms t y(t);
y(t) = t^3+t^2; % just an example
d = diff(y);
for k = 1:mx
symvar = strcat("d = diff(d)");
eval(symvar);
d_(k) = d;
end
end

Walter Roberson
Walter Roberson on 20 Jun 2023
syms t y(t)
That creates y(t) as a symfun not as a sym
d= diff(y)
When you differentiate a symfun then the output is a symfun
for i=1:5
d= diff(d)
d(i)=d;
end
As discussed above, d will be a symfun. You are trying to assign a symfun as an element of an array. However, MATLAB does not permit the construction of arrays of symfun .
If hypothetically MATLAB permitted creation of arrays of symfun, then because the syntax for indexing is to use () and the syntax for function invocation is to use () as well, it would not be possible for MATLAB to determine whether you were intending to index the array of symfun, or if you were instead intending to invoke the array of symfun with a particular parameter.
You can construct cell arrays of symfun,
for i=1:5
d = diff(d);
d_deriv{i} = d;
end
and then you can pick one out and invoke it, such as d_derive{3}(7.2) to evaluate the third derivative at value 7.2
You can create an array of symbolic expressions, such as
for i=1:5
d = diff(d);
d_deriv(i) = d(t);
end
invoking the symfun d with a particular parameter, t, gives a sym result. d_deriv with then be a vector of symbolic values, not a vector of symfun

Community Treasure Hunt

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

Start Hunting!