# Matlab help: how to create piecewise function

Discussion in 'OT Technology' started by terminator1010, Jan 24, 2006.

1. ### terminator1010Eld

Joined:
Dec 23, 2001
Messages:
4,891
0
Location:
Hordanes Land
Lets say I want to create a function x(t)=5 t<0 and x(t)=2 t>=0 how do you do this?

2. ### deusexaetheraOT Supporter

Joined:
Jan 27, 2005
Messages:
19,712
0
God, I hate Matlab. Whatever the other math equation program is, the one that physics professors like, that's the better one by far. I don't think anybody uses Matlab in real life.

3. ### terminator1010Eld

Joined:
Dec 23, 2001
Messages:
4,891
0
Location:
Hordanes Land
Cmon, i know someone knows the answer to this. I need to know how to do this for an assigment due tomorrow!!

4. ### chips...

Joined:
May 2, 2004
Messages:
3,755
0
Location:
Phoenix, AZ
i thought it said methlab

5. ### MobileSuitNew Member

Joined:
Jul 29, 2002
Messages:
4,285
0
Location:
NoVA
Actually Mathlab is a pretty sweet program. A lot of people use it in the DoD for research and testing purposes, and I'm sure its used other places too. It really is an awesome software package...

Sorry I didnt get here earlier though, are you trying to plot a piecewise function? Or do you not care because it was due earlier today...

One way would be to set up two different X's

like for t>0 y=X_1
and for t<0 y=X_2

and then you set up the X variables to contain the right variables

6. ### terminator1010Eld

Joined:
Dec 23, 2001
Messages:
4,891
0
Location:
Hordanes Land
okay, I tried:
for t>0
x=5
end
but its giving me an error: Missing variable or function.

but if instead of "for t>0" I put, say, "for t=3" It works fine. Any idea why I can't use '>' or '<'?

7. ### terminator1010Eld

Joined:
Dec 23, 2001
Messages:
4,891
0
Location:
Hordanes Land
help??

8. ### Heir_Of_IsildurNew Member

Joined:
Jan 10, 2006
Messages:
37
0
Location:
Paris
You are making a for loop.

Syntax for a for loop in MATLAB is -

for x = 1:n, {THIS IS THE INITIALISATION OF 'x',THE INCREMENT, AND ITS LIMIT}
~functionality~
end

You have put "for t>0".
It is not possible to initialise a variable to an inequality.

Hope it's clear.