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)

 Accepted Answer

%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 Comments

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.
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 :)
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
Fantastic. Thanks much.
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 :)

Sign in to comment.

More Answers (1)

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

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 22 Jan 2011

Community Treasure Hunt

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

Start Hunting!