Assignment2-Template
Copyright By PowCoder代写 加微信 powcoder
### ASSIGNMENT 2 ** MATH 1MP3, Spring 2022
# Question 1: start
def Leibniz_sum(n):
“”” Write the code for the function, and check using test cases below
to make sure that it’s correct
you will use this function to answer Question 1 in the next cell
# insert your code here
# test cases to try out
print(Leibniz_sum(0)) # should print 4.0
print(Leibniz_sum(20)) # should print 3.189184782277596
print(Leibniz_sum(1000)) # 3.1425916543395442
# Question 1: continued
# Write code to find n and the corresponding approximation of pi
# (remember to run the above cell before running this one)
# for this part, you can either write a function, or just write plain code, up to you.
import math
# insert your code here
# Question 2
def quadratic(a, b, c):
“”” solve the quadratic equation ax^2+bx+c=0, and return
the answer as requested in the text of the question”””
# insert your code here
# note: the output of your print commands does not have to look identical
# to what is shown here, but must give the correct text and the correct value(s)
print(quadratic(1,5,6)) # should print (‘real distinct roots’, [-2.0, -3.0])
print(quadratic(1,-22,121)) # should print (‘repeated real root’, [11.0])
print(quadratic(1,-8,65)) # should print (‘complex conjugate roots’, [(4+7j), (4-7j)])
print(quadratic(1,0,1)) # should print (‘complex conjugate roots’, [1j, -1j])
print(quadratic(1,-1,0)) # should print (‘real distinct roots’, [1.0, 0.0])
# Questions 3a and 3b
def differentiate_polynomial(pol):
# insert your code here
def integrate_polynomial(pol):
# insert your code here
print(differentiate_polynomial([1,2,4,3,15,6])) # should print [2, 8, 9, 60, 30]
print(integrate_polynomial([12,10,9,36,5])) # should print [0, 12.0, 5.0, 3.0, 9.0, 1.0]
print(differentiate_polynomial(integrate_polynomial([1,142,54,67,8]))) # should return … think!
# Question 4
import math
def erf_series(x, tol, max_terms):
Approximate the value of erf(x) for x=0.7 to the accuracy tol, given to be 10**(-10)=1e-10
max_terms is defined on print commands
Stop the calculation when either of the two events happens:
* the number of terms added exceeds max_steps
* the tolerance tol has been reached, i.e., the absolute value of the next term to
be added is smaller than tol
Return the approximation and text as shown.
************* Note that there could be slight discrepancy in the
precision of the answer, depending how you code the summation. ****************
# insert your code here
# note: the output of your print commands does not have to look identical
# to what is shown here, but must give the correct text and the correct value(s)
# note: there could be slight discrepancy in the
# precision of the answer, depending how you code the summation
print(erf_series(0.7, 1e-10, 3)) # should print 0.6798187341903128 (max number of terms reached)
print(erf_series(0.7, tol=1e-10, max_terms=20)) # should print 0.6778011938294721 (tolerance reached)
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com