# Homework 5 Solution

\$30.00

Category:

## Description

Please submit to CANVAS a .zip le that includes the following Matlab functions:

int midpoint rule.m

int trapezoidal rule.m

int Simpson rule.m

test integration.m

Exercise 1 Consider the following integral of a function f(x) on a nite interval [a; b]

• b

I(f) = f(x)dx: (1)

a

Write three Matlab/Octave functions implementing, respectively, the composite midpoint rule, the composite trapezoidal rule, and the composite Simpson rule to compute the numerical approxima-tion of I(f). Such functions should be of the form

 function [I]=int midpoint rule(fun,a,b,n) (composite midpoint rule) function [I]=int trapezoidal rule(fun,a,b,n) (composite trapezoidal rule) function [I]=int Simpson rule(fun,a,b,n) (composite Simpson rule)

Input:

fun: function handle representing f(x)

a,b: endpoints of the integration interval

n: number of evenly-spaced points in [a; b] (including endpoints)

 xj = a + (j 1)h; h = b a ; j = 1; :::n: n 1 Output: I: numerical approximation of the integral (1).

Exercise 2 Use the functions you coded in Exercise 1 to compute the numerical approximation of the integral

 1 1 3 2 x3 I = Z cos e x dx: (2) 3 1 + x2 2 30

To this end, write a Matlab/Octave function

1

[em,et,es] = function test integration()

that returns the following items:

en, et, es: row vectors with components the absolute values of the integration errors

jIref Inj n = 2; 3; :::; 10000 (3)

obtained with the midpont (vector en), trapezoidal (vector et) and Simpson (vector es) rules. Here,

Iref = 1:6851344770476

is the reference value of the integral (2) while In is the numerical approximation obtained by using the composite midpoint, trapezoidal, and Simpson rules with n nodes.

The function test integration() should also return the plot of the integrand function ap-pearing in (2) in figure(1), and the plots of the errors en, et and es versus n in a log-log scale in figure(2) (one gure with three plots). (Hint: use the Matlab command loglog()).

2

error: Content is protected !!