Legend Producing Wrong Colour Lines

2 views (last 30 days)
Alyssa Neale
Alyssa Neale on 29 Jul 2022
Edited: VBBV on 29 Jul 2022
Hi I have been trying to plot multiple lines on the same plot and the colours of the lines are coming up incorrectly. Here is my code and the results. I am currently running 2020b
plot(x,freal,'k')
hold on
plot(x,ftaylor0,'r')
plot(x,ftaylor1,'b')
plot(x,ftaylor2,'g')
plot(x,ftaylor3,'m')
plot(x,ftaylor4,'c')
hold off
legend('f(x)', 'p0(x)', 'p1(x)', 'p2(x)', 'p3(x)', 'p4(x)')
xlabel('x')
ylabel('f(x)')
  2 Comments
VBBV
VBBV on 29 Jul 2022
can you provide the full code ?
Alyssa Neale
Alyssa Neale on 29 Jul 2022
x=-5:0.1:5;
freal=x.*exp(-x);
ftaylor0= 1/exp(1);
ftaylor1= 1/exp(1);
ftaylor2= (1/exp(1))-(1/(2*exp(1)).*(x-1).^2);
ftaylor3= (1/exp(1))-(1./(2.*exp(1)).*(x-1).^2)+((1./(3.*exp(1))).*(x-1).^3);
ftaylor4= (1/exp(1))-(1./(2.*exp(1)).*(x-1).^2)+((1./(3.*exp(1))).*(x-1).^3)-((1./(8.*exp(1))).*(x-1).^4);

Sign in to comment.

Answers (1)

VBBV
VBBV on 29 Jul 2022
x=-5:0.1:5;
freal=x.*exp(-x);
ftaylor0= 1./exp(x); % they is scalar in your code
ftaylor1= 1./exp(-x); % this is scalar in your code
ftaylor2= (1/exp(1))-(1/(2*exp(1)).*(x-1).^2);
ftaylor3= (1/exp(1))-(1./(2.*exp(1)).*(x-1).^2)+((1./(3.*exp(1))).*(x-1).^3);
ftaylor4= (1/exp(1))-(1./(2.*exp(1)).*(x-1).^2)+((1./(3.*exp(1))).*(x-1).^3)-((1./(8.*exp(1))).*(x-1).^4);
plot(x,freal,'k')
hold on
plot(x,ftaylor0,'r')
plot(x,ftaylor1,'b')
plot(x,ftaylor2,'g')
plot(x,ftaylor3,'m')
plot(x,ftaylor4,'c')
hold off
legend('f(x)', 'p0(x)', 'p1(x)', 'p2(x)', 'p3(x)', 'p4(x)')
You can see ftaylor0 and ftaylor1 are scalars with just one point. You need to make them vectors
  1 Comment
VBBV
VBBV on 29 Jul 2022
Edited: VBBV on 29 Jul 2022
x=-5:0.1:5;
freal=x.*exp(-x);
ftaylor0= 1./exp(1) % they is scalar in your code
ftaylor0 = 0.3679
ftaylor1= 1./exp(1) % this is scalar in your code
ftaylor1 = 0.3679
ftaylor2= (1/exp(1))-(1/(2*exp(1)).*(x-1).^2);
ftaylor3= (1/exp(1))-(1./(2.*exp(1)).*(x-1).^2)+((1./(3.*exp(1))).*(x-1).^3);
ftaylor4= (1/exp(1))-(1./(2.*exp(1)).*(x-1).^2)+((1./(3.*exp(1))).*(x-1).^3)-((1./(8.*exp(1))).*(x-1).^4);
plot(x,freal,'k')
hold on
plot(ftaylor0,'>r','MarkerSize',10) % use a marker to plot
plot(ftaylor1,'sb','MarkerSize',4) % use a marker to plot
plot(x,ftaylor2,'g')
plot(x,ftaylor3,'m')
plot(x,ftaylor4,'c')
hold off
legend('f(x)', 'p0(x)', 'p1(x)', 'p2(x)', 'p3(x)', 'p4(x)','location','best')
You can also plot them as data point with proper legend

Sign in to comment.

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!