Assignment 1: Compsci 1Jc3 Introduction To Computational Thinking Fall 2020
Assignment 1: Compsci 1Jc3 Introduction To Computational Thinking Fall 2020
Assignment 1: Compsci 1Jc3 Introduction To Computational Thinking Fall 2020
Assignment 1
Dr. William M. Farmer and Curtis D’Alves
McMaster University
1 Background
The Taylor series of a function f : R → R provides a method of approxi-
mating the value of a function at x using methods from calculus. A Taylor
series is constructed at some chosen point a, and requires knowledge of the
value of the function and its derivatives at this point. Formally, the Taylor
series of f at the point a is:
∞
X f (k) (a)
(x − a)k (1)
k!
k=0
Note: f (k) (a) denotes the k th derivative of f (a) with f (0) (a) = f (a), and
k! denotes the factorial of k, i.e., k! = 1 ∗ 2 ∗ · · · ∗ (k − 1) ∗ k.
1
For most common functions, we have the following equality for x near a:
∞
X f (k) (a)
f (x) = (x − a)k (2)
k!
k=0
Notice that the fourth derivative is equal to the zeroth derivative (the orig-
inal function), so all the other derivatives are repetitions of the first four.
Therefore, we obtain the following equation:
cos(a)
cos(x) = (x − a)0
0!
− sin(a)
+ (x − a)1
1!
− cos(a)
+ (x − a)2 (4)
2!
sin(a)
+ (x − a)3
3!
cos(a)
+ (x − a)4
4!
+ ···
where the right-hand side is the Taylor series for cos at a. When a = 0, this
simplifies nicely to:
∞
X (−1)k x2k
cos(x) = (5)
(2k)!
k=0
2
Finally, let mod : R → R → R be the modulus function (i.e., remainder
of a division) for two real numbers. For example:
1.1 Aside
Feeling worried because you don’t know what a Taylor series is and why they
can be used to approximate functions? Don’t Worry! As computer scientists,
we often work with other disciplines, be it math, physics, biology, etc., and
rely on explanations from experts in those fields. Accept the knowledge
you’ve been given and apply the skills you have. You’ve been given a formula
for approximating cos. You know what formulas are, how to interpret them,
instantiate their variables, and code them up in Haskell. Don’t be scared by
something you haven’t seen before. You have all you need to complete this
assignment.
2 Assignment 1
The purpose of this assignment is to compute approximations for the trig
functions cos, sin, and tan. Do so by carefully following these requirements:
2.1 Requirements
1. Download from Avenue Assign1 Project Template.zip which con-
tains the Stack project files for this assignment. Modify the
Assign 1.hs in the src folder so that the following requirements are
satisfied.
2. Your name, the date, and “Assignment 1” are in comments at the top
of your file. macid is defined to be your MacID. Note: Your MacID
is not your student number. Your student number is a number, while
your MacID is what you use use to sign into systems like Avenue and
Mosaic.
3. The file includes a function cosTaylor of type Double -> Double ->
Double -> Double -> Double that takes the values a, cos(a), sin(a)
and x as input and computes the 4th Taylor polynomial approximation
of cos(x) at a (i.e., Equation 4 with 5 iterations on the right-hand side).
4. The file includes a function fmod of type Double -> Double -> Double
that computes mod.
3
5. The file includes a function cosApprox of type Double -> Double that
computes an approximation of cos(x) using cosTaylor with appropri-
ate values for a, cos(a), sin(a), and y as specified by the following
table:
6. The file includes a function sinApprox of type Double -> Double that
uses cosApprox and the fact that
π
sin(x) = − cos x +
2
7. The file includes a function tanApprox of type Double -> Double that
uses cosApprox, sinApprox, and the fact that
sin(x)
tan(x) =
cos(x)
8. Your file loads successfully into GHCi and all of your functions perform
correctly.
2.2 Testing
You should test on your own all of the functions in the file you submit. We
recommend that you compare your cosApprox, sinApprox, and tanApprox
function to the cos, sin, tan functions provide by Prelude. cosApprox should
be fairly close to cos for all values of x, but there will an additional error due
to using floating point arithmetic. You will be required to submit formal
test plans for all subsequent assignments, but not for this assignment.
4
3.1 Requirements
1. Add the Extra Credit functions to the Assign 1 ExtraCredit.hs file
in the src folder (not Assign 1.hs). Modify this file so that the
following requirements are satisfied.
2. Your name, the date, and “Assignment 1 Extra Credit” are in com-
ments at the top of your file. macid is defined to be your MacID.
3. The file includes a function cosApprox of type Double -> Double ->
Double that takes a value x and a tolerance t and returns an approxi-
mation of cos(x) with a relative error within tolerance t. (You may use
the built-in Haskell cos function to find the relative error). The func-
tion can result in a stack overflow if the tolerance is adjusted above
the limits of floating point arithmetic.
4. The file includes a function sinApprox of type Double -> Double ->
Double that takes a value x and a tolerance t and returns an approxi-
mation of sin(x) with a relative error within tolerance t. (You may use
the built-in Haskell cos function to find relative error). The function
can result in a stack overflow if the tolerance is adjusted above the
limits of floating point arithmetic
5. The file includes the same function tanApprox of type Double ->
Double -> Double that takes a value x and a tolerance t and returns
an approximation of tan(x) with a relative error within tolerance t.
(You may use the built-in Haskell cos function to find relative error).
The function can result in a stack overflow if the tolerance is adjusted
above the limits of floating point arithmetic
6. Your file successfully loads into GHCi and all of your functions perform
correctly.
3.2 Testing
Test on your own all of the functions in the file you submit. You will be
required to submit formal test plans for all subsequent assignments.