How do i graph various conditions in MATLAB
1 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(1 个)
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)
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!