How do i graph various conditions in MATLAB

1 Ansicht (letzte 30 Tage)
Rachael Hnatek
Rachael Hnatek am 10 Feb. 2019
Beantwortet: Mark Sherstan am 10 Feb. 2019
So heres my issue. I have specific conditions. So lets say I want to graph the function y=0.005*x at x<=2000. But. At 2000<x<2500 y=10-0.02*x, and if x>2500, then y=0. I have been doing if and elseif but my graph only shows the first condition. Can you help me with the code?

Antworten (1)

Mark Sherstan
Mark Sherstan am 10 Feb. 2019
You can give the code below a try or post your own code so we can help you with your train of thought.
x = 1:4000;
y = zeros(length(x));
for ii = 1:length(x)
if x(ii)<=2000
y(ii) = 0.005*x(ii);
elseif (2000<x(ii) && x(ii)<2500)
y(ii) = 10-0.02*x(ii);
elseif x(ii)>2500
y(ii) = 0;
else
fprintf("Error at %0.f\n",x(ii));
end
end
plot(x,y)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by