Matlab help: how to create piecewise function

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

  1. terminator1010

    terminator1010 Eld

    Joined:
    Dec 23, 2001
    Messages:
    4,891
    Likes Received:
    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. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    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. terminator1010

    terminator1010 Eld

    Joined:
    Dec 23, 2001
    Messages:
    4,891
    Likes Received:
    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

    chips ...

    Joined:
    May 2, 2004
    Messages:
    3,755
    Likes Received:
    0
    Location:
    Phoenix, AZ
    i thought it said methlab :mamoru:
     
  5. MobileSuit

    MobileSuit New Member

    Joined:
    Jul 29, 2002
    Messages:
    4,285
    Likes Received:
    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. terminator1010

    terminator1010 Eld

    Joined:
    Dec 23, 2001
    Messages:
    4,891
    Likes Received:
    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. terminator1010

    terminator1010 Eld

    Joined:
    Dec 23, 2001
    Messages:
    4,891
    Likes Received:
    0
    Location:
    Hordanes Land
  8. Heir_Of_Isildur

    Heir_Of_Isildur New Member

    Joined:
    Jan 10, 2006
    Messages:
    37
    Likes Received:
    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.
     

Share This Page