Math Help Forum

Math Help Forum Feed Site Feed

Go Back   Math Help Forum > Math Resources > Mathematics Software Discussion
Reply
 
Thread Tools Display Modes
  #1  
Old October 20th, 2009, 02:25 PM
Newbie
 
Join Date: Oct 2009
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
apocolypto is on a distinguished road
Default [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.
Reply With Quote
Advertisement
 
  #2  
Old October 20th, 2009, 07:30 PM
Member
 
Join Date: Mar 2007
Posts: 143
Country:
Thanks: 20
Thanked 21 Times in 20 Posts
elbarto is on a distinguished road
Default

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
Reply With Quote
The following users thank elbarto for this useful post:
Donate to MHF
  #3  
Old October 20th, 2009, 08:22 PM
Newbie
 
Join Date: Oct 2009
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
apocolypto is on a distinguished road
Default

thanks a lot. Its wierd that the code has to be like that but it worked.
Reply With Quote
  #4  
Old October 20th, 2009, 09:27 PM
CaptainBlack's Avatar
Grand Panjandrum
 
Join Date: Nov 2005
Location: South of England
Posts: 12,260
Country:
Thanks: 776
Thanked 3,982 Times in 3,214 Posts
CaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond reputeCaptainBlack has a reputation beyond repute
Default

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]
__________________
Truth does not change because it is, or is not, believed by a majority of the people.

Giordano Bruno
Reply With Quote
  #5  
Old October 20th, 2009, 10:19 PM
Member
 
Join Date: Mar 2007
Posts: 143
Country:
Thanks: 20
Thanked 21 Times in 20 Posts
elbarto is on a distinguished road
Default

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
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump


All times are GMT -7. The time now is 03:04 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 ©2008, Crawlability, Inc.
©2005 - 2009 Math Help Forum


Math Help Forum is a community of maths forums with an emphasis on maths help in all levels of mathematics.
Register to post your math questions or just hang out and try some of our math games or visit the arcade.