# Math 151 Lab 05 - 02/05/2018:

DUE: Monday, February, 2018 (by 4:00pm before class starts)

INSTRUCTIONS: Save this Matlab script with the filename LastName_Lab05.m (example: Lewis_Lab05.m). Complete each question (either in words or with Matlab code). When you are ready to have your assignment graded, choose the PUBLISH command from the file menu, and submit the resulting file to CANVAS.

## Contents

## #1

WRITE CODE BELOW:

Write code for 1) (a) (b) (c) (d) listed in the file Lab05.pdf

WRITE A COMMENT BELOW:

a) Did increasing the number of rectangles make the solution generally more accurate?

b) Were the left-endpoint approximations an over or under estimate? What about the right-endpoint approximations? Is there some characteristic of the curve which suggests why the approximations are over or under estimates.

------------------------------------------------------------------------

(a) Yes

(b) Left-endpoint method was an over approximation because the function is strictly decreasing. For the same reason, right-endpoint method was an under approximation.

Function Code: ---------------------------------------------------------

function ret = left(a,b,n,f)

%a and b are outermost endpoints

%n is number of intervals

%expects function defined in f.m

x = linspace(a,b,n+1);

dx = x(2)-x(1);

s = 0;

for i = 1:n

s = s + f(x(i));

end

ret = s*dx;

function ret = right(a,b,n,f)

%a and b are outermost endpoints

%n is number of intervals

%expects function defined in f.m

x = linspace(a,b,n+1);

dx = x(2)-x(1);

s = 0;

for i = 1:n

s = s + f(x(i+1));

end

ret = s*dx;

left(0,1,5, @(x) 1-x) left(0,1,10, @(x) 1-x) right(0,1,5, @(x) 1-x) right(0,1,10, @(x) 1-x)

ans = 0.6000 ans = 0.5500 ans = 0.4000 ans = 0.4500

## #2

WRITE CODE BELOW:

Write code for 2) (a) (b) listed in the file Lab05.pdf.

WRITE A COMMENT BELOW:

Did increasing the number of rectangles to 50 significantly reduce the amount of error?

------------------------------------------------------------------------

Accuracy was increased by about 2 decimal places.

Function Code: ---------------------------------------------------------

function ret = midpoint(a,b,n,f)

%a and b are outermost endpoints

%n is number of intervals

%expects function defined in f.m

x = linspace(a,b,n+1);

dx = x(2)-x(1);

s = 0;

for i = 1:n

s = s + f((x(i+1)+x(i))/2);

end

ret = s*dx;

midpoint(0,3,10,@(x) x.^2) midpoint(0,3,50,@(x) x.^2)

ans = 8.9775 ans = 8.9991

## #3

WRITE CODE BELOW:

Write code for 3) (a) (b) listed in the file Lab05.pdf

WRITE A COMMENT BELOW:

Which method was more accurate? Trapezoidal rule or Simpson's rule?

------------------------------------------------------------------------

Trapezoidal method was more accurate. This is an example which shows more ellaborate methods do not always yield more accurate solutions.

Function Code: ---------------------------------------------------------

function ret = trapezoid(a,b,n,f)

%a and b are outermost endpoints

%n is number of intervals

%expects function defined in f.m

x = linspace(a,b,n+1);

dx = x(2)-x(1);

s = 0;

for i = 2:n

s = s + 2*f(x(i));

end

ret = (s+f(x(1))+f(x(n+1)))*dx/2;

function ret = simpson(a,b,n,f)

%a and b are outermost endpoints

%n is number of intervals

%expects function defined in f.m

if mod(n,2)==1

x = linspace(a,b,n+1);

dx = x(2)-x(1);

s = 0;

%iterate over the even indices

for i = 2:2:n

s = s + 4*f(x(i));

end

%iterate over the odd indices excluding first and last

for i = 3:2:n-1

s = s + 2*f(x(i));

end

ret = (s+f(x(1))+f(x(n+1)))*dx/3;

else

%n should be odd

ret = 'Error: n not odd';

end

trapezoid(0,pi,20,@sin) simpson(0,pi,20,@sin) simpson(0,pi,21,@sin)

ans = 1.9959 ans = Error: n not odd ans = 1.9814