Is there anyway I can reduce this program to a few lines?

1 view (last 30 days)
Hello,
I would like to reduce this program to 3 lines only or more but not explicit, Any ideas?
x1=-3*pi:0.1:-2*pi;
y1=-cos(x1);
x2=-2*pi:0.1:-pi;
y2=cos(x2);
x3=-pi:0.1:0;
y3=-cos(x3);
x4=0:0.1:pi;
y4=cos(x4);
x5=pi:0.1:2*pi;
y5=-cos(x5);
x6=2*pi:0.1:3*pi;
y6=cos(x6);
plot(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6)

Accepted Answer

Matt J
Matt J on 12 Jul 2013
Edited: Matt J on 12 Jul 2013
x={-3*pi:0.1:-2*pi,
-2*pi:0.1:-pi,
-pi:0.1:0,
0:0.1:pi,
pi:0.1:2*pi,
2*pi:0.1:3*pi}.';
p=[x;cellfun(@(c,n)(-1)^n*cos(c),x,num2cell(1:6),'uni',0)];
plot(p{:})
  3 Comments
Matt J
Matt J on 12 Jul 2013
Edited: Matt J on 12 Jul 2013
Thanks, Can I have them all same color?
I leave that as an exercise for you!
Would you explain the last line, please?
See this link

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!