0% found this document useful (0 votes)
3 views

Assignment 3

The document outlines the instructions for COL100 Assignment 3, which requires students to write Python programs implementing various mathematical functions using Taylor series expansions and the bisection method. Each function must include detailed documentation, loop invariants, and time complexity analysis. The assignment emphasizes the importance of original work and provides specific examples and constraints for the implementation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Assignment 3

The document outlines the instructions for COL100 Assignment 3, which requires students to write Python programs implementing various mathematical functions using Taylor series expansions and the bisection method. Each function must include detailed documentation, loop invariants, and time complexity analysis. The assignment emphasizes the importance of original work and provides specific examples and constraints for the implementation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

COL100 Assignment 3

Due date: Wednesday, 19𝑡ℎ January 2021 23:59

Instructions: All programs are to be written in Python. Functions must be


documented with proper comments explaining

• the purpose of the function

• the types and meanings of the input arguments

• any conditions on the input arguments (input specifications)

• the type and meaning of the return values

• conditions on the outputs (output specifications)

For example:

def mean(a, b):


# The mean of two numbers.
#
# INPUT parameters
# a: float -- the first number
# b: float -- the second number
#
# Returns: float -- the mean of a and b
# OUTPUT (a+b)/2

return (a + b)/2

1
Within the program you MUST document the following:

• Loop invariants

• Highlight (via a comment) the decreasing measure in each iteration which


guarantees termination

• Time complexity of the function. Use appropriate notation in your argu-


ments/explanations. You will get 0 for not writing appropriate explanations.
You will be asked to explain the same during the demo.

Write all your code in a single Python file named <entryNumber> assignment 3.py.

Other specific instructions:

1. Use the Python math library to get the value of 𝜋.

Part 1
Question 1
In this assignment we will work with iteration (loops) and write their loop in-
variants. We already learnt in class how to calculate the value of sin(𝑥) using the
Taylor series expansion. We will apply the same ideas to calculate some more
useful functions.

Implement the following functions using Taylor’s Series expansions to find the
value of the function for a given 𝑥.
𝑥2 𝑥3
1. 𝑒 𝑥 = 1 + 𝑥 + 2! + 3! +...

Write a function expn(x, n) where n is the number of terms up to which


the series is to be expanded and x is the point at which the function is to be
evaluated.

Example: As 𝑒 0 = 1 and let us suppose the number of terms is 2, the function


should behave as

>>> expn(0, 2)
1

2
𝑥2 𝑥4
2. cos(𝑥) = 1 − 2! + 4! +...

Example: As 𝑐𝑜𝑠 ( 𝜋2 ) = 0 and let us suppose the number of terms is 3, but


as the series converges on large values of 𝑛. You should obtain

>>> cosine(pi/2, 2)
0.019

1
3. 1−𝑥 = 1 + 𝑥 + 𝑥 2 + 𝑥 3 + . . . for |𝑥 | < 1

Example:

>>> inverse(0.5, 2)
1.75

𝑥2 𝑥3
4. ln(1 + 𝑥) = 𝑥 − 2 + 3 ...

Example:

>>> natural_log(0.5, 2)
0.375

𝑥3 𝑥5 𝑥7
5. tan−1 (𝑥) = 𝑥 − 3 + 5 − 7 ...

Example:

>>> tan_inv(1, 2)
0.866

Instructions:

1. The functions are to be implemented iteratively (using loops).

2. Loop invariants are to be provided for each function using comments.

3. Provide the time complexity of your functions.

3
Question 2
Bisection method: The Bisection method is one of the simplest and most reliable
of iterative methods for finding the solutions of nonlinear equations. This method,
also known as binary chopping or half-interval method, relies on the fact that if
𝑓 (𝑥) is real and continuous in the interval 𝑎 < 𝑥 < 𝑏, and 𝑓 (𝑎) and 𝑓 (𝑏) are of
opposite signs, that is, 𝑓 (𝑎) · 𝑓 (𝑏) < 0, then there is at least one real root between
𝑎 and 𝑏 (there may be more than one root in the interval). Let 𝑥 1 = 𝑎 and 𝑥 2 = 𝑏.
Let us also define another point 𝑥 0 , which is the midpoint of 𝑎 and 𝑏, that is,

𝑥1 + 𝑥2
𝑥0 =
2

Now, the following three conditions arise:

1. If 𝑓 (𝑥 0 ) = 0, we have a root at 𝑥 0 .

2. If 𝑓 (𝑥 0 ) · 𝑓 (𝑥 1 ) < 0, then there is a root between 𝑥 0 and 𝑥 1 .

3. If 𝑓 (𝑥 0 ) · 𝑓 (𝑥 2 ) < 0 then there is a root between 𝑥 0 and 𝑥 2 .

It follows that by testing the sign of the function at midpoint, we can deduce that
which part of the interval contains the root.

Note: While some books suggest finding if two numbers are of opposite sign by
multiplying them and checking if the product is negative, this idea (while correct
mathematically) is not a good idea computationally (where the approximation may
not precisely reflect the true mathematical value).

Write a program to find a root of the following functions using Bisection


Method. Take the last 2 digits of your entry number and find its remainder when
divided by 4. Depending on the result, you get one of the following functions:

0 → 𝑓 (𝑥) = cos(𝑥) + 𝑒 −𝑥

1 → 𝑓 (𝑥) = 𝑒 −𝑥 − sin(𝑥)
1
2 → 𝑓 (𝑥) = sin(𝑥) + 1−𝑥
1
3 → 𝑓 (𝑥) = 1−𝑥 − cos(𝑥)

4
For example, if your entry number is 2021CS12345, then your function is 𝑓 (𝑥) =
𝑒 −𝑥 − sin(𝑥)

Define a function named bisect(a,b,eps) to find the root of your assigned


function via iterative bisection. The functions are evaluated in the interval [𝑎, 𝑏],
and should give a result with tolerance eps. The output of your function should
be a 3-tuple (Found, Value, IterList).

• Found (type bool): denotes if there is a root of the function in the given
interval

• Value (type float): the root of the function in the given interval, if it exists

• IterList (type float list): denotes the series of approximate results as


the iteration converges towards the root (or when you exit the loop).

Constraints: Found == True implies |𝑓 (Value)| ≤ eps

Analysis: Write down the time complexity of the bisection method using com-
ments in the code.

Note: Copying from the internet or from each other will lead to a straight 0 in the
entire assignment and lead to a grade down or an F grade.

Other parts will be released soon…

Related Links
Moodle course: Here

Queries / FAQs: Here

Submission instructions: Here

You might also like