How do i graph various conditions in MATLAB

조회 수: 1 (최근 30일)
Rachael Hnatek
Rachael Hnatek 2019년 2월 10일
답변: Mark Sherstan 2019년 2월 10일
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?

답변 (1개)

Mark Sherstan
Mark Sherstan 2019년 2월 10일
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)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by