0% found this document useful (0 votes)
14 views9 pages

Assign 3

The document outlines the instructions for COL100 Assignment 3, due on January 26, 2021, requiring Python programming with detailed function documentation. It includes tasks related to Taylor series expansions, iterative methods like the Bisection method, and calculations of coefficients and limits. Specific requirements include documenting loop invariants, time complexity, and ensuring outputs are accurate to six decimal places.

Uploaded by

Jadugar Chirag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views9 pages

Assign 3

The document outlines the instructions for COL100 Assignment 3, due on January 26, 2021, requiring Python programming with detailed function documentation. It includes tasks related to Taylor series expansions, iterative methods like the Bisection method, and calculations of coefficients and limits. Specific requirements include documenting loop invariants, time complexity, and ensuring outputs are accurate to six decimal places.

Uploaded by

Jadugar Chirag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

COL100 Assignment 3

Due date: Wednesday, 26𝑡ℎ 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: Use the Python math library to get the value of 𝜋
and functions like sin(𝑥), cos(𝑥), 𝑒 𝑥 (for Question 2).

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 the number of terms be 3. You should get

>>> cosine(pi/2, 3)
0.0199689

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

Example:

>>> inverse(0.5, 3)
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, 3)
0.86666667

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.

4. Your answer should be correct upto 6 decimal digits

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:

• 𝑓 (𝑎) · 𝑓 (𝑏) < 0

• Found == True implies |𝑓 (Value)| ≤ eps

Example: Consider the case when the remainder is 0, then 𝑓 (𝑥) = cos(𝑥) + 𝑒 −𝑥 .
Then the function should behave as follows:

>>> bisect(4, 5, 1e-2)


(True, 4.703125, [4.5, 4.75, 4.625, 4.6875, 4.71875, 4.703125])

Analysis: Write down the time complexity of the bisection method using com-
ments in the code. Assume that in your algorithm, the width of the bisected
interval is always greater than or equal to a threshold tolerance: tol. Use this
parameter in your analysis. Do NOT use this parameter in your 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.

5
Part 2
Question 3
For the Taylor Series given in Part 1 Question 1, write functions that return the
coefficient of 𝑥 𝑛 . Your function names should be similar to Part 1 question 1. For
eg: expn coeff, inverse coeff etc

1. Example: if we need to find coefficient of 𝑥 3 in 𝑒 𝑥 . Our function would be


as follows:

>>> expn_coeff(3)
0.16666666666

2. Remember: There might be cases when the coefficient is zero than you
must return zero. For example: if we need to find coeff of 𝑥 3 in the expansion
of cos(x)

>>> cosine_coeff(3)
0.0

3. Functions to be implemented are

expn_coeff(n):
cosine_coeff(n):
inverse_coeff(n):
natural_log_coeff(n):
tan_inv_coeff(n):

Question 4
For all the Taylor series expansion in Part 1 Question 1 write functions that return
the sum of series up to to a specific power of x 𝑛𝑖=0 𝑐 (𝑖)𝑥 𝑖 .
Í

1. Example: If we want to calculate 𝑒 3 up to the term 𝑥 3 in its expansion we

6
would get

>>> expn_t(3,3)
13.0

2. Watch Closely: This question is different from that in Part 1. For example:
( 𝜋2 ) 2 ( 𝜋2 ) 4
in case of expansion of cos( 𝜋2 ) up to 𝑥 5 we get 1 - 2! + 4!

>>> cosine_t(pi/2,5)
0.199689

3. Functions to be implemented are

expn_t(x,n):
cosine_t(x,n):
inverse_t(x,n):
natural_log_t(x,n):
tan_inv_t(x,n):

Question 5
Suppose we have two functions 𝑓 (𝑥) and 𝑔(𝑥) represented as Taylor series, 𝑓 (𝑥) =
𝑎(0) + 𝑎(1)𝑥 + 𝑎(2)𝑥 2 + ... and 𝑔(𝑥) = 𝑏 (0) + 𝑏 (1)𝑥 + 𝑏 (2)𝑥 2 + ...

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: Evaluate
one of the following functions:

0 → 𝑓 (𝑥) = cos(𝑥) and 𝑔(𝑥) = ln(1 + 𝑥)


1
1 → 𝑓 (𝑥) = 𝑒 −𝑥 and 𝑔(𝑥) = 1−𝑥
1
2 → 𝑓 (𝑥) = 1−𝑥 and 𝑔(𝑥) = 𝑒 −𝑥

3 → 𝑓 (𝑥) = ln(1 + 𝑥) and 𝑔(𝑥) = cos(𝑥)

Write the following functions:

7
(a) add coeff(𝑎, 𝑏, 𝑛) returns the coefficient of 𝑥 𝑛 in the Taylor series of 𝑓 (𝑥) +
𝑔(𝑥).

(b) mul coeff(𝑎, 𝑏, 𝑛) returns the coefficient of 𝑥 𝑛 in the Taylor series of 𝑓 (𝑥) ·
𝑔(𝑥). First try this out by hand, then write the code after having figured out
the pattern.
𝑑 ·𝑓 (𝑥)
(c) diff coeff(𝑎, 𝑛) returns the coefficient of 𝑥 𝑛 in the Taylor series of 𝑑𝑥 .

Question 6
Define a function limit diff(x,epsilon) to evaluate first order differential of
the following functions using limit definition.

𝑓 (𝑥 + ℎ) − 𝑓 (𝑥)
𝑓 ′ (𝑥) = lim
ℎ→0 𝑥

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: Evaluate
the corresponding one of the following functions:
sin(𝑥)
0 → 𝑓 (𝑥) = cos(𝑥)

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

3 → 𝑓 (𝑥) = ln(1 + 𝑥) · cos(𝑥)

Constraints:

• Use functions definition from Question 1.

• Consider ℎ = 𝜖.

• Fix the value of 𝑛 = 20.

Related Links
Moodle course: Here

8
Queries / FAQs: Here

Submission instructions: Here

You might also like