跳至主要內容

Assignment 2

AI悦创原创大约 5 分钟...约 1472 字

Question 1

This assignment is worth 10 points; the number of points a question is worth is in the square brackets next to the problem number.

EN
  1. [1] First, create a function Leibniz_sum(n), where n is a positive integer, which com-putes the sum

Leibniz_sum(n)=4i=0n(1)i2i+1=4(113+15+(1)n2n+1) Leibniz\_sum(n) = 4 \sum_{i=0}^{n} \frac{(-1)^i}{2i + 1} = 4 \left(1 - \frac{1}{3} + \frac{1}{5} - \dots + \frac{(-1)^n}{2n + 1}\right)

This sum was used (long before calculators and computers) to approximate the value of π\pi. Next, using the function Leibniz_sum(n) write a code to find the first value for n such that

Leibniz_sum(n)π<0.0005 |Leibniz\_sum(n) - \pi| < 0.0005

and the corresponding approximation of π\pi.

Question 2

  1. [2] Create a function solve_quadratic(a,b,c) which solves the quadratic equation ax2+bx+c=0ax^2 + bx + c = 0 (a, b, and c can be any real numbers).

(a) If there are two real, distinct roots, return the message “real distinct roots” and the two roots in a list.

(b) If there is a repeated real root, return the message “repeated real root” and its value in a list (with a single element).

(c) If the solutions are complex, return the message “complex conjugate roots” and the two complex roots. (Recall that complex(3,4) defines the complex number 3+4j3 + 4j.)

def quadratic(a, b, c):
    # 计算判别式
    delta = b**2 - 4*a*c
    
    # 判别式大于0,有两个实的不同的根
    if delta > 0:
        root1 = (-b + delta**0.5) / (2*a)
        root2 = (-b - delta**0.5) / (2*a)
        return ('real distinct roots', [root1, root2])
    
    # 判别式等于0,有一个重复的实根
    elif delta == 0:
        root = -b / (2*a)
        return ('repeated real root', [root])
    
    # 判别式小于0,有两个共轭复数根
    else:
        real_part = -b / (2*a)
        imaginary_part = (-delta)**0.5 / (2*a)
        root1 = complex(real_part, imaginary_part)
        root2 = complex(real_part, -imaginary_part)
        return ('complex conjugate roots', [root1, root2])

# 测试函数
test_results = [
    quadratic(1, 5, 6),
    quadratic(1, -22, 121),
    quadratic(1, -8, 65),
    quadratic(1, 0, 1),
    quadratic(1, -1, 0)
]

test_results

Question 3

Assume that you have done a preliminary investigation (for instance, you used software to graph), and determined that the equation f(x)=0.25x(sin(x3))2+0.12=0f(x) = 0.25 \sqrt{x} - \left( \sin \left( \frac{x}{3} \right) \right)^2 + 0.12 = 0 has 2 distinct roots in the interval [0, 10]. (Note that there is no way to solve this equation exactly using pencil and paper.)

(a)[1] Write a function to calculate the values of f(x), and use it to determine the intervals where the two roots lie. (This means you need to identify two intervals, so that each interval contains one root. Of course, there are many correct choices.)

(b)[2] Use the bisection method (look at the template for the values of the parameters) to locate the two solutions, with the tolerance as given in the template. Start the bisection with the intervals that you identified in (a). Output: for each solution, print the interval which contains it.

# Question 3(a)
import math

def f(x):
    # insert your code here

# insert print commands, as needed
# 3(b)

def bisection(f, a, b, tol=1e-10):
    """
    Approximate a root of the function f in the interval [a,b] using the bisection method. 
    For each solution:
    * start with the interval you identified in 3(a) 
    * make the bisection stop when the length of the interval < tol
    * state what that interval is
    """

     # insert your code here

# insert print commands, as needed

Question 4

[2] In this question you will approximate the (Gauss) error function

erf(x)=2πi=0(1)ix2i+1(2i+1)i!=2π(xx33+x510x742+x9216) \text{erf}(x) = \frac{2}{\sqrt{\pi}} \sum_{i=0}^{\infty} \frac{(-1)^i x^{2i+1}}{(2i+1)i!} = \frac{2}{\sqrt{\pi}} \left( x - \frac{x^3}{3} + \frac{x^5}{10} - \frac{x^7}{42} + \frac{x^9}{216} - \cdots \right)

Write the code that computes the sum above, and which stops when either of the two events happens:

(1) The number of terms added exceeds max_terms. So if max_terms=4, (and option (2) has not stopped the summation), your code should return the sum of the first four terms.

(2) The tolerance tol has been reached (and option (1) has not stopped the summation), i.e., the absolute value of the next term to be added is smaller than tol.

The remaining parameters and relevant information are given in the template.

Question 5

In this question, we represent a polynomial (of degree n)

a0+a1x+a2x2++anxn a_0 + a_1 x + a_2 x^2 + \cdots + a_n x^n

as a list of its coefficients pol=[a0,a1,a2,...,an]pol=[a_0, a_1, a_2, . . . , a_n].For instance, 65x+x36 − 5x + x^3 is represented as [6,−5,0,1]

(a)[1] Write a function differentiate__polynomial(pol) which computes the derivative of pol and represents it again as a list of coefficients.

(b)[1] Write a function integrate__polynomial(pol) which computes the antiderivative of pol and represents it again as a list of coefficients (you can assume that the integration constant is zero).

You are not allowed to use any modules/packages for this question; instead, write your own code.

公众号: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
评论
  • 按正序
  • 按倒序
  • 按热度