How do i graph various conditions in MATLAB

1 visualización (últimos 30 días)
Rachael Hnatek
Rachael Hnatek el 10 de Feb. de 2019
Respondida: Mark Sherstan el 10 de Feb. de 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?

Respuestas (1)

Mark Sherstan
Mark Sherstan el 10 de Feb. de 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)

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by