MATLAB: Breaking a function into components

matlab function

I have a function containing several variables which I am looking to pull from it the individual variables and their values. i.e. function =5x+3y+8z. I need to be able to extract from this, say just the x variable and its value, answer=5x. Thanks in advance

Best Answer

You probably need to use the Symbolic Math Toolbox, especially if you have only one such equation. (The Symbolic Math Toolbox doesn’t work efficiently for iterative calculations.)
syms x y z
f = 5*x+3*y+8*z
% Set y=0 and z=0 (then simplify if necessary):
f1 = subs(f, {y,z},{0,0})
yields:
f1 =
5*x