Index into a vector that contains functions within it

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

 Accepted Answer

I figured it out.
Use formula() to turn the function from a symfun object to just a normal sym, and then I can index into that matrix just fine. Can be turned back into a symfun by just redefining it as a symfun.

More Answers (0)

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!