Plot y = exp(x) in the interval [-3, 3] using a solid line. Now plot a linear ap
ID: 2079710 • Letter: P
Question
Plot y = exp(x) in the interval [-3, 3] using a solid line. Now plot a linear approximation of e^x (i.e. y = x) using a dashed line with a square marker in a different color and a quadratic approximation of e^x (i .e. y = 1 + x + x^2/2) using a dotted line and a circle marker in another color. Title the plot "Plots of Approximations of exp(x)." Label the y-axis as "Approximations of exp(x)" and the x-axis as "X". Use text to label the exp(x) curve at position (2, 8). Use gtext to label the linear and quadratic approximations of exp(x) appropriately. Set axes limits to [-3, 3, -5, 15]. b. Repeat the plots in a. but this time identify the plotted lines using legend instead of text and gtext.Explanation / Answer
a.
x = -3:0.1:3;
y1 = exp(x);
y2 = x;
y3 = 1+x+(x.^2)/2;
plot(x,y1)
hold on
plot(x,y2, 'g-s')
hold on
plot(x,y3, 'c:o')
title('Plots of Approximations of e^{x}')
xlabel('x')
ylabel('Approximations of e^{x}')
text(2,8,' leftarrow e^{x}')
gtext('<-linear approximation of e^{x}')
gtext('<-quadratic approximation of e^{x}')
axis([-3 3 -5 15])
b.
x = -3:0.1:3;
y1 = exp(x);
y2 = x;
y3 = 1+x+(x.^2)/2;
plot(x,y1,x,y2, 'g-s',x,y3, 'c:o')
title('Plots of Approximations of e^{x}')
xlabel('x')
ylabel('Approximations of e^{x}')
legend('e^{x}','linear approximation of e^{x}','quadratic approximation of e^{x}')
axis([-3 3 -5 15])