Legend does not match plot lines on MATLAB plot
I am currently trying to plot a function along with it's Taylor
polynomials in MATLAB but I am running into a bit of a problem. When I try
to run the following script, the first plot and its legend match, but for
some reason the second plot and its legend do not match the lines. I am
new to MATLAB and using MATLAB 2012b.
Here is the script:
% First Graph: This graph is correct
x = 0:.01:3;
f = sqrt(x);
P1 = 1 + 0.5*(x - 1);
P4 = 1 + (1/2)*(x - 1) - (1/4)*(1/2)*(x - 1).^2 ...
+ (3/8)*(1/6)*(x - 1).^3 - (15/16)*(1/24)*(x - 1).^4;
plot(x, f, '-k', x, P1, ':r', x, P4, '--b');
xlabel('x');
ylabel('y');
legend('y = sqrt(x)', 'y = P_1(x)', 'y = P_4(x)', 'Location', 'NorthWest');
% Second Graph: This graph is NOT correct
y = -4:.01:4;
g = exp(cos(y));
P1 = exp(1);
P3 = exp(1) - (exp(1)/2)*y.^2;
figure(2);
plot(y, g, '-k', y, P1, ':r', y, P3, '--b');
xlabel('x');
ylabel('y');
legend('y = exp[cos(x)]', 'y = P_1(x)', 'y = P_3(x)', 'Location', 'South');
No comments:
Post a Comment