Back to tutorial index

# Lab #3 - Solutions

## Introduction

function lab_solns()

set1()
set2()

end

Back to lab exercises.

Back to the top

## Problem set 1

function set1()
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 set2()

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