Lab #3 - Solutions


Topics in this lab

Introduction

function lab_3_solns()

set1()
set2()
set3()

end

Back to lab exercises.

Back to the top

Problem set 1

function set1()
x = linspace(-1,1,11);
% Exercise 1
y = 4*x - 12
y =

  Columns 1 through 7

  -16.0000  -15.2000  -14.4000  -13.6000  -12.8000  -12.0000  -11.2000

  Columns 8 through 11

  -10.4000   -9.6000   -8.8000   -8.0000

% Exercise 2
y = tanh(x/10)
y =

  Columns 1 through 7

   -0.0997   -0.0798   -0.0599   -0.0400   -0.0200         0    0.0200

  Columns 8 through 11

    0.0400    0.0599    0.0798    0.0997

% Exercise 3
y = -2*(x + 3).^2 + 5
y =

  Columns 1 through 7

   -3.0000   -4.6800   -6.5200   -8.5200  -10.6800  -13.0000  -15.4800

  Columns 8 through 11

  -18.1200  -20.9200  -23.8800  -27.0000

% Exercise 4
y = (3*x.^3 - 1)./(x.^2 + 3)
y =

  Columns 1 through 7

   -1.0000   -0.6967   -0.4905   -0.3772   -0.3368   -0.3333   -0.3211

  Columns 8 through 11

   -0.2557   -0.1048    0.1473    0.5000

% Exercise 5
y =  x.*exp(-x)
y =

  Columns 1 through 7

   -2.7183   -1.7804   -1.0933   -0.5967   -0.2443         0    0.1637

  Columns 8 through 11

    0.2681    0.3293    0.3595    0.3679

% Exercise 6
y  =  1./(cos(x).^2 + 1)
y =

  Columns 1 through 7

    0.7740    0.6732    0.5948    0.5410    0.5101    0.5000    0.5101

  Columns 8 through 11

    0.5410    0.5948    0.6732    0.7740

% Exercise 7
y =  exp(-10*(x-2).^2)
y =

   1.0e-04 *

  Columns 1 through 7

    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000

  Columns 8 through 11

    0.0000    0.0000    0.0056    0.4540

% Exercise 8
y = (x + 2).^(sin(pi*x))
y =

  Columns 1 through 7

    1.0000    0.8984    0.7261    0.6395    0.7079    1.0000    1.5895

  Columns 8 through 11

    2.2993    2.4812    1.8316    1.0000

end

Back to the top

Problem set 2

function set2()
close all;

f = @(x) atan(x);
g = @(x) nthroot(x,3);
h = @(x) x.^3 + (5-x).^2 - 7;

x = linspace(-5,5,200);

Exercise 1

plot(x,f(x));
hold on;
plot(x,f(10*x));
plot(x,f(x/10));

Exercise 2

clf;
plot(x,g(f(x)));

Exercise 3

figure(3);
plot(x,g(x).*f(10*h(x)));
end

Back to the top

Problem set 3

Plot the curve and tangent line Set up an anonymous function handles to define h(x).

function set3()

close all;

f = @(x) exp(cos(2*pi*x));
g = @(x) x.^2 + 5;
h = @(x) f(x)./g(x);

% Define derivatives of each function

fp = @(x) -2*pi*sin(2*pi*x).*exp(cos(2*pi*x));
gp = @(x) 2*x;
hp = @(x) (g(x).*fp(x) - gp(x).*f(x))./g(x).^2;

% Plot h(x) and a tangent line to a point on the curve
clf;
x = linspace(-5,5,201);
plot(x,h(x),'b','linewidth',2)

% Hold the plot so we can add a second plot

hold on;

% Add the tangent line at a point 'a'

a = 1.41;
T = hp(a)*(x - a) + h(a);
plot(x,T,'k','linewidth',2);

% And plot a symbol at the point of tangency.

plot(a,h(a),'r.','markersize',20)

% Plot the derivative h'(x)

plot(x,hp(x),'r-')

% Draw the x-axis so we can see where the derivative crosses the x-axis.
% It should cross whenever h(x) has a maximum or minimum.

plot(x,0*x,'k-')

% Adjust the y-axis limits and the aspect ratio

ylim([-0.2 0.75])
daspect([1 0.2 1])

% And add a title and axis labels

xlabel('x','fontsize',16)
ylabel('h(x0','fontsize',16)
title('h(x), h''(x) and tangent line','fontsize',18)
end

Back to the top

Get the code

Do you want to try the above code fragments on your own? Download the Matlab script that produces this page here. (lab_3_solns.m)

Powered by MathJax

Published with MATLAB® 8.2