Homework #3

All problems MUST be in a SINGLE *.m file and be in separate blocks using '%% Problem X'. Otherwise you will not receive credit. You will need an additional file for the class definition. See class webpage for naming convention. NOTE: You need only submit one complete class definition file for all problems below.

Contents

Problem 1

Use our 'Nintegrate' class from Jan 26 notes as a starting point. Add a property 'Int_Method' that indicates which method of integration will be used. This can be set as 'MidPoint', 'Trapezoid' or 'Simpson'

Show this works using the code below (use our HW naming conventions).

NI=NIntegrate()
NI.Int_Method='MidPoint'

Problem 2

Within the 'IntFunction' method, use a 'switch' statement to choose between integration methods. Show this is implemented by displaying the class definition code to the command window like this:

type LastName_HW3_NIntegrate

Problem 3

Implement the 'Trapezoid' method within the 'switch' block. Calculate the integral of exp(-x) from x=0.5 to x=5 using h=.1 Display the difference of the result from the exact known value.

Problem 4

Implement the Simpson's Rule method within the 'switch' block. Calculate the integral of exp(-x) from x=0.5 to x=5 using h=.1 Display the difference of the result from the exact known value.

Problem 5

Add a method to the class that will plot the trapezoids being used for the 'Trapezoid' method. When plotting the function, sub-sample the function 100 times smaller than 'h'. Show the result when calculating the integral of exp(-x) from x=0 to x=3 using h=.5 The result should look something like the following, where I've only plotted one of the trapezoids. You should plot them all on the same graph.