跳至主要內容

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)
Excellent85\ge 85
Very good76.5\ge 76.5 and <85< 85
Good64\ge 64 and <76.5< 76.5
Need improvement40\ge 40 and <64< 64
Did you try?<40< 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)f(x) has one root between x0x_{0} and x1x_{1} (x0<x1x_{0} < x_{1}). We then:

  • Evaluate ff at the midpoint xmid=(x0+x1)/2x_{\rm mid} = (x_0 + x_1)/2, i.e. compute
    fmid=f(xmid)f_{\rm mid} = f(x_{\rm mid})

  • Evaluate f(x0)f(xmid)f(x_0) \cdot f(x_{\rm mid})

    • if f(x0)f(xmid)<0f(x_0) \cdot f(x_{\rm mid}) < 0:

      ff must change sign somewhere between x0x_0 and xmidx_{\rm mid}, hence the root must lie between
      x0x_0 and xmidx_{\rm mid}, so set x1=xmidx_1 = x_{\rm mid}.

    • else:

      ff must change sign somewhere between xmidx_{\rm mid} and x1x_1, so set
      x0=xmidx_0 = x_{\rm mid}.

The above steps can be repeated a specified number of times, or until fmid|f_{\rm mid}|
is below a tolerance, with xmidx_{\rm mid} being the approximate root.

Task

The function

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

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

  1. Use the bisection method to find an approximate root xrx_{r} using 20 iterations
    (use a for loop).
  2. Use the bisection method to find an approximate root xrx_{r} such that
    f(xr)<1×106\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(xmid)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|x| < 1 the series:

(1+x)1/2=n=0(1)n(2n)!4n(n!)2xn (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/0.161/\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/0.161/\sqrt{0.16} to within 1×1051 \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悦创·编程一对一

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

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

方法一:QQopen in new window

方法二:微信:Jiabcdefh

上次编辑于:
贡献者: AndersonHJB
你认为这篇文章怎么样?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
评论
  • 按正序
  • 按倒序
  • 按热度