How can i create array of symbolic expressions?
조회 수: 22 (최근 30일)
이전 댓글 표시
syms t y(t)
d= diff(y)
for i=1:5
d= diff(d)
end
This outputs multiple differentials like this ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/535174/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/535174/image.png)
But I need it as follows ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/535179/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/535179/image.png)
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.
댓글 수: 0
답변 (3개)
Shadaab Siddiqie
2021년 3월 4일
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]});
Thomas
2023년 6월 19일
편집: Walter Roberson
2023년 6월 20일
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
댓글 수: 0
Walter Roberson
2023년 6월 20일
syms t y(t)
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
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!