Legend Producing Wrong Colour Lines

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

VBBV
VBBV 2022년 7월 29일
can you provide the full code ?
Alyssa Neale
Alyssa Neale 2022년 7월 29일
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);

댓글을 달려면 로그인하십시오.

답변 (1개)

VBBV
VBBV 2022년 7월 29일

0 개 추천

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

VBBV
VBBV 2022년 7월 29일
편집: VBBV 2022년 7월 29일
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

댓글을 달려면 로그인하십시오.

카테고리

제품

릴리스

R2020b

태그

질문:

2022년 7월 29일

편집:

2022년 7월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by