Anda di halaman 1dari 3

# COMPSCI 1JC3

## Introduction to Computational Thinking

Fall 2018

Assignment 5
Dr. Ming Ming Tan
McMaster University

## The purpose of Assignment 5 is implement a practical higher-order func-

tion. The requirements for Assignment 5 are given below. Please submit
Assignment 5 as a single .hs file to the Assignment 5 folder on Avenue
under Assessments/Assignments. Assignment 5 is due December 3, 2018
before 10am. Unlike the previous assignments, there is not an extra credit
version of Assignment 5.
Although you are allowed to receive help from the instructional
staff and other students, your submitted program must be your
own work. Copying will be treated as academic dishonesty!

1 Background
Consider a function f (x). If r is such that f (r) = 0 then r is said to be
the a root of f (x). One way to find a root of a continuous function is by
using the bisection method. The following describes the bisection method
for estimating a root of a continuous function f in the interval [a, b] within
a tolerance , where a < b and f (a) ∗ f (b) ≤ 0.
a+b
Step 1: Define m = 2 be the midpoint of the interval [a, b].
Step 2: If any of the values f (a), f (m) and f (b) are zero, then return the
corresponding point as solution and exit.
Step 3: If (b − a) ≤ , then return the value of a as solution and exit.
Step 4: If f (a) ∗ f (m) ≤ 0 then set m as the new b. Otherwise, set m as the
Next, we look at the definite integral of a function. The value of a definite
integral
Z b
f (x) dx
a
can be approximated, using the trapezoidal rule, by the summation
n
X f (xi−1 ) + f (xi ) b−a

2 n
i=1

1
where
a = x0 < x1 < · · · < xn = b
and xi − xi−1 = (b − a)/n for all i with 1 ≤ i ≤ n. The approximation
becomes more accurate as the parameter n increases in value.

2 Assignment 5
The purpose of this assignment is to create a Haskell module for approxi-
mating the root and the definite integral of a function.

2.1 Requirements
1. The name of your Haskell file is Assign 5 YourMacID.hs where Your-
2. Your name, MacID, the date, and “Assignment 5” are given in com-
ments at the top of your file.
3. The first uncommented line of the file should be

## 4. (3 marks) The file includes a function getRoot of type

(Double->Double) -> Double -> Double -> Double -> Double
such that
getRoot f a b epsilon
returns a value within the interval [a,b], which differs from a root of
function f, by less than epsilon. getRoot is implemented using the
bisection method. The bisection method works if f (a) and f (b) have
opposite signs. If the input a and b do not satisfy this condition, then
output an error message using the error function.
5. (3 marks) The file includes a function definiteIntegral of type
Double -> Double -> (Double -> Double) -> Integer -> Double
such that
definiteIntegral a b g n
computes an approximation to the definite integral
Z b
g(x) dx
a
using the trapezoidal rule with n partitions.
6. Your file can be imported into GHCi and all of your functions perform
correctly.

2
2.2 Testing
Include in your file a test plan for all the functions mentioned above. The
test plan must include at least three test cases for each function. Each test
case should have following form:
Function: Name of the function being tested.
Test Case Number: The number of the test case.
Input: Inputs for function.
Expected Output: Expected output for the function.
Actual Output: Actual output for the function.