Question 2: Calculating e The constant e, sometimes called Euler’s number, is important in many areas of mathematics and…

Question:

Transcribed Image Text:

Question 2: Calculating e

The constant e, sometimes called Euler’s number, is important in many areas of mathematics and

physics. An approximation for it is present in the constant math.e, which is 2.718281828459045.

In this question you will write code to calculate the value of e and ex.

Exercise

You may use import math for this question, but the only thing you should use from it is

math. factorial. Do not use anything else from the math module for this question.

It turns out that we can calculate e* using the following sum:

хо х1 х2 х3 x4

x5 x6

0! 1! 2! 3! 4! 5! 6!

+

+

+ + + + +

Write a function exponent (x: float, threshold: float) -> float.

The function uses the pattern above to approximate ex, including only terms that are at least

as big as threshold. It is given that threshold is a positive number.

For example, consider terms in exponent (2.0, 0.7):

хо

0!

3!

=

2!

ex

1

= 1; 1 ≥ 0.7, so we include this term.

1

2

= 2; 2 ≥ 0.7, so we include this term.

2; 2 ≥ 0.7, so we include this term.

1.333…; 1.333… > 0.7, so we include this term.

4

IS

8

6

16

= 0.666…; 0.666… < 0.7, so we do not include this or any subsequent term.
=
4! 24
So exponent (2.0, 0.7) must return 1+2+2+1.333... ≈ 6.3333.
On the other hand, exponent (2.0, 2.0) must return 0; since the first term of the pattern is less
than 2.0, we are adding up no terms.
Consider also terms in exponent (1.0, 0.5):
xº 1
=
O!
= 1;1 ≥ 0.5, so we include this term.
= 1; 1 ≥ 0.5, so we include this term.
0.5; 1 ≥ 0.5, so we include this term.
2
=
= 0.1666...;0.1666 < 0.5, so we do not include this or any subsequent term.
3! 6
So exponent (1.0, 0.5) must return 1 + 1 +0.5 = 2.5.
Test your code carefully when x > 0, but also when x < 0. This approximation should work for
any value of x.
Expert Answer:
Answer rating: 100% (QA)
Unfortunately I can t execute code but I can certainly guide you on how to write the function in Pyt
View the full answer