Results 1 to 5 of 5

Math Help - [SOLVED] Matlab plotting help:peicewise with loops

  1. #1
    Newbie
    Joined
    Oct 2009
    Posts
    2

    [SOLVED] Matlab plotting help:peicewise with loops

    so i've tried to plot this funtion multiple times using every trick i can come up with and the loop still only goes through the first ifelse statement in the function file. here is a copy of my function file:

    Code:
    function y=f(x)
      if x<=-1;
        y=15;
      elseif -1<=x<=1;
        y=-5.*x+10;
      elseif 1<=x<=3;
        y=-10.*x.^2+35.*x-20;
      elseif 3<=x<=4;
        y=-5.*x+10;
      elseif 4<=x<=5 ;
        y=-10;
      end
    end
    using this file I try to plot in my main file which is:

    Code:
    delete g228x07.txt; diary g228x07.txt
    clear; clc; close all; echo on
    % Gilat 228/7
    x=[-2:.01:5];
    n=length(x);
    for i=1:n;
        y(i)=f(x(i));
        
    end
    plot(x,y);
    grid on
    
    echo off; diary off
    the first three lines are just a reference header and do not impact the problem. I don't know if the loop fails in the for loop or in the ifelse on the function file.
    Last edited by CaptainBlack; October 20th 2009 at 08:32 PM.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    The problem is here:

    Code:
    -1<=x<=1
    when x=2 that condition is still returning true. The way I usually write these expressions is shown below (im sure there is probably a better way). Try paste this code inside a file called "mhfFoo.m" and run is. It work OK on my computer.

    Code:
    function mhfFoo
    x=[-2:0.1:5];
    n=length(x);
    for i=1:n;
        y(i)=f(x(i));
    end
    plot(x,y);
    grid on
    end
    function y=f(x)
    if x<=-1;
        y=15;
    elseif x>=-1 && x<=1;
        y=-5.*x+10;
    elseif x>=1 && x<=3;
        y=-10.*x.^2+35.*x-20;
    elseif x>=3 && x<=4;
        y=-5.*x+10;
    elseif x>=4 && x<=5 ;
        y=-10;
    end
    end
    Regards Elbarto
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Oct 2009
    Posts
    2
    thanks a lot. Its wierd that the code has to be like that but it worked.
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by apocolypto View Post
    so i've tried to plot this funtion multiple times using every trick i can come up with and the loop still only goes through the first ifelse statement in the function file. here is a copy of my function file:

    Code:
    function y=f(x)
      if x<=-1;
        y=15;
      elseif -1<=x<=1;
        y=-5.*x+10;
      elseif 1<=x<=3;
        y=-10.*x.^2+35.*x-20;
      elseif 3<=x<=4;
        y=-5.*x+10;
      elseif 4<=x<=5 ;
        y=-10;
      end
    end
    using this file I try to plot in my main file which is:

    Code:
    delete g228x07.txt; diary g228x07.txt
    clear; clc; close all; echo on
    % Gilat 228/7
    x=[-2:.01:5];
    n=length(x);
    for i=1:n;
        y(i)=f(x(i));
        
    end
    plot(x,y);
    grid on
    
    echo off; diary off
    the first three lines are just a reference header and do not impact the problem. I don't know if the loop fails in the for loop or in the ifelse on the function file.
    multiple condition ifs should be of the form

    Code:
    if 0<x & x<1
       #if body
    elseif
      :
      :
    end
    [/code]
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Member
    Joined
    Mar 2007
    Posts
    206
    Awards
    1
    No problem apocolypto.

    Further the CB's reply, MATLAB has support for short circuiting behavior when evaluating statements so writing the expression as follows is better for evaluating logical scalar values (at least that seems to be the convention mathworks is using).

    Code:
    if 0<x && x<1% Note the "&&" vs "&"
       #if body
    elseif
      :
      :
    end
    It is my understand that this means if "0<x" is false, then the whole statement will be false so "x<1" does not need to be evaluated. This is probably pretty trivial but thought I would mention it as it is starting become more frequent in recent code and it is something that confused me when I was starting out.


    Regards Elbarto
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. loops in matlab, plotting matrix elements
    Posted in the Math Software Forum
    Replies: 1
    Last Post: April 10th 2011, 10:50 AM
  2. Need help with Matlab (Loops)
    Posted in the Math Software Forum
    Replies: 1
    Last Post: February 8th 2011, 01:50 AM
  3. matlab for loops?
    Posted in the Math Software Forum
    Replies: 5
    Last Post: February 28th 2010, 10:49 PM
  4. Matlab - Plotting Circles
    Posted in the Math Software Forum
    Replies: 1
    Last Post: February 16th 2009, 09:28 AM
  5. MATLAB: Plotting MATRIX?
    Posted in the Math Software Forum
    Replies: 3
    Last Post: January 25th 2009, 01:21 PM

Search Tags


/mathhelpforum @mathhelpforum