An Options pay-off question
Ältere Kommentare anzeigen
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 Kommentar
Pallav Mishra
am 11 Feb. 2013
Bearbeitet: Walter Roberson
am 11 Feb. 2013
Antworten (2)
Walter Roberson
am 11 Feb. 2013
0 Stimmen
Right. MATLAB has no range-test operator.
Jan
am 11 Feb. 2013
0 Stimmen
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!