How can I store an equation as a variable?

I've made a function that determines the parametric equation for a line in 3D. But now I want to plot that line using plot3. I'd like to just store the equation in one place so it's easy to access for plot3. For example I'd like to be able to do the following:
XT = t/4 + 3
YT = 3t/2 - 8
ZT = 1
plot3(XT, YT, ZT)
However, it doesn't want to store unknown variables like t in these equations. What can I do to make this work? Thanks for your time. (P.S. First time using matlab answers and I'm pretty sure I used the markup stuff wrong)

 채택된 답변

Paulo Silva
Paulo Silva 2011년 1월 22일

1 개 추천

%create the functions
XT=inline('t./4+3')
YT=inline('3*t./2-8')
ZT=inline('t')
%for example if you want to plot them easily do
ezplot(XT,-1,2) %in this case -1 is lower t and 2 is the higher t value
%if you want to convert from inline to string do
char(XT)
%example
XT=inline('t./4+3');
YT=inline('3*t./2-8');
ZT=inline('1+0*t'); %it refuses to work with just ZT=inline('1');
t=0:0.1:100;
plot3(XT(t),YT(t),ZT(t));
%Answer edited because it had mistakes (/->./ and ->.) and example added

댓글 수: 5

Greg
Greg 2011년 1월 22일
Hey, thanks for the reply, but then I have one more question further. Let's say from here I wanted to have a variable in this function which is already defined along with the one that's not. By that I mean something like this:
k=2
XT=inline('t/2 + k')
How would I go about doing that? Thanks again.
Paulo Silva
Paulo Silva 2011년 1월 22일
You can do XT=inline('t/2 + k',t,k)
and if you want to know the value of XT for a given t and k just do
X(1,2) %being t=1 and k=2
X([1 2 3],2) %for t=1 t=2 t=3 and k=2
If you already have a k value and want to put that value inside the function do
XT=inline(sprintf('t/2+%d',k))
I used the format %d but you should choose the proper format for your k value if you don't like the result.
Also be careful with vector operations, instead of / you should use ./ , same goes for * that should be .* on those inline functions,
because we want element by element operations. I forgot to format them that way in my first answer :)
Paulo Silva
Paulo Silva 2011년 1월 22일
I made a mistake on the last answer, it's
XT=inline(sprintf('t/2+%d',k),'t','k')
If you have an inline without the dots you can change it like this
XT=vectorize(XT)
%t/2+k will be changed to t./2+k
Greg
Greg 2011년 1월 22일
Fantastic. Thanks much.
Paulo Silva
Paulo Silva 2011년 1월 22일
My pleasure, if you have any other question be free to post it, we like to help, at least in my case it helps me remember important things about matlab :)

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 1월 22일

0 개 추천

You can also use symbolic variables if you have the Symbolic Toolbox.

카테고리

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

질문:

2011년 1월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by