<jupyter_start><jupyter_text>Exercise 02.1 (if-else)Consider the following assessment criteria which map a score out of 100 to an assessment grade:| Grade            | Raw score  (/100)      || ---------------- | ---------------------- || Excellent        | $\ge 82$               || Very good        | $\ge 76.5$ and $< 82$  || Good             | $\ge 66$ and $< 76.5$  || Need improvement | $\ge 45$ and $< 66$    || Did you try?     | $< 45$                 |Write a program that, given an a score, prints the appropriate grade. Print an error message if the input score is greater than 100 or less than zero.<jupyter_code># YOUR CODE HERE
raise NotImplementedError()<jupyter_output><empty_output><jupyter_text>Exercise 02.2 (bisection)Bisection is an iterative method for finding approximate roots of a function. Say we know that the function $f(x)$ has one root between $x_{0}$ and $x_{1}$ ($x_{0} < x_{1}$). We then:- Evaluate $f$ at the midpoint $x_{\rm mid} = (x_0 + x_1)/2$, i.e. compute   $f_{\rm mid} = f(x_{\rm mid})$- Evaluate $f(x_0) \cdot f(x_{\rm mid})$  - If $f(x_0) \cdot f(x_{\rm mid}) < 0$:     $f$ must change sign somewhere between $x_0$ and $x_{\rm mid}$, hence the root must lie between     $x_0$ and $x_{\rm mid}$, so set $x_1 = x_{\rm mid}$.     - Else    $f$ must change sign somewhere between $x_{\rm mid}$ and $x_1$, so set    $x_0 = x_{\rm mid}$.The above steps can be repeated a specified number of times, or until $|f_{\rm mid}|$is below a tolerance, with $x_{\rm mid}$ being the approximate root. TaskThe function$$f(x) = x^3 - 6x^2 + 4x + 12$$has one root somewhere between $x_0 = 3$ and $x_1 = 6$.1. Use the bisection method to find an approximate root $x_{r}$ using 15 iterations    (use a `for` loop).2. Use the bisection method to find an approximate root $x_{r}$ such that    $\left| f(x_{r}) \right| < 1 \times 10^{-6}$ and report the number of iterations    required (use a `while` loop).Store the approximate root using the variable `x_mid`, and store $f(x_{\rm mid})$ using the variable `f`.*Hint:* Use  `abs` to compute the absolute value of a number, e.g. `y = abs(x)` assigns the absolute value of `x` to `y`.   (1) Using a `for` loop.<jupyter_code># Initial end points
x0 = 3.0
x1 = 6.0

# Use 15 iterations
for n in range(15):
    # Compute midpoint
    x_mid = (x0 + x1)/2

    # Evaluate function at left end-point and at midpoint
    f0 = x0**3 - 6*x0**2 + 4*x0 + 12
    f = x_mid**3 - 6*x_mid**2 + 4*x_mid + 12
    
    # YOUR CODE HERE
    raise NotImplementedError()

    print(n, x_mid, f)
assert round(x_mid - 4.534149169921875, 10) == 0.0
assert abs(f) < 0.0009<jupyter_output><empty_output><jupyter_text>(2) Using a `while` loopUse the variable `counter` for the iteration number. *Remember to guard against infinite loops.*<jupyter_code># Initial end points
x0 = 3.0
x1 = 6.0

tol = 1.0e-6
error = tol + 1.0

# Iterate until tolerance is met
counter = 0
while error > tol:
    # YOUR CODE HERE
    raise NotImplementedError()

    # Guard against an infinite loop
    if counter > 1000:
        print("Oops, iteration count is very large. Breaking out of while loop.")
        break
    
    print(counter, x_mid, error)
assert counter == 23
assert abs(f) < 1.0e-6<jupyter_output><empty_output><jupyter_text>Exercise 02.3 (series expansion)The power series expansion for the sine function is: $$\sin(x) = \sum_{n = 0}^{\infty} (-1)^n \frac{x^{2n +1}}{(2n+1)!}$$(See mathematics data book for a less compact version; this compact version is preferred here as it is simpler to program.)1. Using a `for` statement, approximate $\sin(3\pi/2)$ using 15 terms in the series expansion and report the absolute error.1. Using a `while` statement, compute how many terms in the series are required to approximate $\sin(3\pi/2)$ to within $1 \times 10^{-8}$. Store the absolute value of the error in the variable `error`.*Note:* Calculators and computers use iterative or series expansions to compute trigonometric functions, similar to the one above (although they use more efficient formulations than the above series). HintsTo compute the factorial and to get a good approximation of $\pi$, use the Python `math` module:```pythonimport mathnfact = math.factorial(10)pi = math.pi```You only need '`import math`' once at the top of your program. Standard modules, like `math`, will be explained in a later. If you want to test for angles for which sine is not simple, you can use ```pythona = 1.3s = math.sin(a)```    to get an accurate computation of sine to check the error.  (1) Using a `for` loop<jupyter_code># Import the math module to access math.sin and math.factorial
import math

# Value at which to approximate sine
x = 1.5*math.pi

# Initialise approximation of sine
approx_sin = 0.0

# YOUR CODE HERE
raise NotImplementedError()
    
print("The error is:")
print(error)
assert error < 1.0e-12<jupyter_output><empty_output><jupyter_text>(2) Using a `while` loop*Remember to guard against infinite loops.*<jupyter_code># Import the math module to access math.sin and math.factorial
import math

# Value at which to approximate sine
x = 1.5*math.pi

# Tolerance and initial error (this just needs to be larger than tol)
tol = 1.0e-8
error = tol + 1.0

# Intialise approximation of sine
approx_sin = 0.0

# Initialise counter
n = 0

# Loop until error satisfies tolerance, with a check to avoid 
# an infinite loop
while error > tol and n < 1000:
    
    # YOUR CODE HERE
    raise NotImplementedError()
    
    # Increment counter
    n += 1    
    
    
print("The error is:")
print(error)

print("Number of terms in series:")
print(n)
assert error <= 1.0e-8<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup