Results 1 to 3 of 3

Math Help - Matlab loop help

  1. #1
    Newbie
    Joined
    Dec 2007
    Posts
    14

    Matlab loop help

    Matlab loop help
    Code:
    for x = 1:15
    
      if  x < 5
        y = 2*x - 5;
        
      elseif  5 < x <10
        y = 4*x;
       
      elseif  10< x <15
        y = x*x;
                
      end;
    
    end;
    
    plot(x,y)
    Why doesnt this work?
    Last edited by CaptainBlack; July 20th 2008 at 11:19 AM.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by j-lee00 View Post
    Matlab loop help
    Code:
    for x = 1:15
     
      if  x < 5
        y = 2*x - 5;
     
      elseif  5 < x <10
        y = 4*x;
     
      elseif  10< x <15
        y = x*x;
     
      end;
     
    end;
     
    plot(x,y)
    Why doesnt this work?
    That depend on what you expect it to do. x and y are scalars when you get to the plot command, what do you want them to be?

    RonL
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by CaptainBlack View Post
    That depend on what you expect it to do. x and y are scalars when you get to the plot command, what do you want them to be?

    RonL
    One interpretaion of what you are trying to do would be:

    Code:
    x = 1:15;
    y=zeros(1,15);
    
    y=y+(x<=5).*(-y+2*x-5);
    y=y+((5<x)&(x<=10)).*(-y+4*x);
    y=y+((10<x)&(x<=15)).*(-y+x.*x);
     
    plot(x,y)
    RonL
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Matlab question! Help! Infinite loop.
    Posted in the Math Software Forum
    Replies: 3
    Last Post: June 30th 2010, 03:07 AM
  2. Matlab while loop, Please help!
    Posted in the Math Software Forum
    Replies: 1
    Last Post: April 23rd 2010, 02:14 AM
  3. help with MATLAB questdlg loop
    Posted in the Math Software Forum
    Replies: 1
    Last Post: March 7th 2010, 10:23 AM
  4. Creating a loop in Matlab help
    Posted in the Math Software Forum
    Replies: 1
    Last Post: February 11th 2010, 04:06 AM
  5. Matlab While Loop Help
    Posted in the Math Software Forum
    Replies: 1
    Last Post: March 8th 2009, 08:50 PM

Search Tags


/mathhelpforum @mathhelpforum