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

 채택된 답변

Star Strider
Star Strider 2014년 5월 23일

2 개 추천

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

댓글 수: 2

Chris
Chris 2014년 5월 24일
Thanks a heap, thats done the trick.
Star Strider
Star Strider 2014년 5월 24일
My pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

George Papazafeiropoulos
George Papazafeiropoulos 2014년 5월 23일

0 개 추천

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...

댓글 수: 1

Chris
Chris 2014년 5월 24일
Cheers George, not quite what i was looking for but I've got my answer now.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

질문:

2014년 5월 23일

댓글:

2014년 5월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by