Breaking a function into components?
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Star Strider
2014-5-23
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
更多回答(1 个)
George Papazafeiropoulos
2014-5-23
Make a function named myfunc as follows:
%----------------------
function out=myfunc(a,vec)
out=a.*vec;
end
%----------------------
Then run the following code:
a=[5;3;8];
x=1;
y=2;
z=3;
vec=[x;y;z];
out=myfunc(a,vec)
I hope this is what you want...
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!