How to plot a piecewise function?
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Alexandros Dhimas
am 20 Dez. 2016
Kommentiert: Walter Roberson
am 25 Dez. 2020

3 Kommentare
Stephen23
am 25 Dez. 2020
Original question asked by Alexandros Dhimas on 20th December 2016 retrieved from Google Cache:
How to plot a polyclonal expression using if statement?
Hello, I' ve got a polyclonal function to plot (using if statement) as shown on this picture:

for -5<x<30.
My thougth on the solution was:
x=linspace(-5,30,1000);
if x<0
y=10*x.^0;
else if x>=0 & x<9
y=10*x+10;
else if 9<=x
y=15*sqrt(4*x)+10;
end
end
end
plot(x,y)
But i get this on the command window "Error using plot A numeric or double convertible argument is expected"
I can't find any mistakes on my code.
Akzeptierte Antwort
Weitere Antworten (2)
Walter Roberson
am 20 Dez. 2016
Either use a for loop or (better) use logical indexing. http://blogs.mathworks.com/steve/2008/01/28/logical-indexing/
0 Kommentare
Roger Stafford
am 21 Dez. 2016
If you only want to plot the function, do this:
x1 = linspace(-5,0,50); y1 = 10*ones(1,50);
x2 = linspace(0,9,90); y2 = 10*x2+10;
x3 = linspace(9,30,210); y3 = 15*sqrt(4*x3)+10;
plot([x1,x2,x3],[y1,y2,y3])
Another way:
x = linspace(-5,30,400);
y = (x<0)*10+(x>=0&x<9).*(10*x+10)+(x>=9).*(15*sqrt(4*abs(x))+10);
plot(x,y)
0 Kommentare
Siehe auch
Kategorien
Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!