02-Control statements

AI悦创原创python 1v1留学生作业辅导剑桥大学大约 8 分钟...约 2517 字

# Exercise 02.1 (if-else)

EN

Consider the following assessment criteria which map a score out of 100 to an
assessment grade:

GradeRaw score (/100)
Excellent$\ge 85$
Very good$\ge 76.5$ and $< 85$
Good$\ge 64$ and $< 76.5$
Need improvement$\ge 40$ and $< 64$
Did you try?$< 40$

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.

# Score from user
score = 72

...


# Exercise 02.2 (bisection)

EN

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.

# Task

The function

$f(x) = - \frac{x^{5}}{10} + x^3 - 10x^2 + 4x + 7$

has one root in the range $0 < x < 2$.

1. Use the bisection method to find an approximate root $x_{r}$ using 20 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.

# Exercise 02.3 (series expansion)

EN

For $|x| < 1$ the series:

$(1 + x)^{-1/2} = \sum_{n = 0}^{\infty} \frac{(-1)^n (2n)!}{4^n (n!)^2} x^n$

converges.

1. Using a for statement, approximate $1/\sqrt{0.16}$ using 30 terms in the series expansion and report the absolute error.

2. Using a while statement, compute how many terms in the series are required to approximate $1/\sqrt{0.16}$ to within $1 \times 10^{-5}$.

Store the absolute value of the error in the variable error.

# Hints

To compute the factorial, use the Python math module:

import math
nfact = math.factorial(10)


You only need import math once at the top of your program. Standard modules, like math, will be explained in a later

# Import the math module to access math.factorial
import math

# Value of x (such that (1 - x) = 0.16
x = -0.84

# Initialise approximation of the function
approx_f = 0.0

...

print("The error is:")
print(error)

## test ##
assert error < 1.0e-2

# Import the math module to access math.sin and math.factorial
import math

# Value of x (such that (1 - x) = 0.16)
x = -0.84

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

# Initialise approximation of function
approx_f = 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:

...

# Increment counter
n += 1

print("\nThe error is:", error)
print("Number of terms in series:", n)

## test ##
assert error <= 1.0e-5


AI悦创·编程一对一

AI悦创·推出辅导班啦，包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发、Web、Linux」，全部都是一对一教学：一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然，还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线，随时响应！微信：Jiabcdefh

C++ 信息奥赛题解，长期更新！长期招收一对一中小学信息奥赛集训，莆田、厦门地区有机会线下上门，其他地区线上。微信：Jiabcdefh

• 0
• 0
• 0
• 0
• 0
• 0

• 按正序
• 按倒序
• 按热度