필터 지우기
필터 지우기

How to plot function with discontinous range

조회 수: 1 (최근 30일)
Nikhil
Nikhil 2013년 10월 4일
댓글: Nikhil 2013년 10월 4일
Hey, I want to plot P(x) over x=-10:0.5:20
P(x) is defined as
P(x)= x for 0=<x=<1;
= 2-x for 1<=x<=2;
P(x) is zero at other points.
For this I have written following code:
dx=1; xx=1:dx:20;
for i=1:1:20;
fp(1,i)=bilinear(i);
end
plot(xx',fp');
function [ z ] = bilinear( x )
if (1>=x>=0)
z=x;
elseif (2>=x>=1)
z=2-x;
else
z=0;
end
end
But after running this code, I am not getting the triangular plot which I want. Can somebody tell me where is my logic wrong?
Thanks in advance,
Nikhil

채택된 답변

Walter Roberson
Walter Roberson 2013년 10월 4일
1>=x>=0 does not do a range comparison in MATLAB. Instead it tests ((l>=x)>=0) . The l>=x subexpression will return true (1) or false (0) . Both 0 and 1 are >= 0, so the outer comparison will always return true.
Use 0 <= x & x <= 1
And after you have done the assignment, read up on logical indexing.
  댓글 수: 1
Nikhil
Nikhil 2013년 10월 4일
Thanks a lot sir! It totally solved my problem.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by