How to vary color on single 2d line generated using a 'for loop' and conditional statements?
Show older comments
I have completed a script that runs a range of x values through for loops that generates various outputs correlated to the conditional statements and plots them. How would I allocate 3 various colors to the curve dependent on which condition was used to generate that section of the curve? Is it possible to define the color of a curve over a specific domain or x value? I have read over a question previously posted for varying color on 2d lines but was unable to duplicate it. I have included my current script below. Thank you for any insight or direction!
clc; clear all;
x=-2:.01:6;%x defined
for k=1:length(x);%for loop initiated and k defined
if x(k)<-1;%first conditional statment
y(k)=exp(x(k)+1);%resulting statement if condition1 is met
elseif (x(k)>=-1)&(x(k)<5);%2nd condition
y(k)=2+cos(pi*x(k));%resulting statement if condtion2 is met
else
y(k)=10*(x(k)-5)+1;%resulting statement if condition3 is met
end
end
plot(x,y),xlabel('Time (SECONDS)'),ylabel('Height (KM)')
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!