An Options pay-off question

I am trying to create a scenario where x changes from 1 to 5 with an interval of 0.1. If x<1.5, y =1.5; if 1.5<=x<3; y=x; if x>3, y = 3. I need to plot y as a function of x.
The problem is that creating a nested if loop does not create the graph of the last loop. Here's my code:
for x = 1:0.1:5
if (x<1.5)
y = 1.5;
plot(x,y)
hold on
elseif (1.5<=x<3)
y=x;
plot(x,y)
else
y=3;
plot(x,y)
hold off
end
end

댓글 수: 1

Pallav Mishra
Pallav Mishra 2013년 2월 11일
편집: Walter Roberson 2013년 2월 11일
Oh I got it..my elseif construct was wrong..my code should have been:
>> for x = 1:0.1:5
if (x < 1.5)
y = 1.5;
plot(x,y)
hold on
elseif (x >= 1.5) && (x < 3)
y = x;
plot(x,y)
else
y = 3;
plot(x,y)
end
end

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

답변 (2개)

Walter Roberson
Walter Roberson 2013년 2월 11일

0 개 추천

Right. MATLAB has no range-test operator.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2013년 2월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by