Index into a vector that contains functions within it
Show older comments
Hello all. I was wondering how to index into a vector that contains functions within it. For instance:
syms x y
assume([x,y], 'real') %this line is here to prevent Matlab from adding bars and lots of absolute value symbols
% to the symbolic variables in the display window of a live script
f(x,y) = 2*x^2+3*y;
g(x,y) = 16*x^2+14*y;
x_diff_vector = diff([f g]',x)
x_diff_vector(1) %I want this to give me the function for the derivative of f(x,y)
x_diff_vector(2) %I want this to give me the function for the derivative of g(x,y)
As of right now, those last two lines are simply executing the functions dFdx and dGdx within x_diff_vector, assuming that the (1) or (2) is a value for X. Matlab then gives an error, saying that it also expected a value in the y-position. ie (1,y) or (2,y)
Is there any simple way to index into that x_diff_vector and retrieve the functions within, or do I just have to find a different way to structure my functions such that they don't end up in a vector. The actual code I am using is more complicated, this was just an example, so I would prefer be able to index into x_diff_vector if possible.
Thanks
1 Comment
Andrew Newell
on 20 Apr 2021
Have a look at How can I define an array of symbolic functions?
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!