代写 math python statistic MATH 210 Assignment 1¶

MATH 210 Assignment 1¶
Instructions¶
• There are 4 problems and 20 total points.
• Write your solutions in the cells below.
• You may work on these problems with others but you must write your solutions on your own.
• Do not import any Python packages such as math or numpy to complete this assignment.
• Execute the test cells to verify that your solutions pass.
• This notebook contains hidden tests! Your solution may not be completely correct even if it passes all tests below.
• Submit this notebook to Canvas before 11:59pm Friday September 20.

Problem 1 (3 points)¶
Consider the Taylor series
$$ \frac{1}{2} \ln \left( \frac{1 + x}{1 – x} \right) = \sum_{n=0}^{\infty} \frac{x^{2n+1}}{2n+1} \ , \ x \in (-1,1) $$
Write a function called n_sum which takes 2 input parameters x and N and returns the sum
$$ \sum_{n=0}^{N} \frac{x^{2n+1}}{2n+1} $$
In [ ]:
# YOUR CODE HERE
In [ ]:
“Check that n_sum returns the correct datatype.”
assert type(n_sum(1,1)) == float , “Return value should be a float.”
print(“Problem 1 Test 1: Success!”)
In [ ]:
“Check that n_sum returns the correct values.”
epsilon = 10e-14
assert abs(n_sum(1,1) – 4/3) < epsilon , "Value n_sum(1,1) should be 4/3." print("Problem 1 Test 2: Success!") In [ ]: "Check that n_sum returns the correct values." epsilon = 10e-14 assert abs(n_sum(0.2,50) - 0.20273255405408222) < epsilon , "Value n_sum(0.2,50) should be 0.20273255405408222" print("Problem 1 Test 3: Success!") Problem 2 (4 points)¶ Write a function called mn_sum which takes 1 input parameter N, and returns the sum $$ \sum_{n=1}^{N} \sum_{m=1}^{N} \frac{1}{m^2 + n^2} $$ In [ ]: # YOUR CODE HERE In [ ]: "Check that mn_sum returns the correct datatype." assert type(mn_sum(10)) == float , "Return value should be a float." print("Problem 2 Test 1: Success!") In [ ]: "Check that mn_sum returns the correct values." epsilon = 10e-14 assert abs(mn_sum(1) - 1/2) < epsilon , "Value mn_sum(1) should be 1/2." print("Problem 2 Test 2: Success!") In [ ]: "Check that mn_sum returns the correct values." epsilon = 10e-14 assert abs(mn_sum(2) - (1/2 + 2/5 + 1/8)) < epsilon , "Value mn_sum(2) should be 41/40." print("Problem 2 Test 3: Success!") Problem 3 (4 points)¶ Represent a polynomial $p(x)=c_0+c_1x+c_2x^2+\cdots+c_dx^d$ as a list of coefficients $[c_0,c_1,c_2,\dots,c_d]$. Write a function called poly_int which takes 3 input parameters p, a and b, where p is a Python list of numbers representing a polynomial $p(x)$, and a and b are numbers. The function returns the value of the definite integral $$ \int_a^b p(x) \, dx $$ In [ ]: # YOUR CODE HERE In [ ]: # "Check that poly_int returns the correct type." assert type(poly_int([1,1,1],0,1)) == float , "Return value should be a float." print("Problem 3 Test 1: Success!") In [ ]: "Check that poly_int returns the correct values." epsilon = 10e-14 assert abs(poly_int([0,1],0,1) - 1/2) < epsilon , "Value poly_int([0,1],0,1) should be 1/2." print("Problem 3 Test 2: Success!") In [ ]: "Check that poly_int returns the correct values." epsilon = 10e-14 assert abs(poly_int([1,1,1,1],0,1) - (1 + 1/2 + 1/3 + 1/4)) < epsilon , "Value poly_int([1,1,1,1],0,1) should be 1 + 1/2 + 1/3 + 1/4." print("Problem 3 Test 3: Success!") Problem 4 (8 points)¶ Write a function called statistics which takes one input parameter samples, a list of numbers, and returns a tuple (of length 7) of statistics: (mean, standard deviation, minimum, median, maximum, range, total number of samples) Recall, if samples is the list of numbers $[x_0,x_1,...,x_N]$ then $$ \begin{align} \text{mean} & : & \mu = \frac{1}{N+1}\sum_{k=0}^N x_k \\ \text{standard deviation} & : & \sqrt{\frac{\sum_{k=0}^N (\mu - x_k)^2}{N + 1}} \\ \text{range} & : & \max\{x_k\} - \min\{x_k\} \\ \end{align} $$ The median is the middle value if the number of samples is odd, or the median is the average of the two middle values if the number of samples is even. Use the built-in function sorted to sort the list of values. Remember: • Do not import any Python packages • Do not use Python keywords or built-in function names (such as max, min, and range) as variable names In [ ]: # YOUR CODE HERE In [ ]: "Check that statistics returns the correct datatype." assert type(statistics([1.3,5.2,3.7])) == tuple , "Return value should be a tuple." assert len(statistics([1.3,5.2,3.7])) == 7 , "Return value should be a tuple of length 7." print("Problem 4 Test 1: Success!") In [ ]: "Check that statistics returns the correct mean value." epsilon = 10e-14 assert abs(statistics([1.0,2.0,3.0])[0] - 2.0) < epsilon , "Mean value failed." print("Problem 4 Test 2: Success!") In [ ]: "Check that statistics returns the correct median value." assert abs(statistics([1.0,2.0,3.0])[3] - 2.0) < epsilon , "Median value failed." print("Problem 4 Test 3: Success!") In [ ]: "Check that statistics returns the correct standard deviation value." epsilon = 10e-14 assert abs(statistics([1.0,2.0,3.0])[1] - (2/3)**0.5) < epsilon , "Standard deviation value failed." print("Problem 4 Test 4: Success!") In [ ]: "Check that statistics returns the correct range values." epsilon = 10e-14 assert abs(statistics([1.0,2.0,3.0])[2] - 1.0) < epsilon , "Minimum value failed." assert abs(statistics([1.0,2.0,3.0])[4] - 3.0) < epsilon , "Maximum value failed." assert abs(statistics([1.0,2.0,3.0])[5] - 2.0) < epsilon , "Range value failed." assert statistics([1.0,2.0,3.0])[6] == 3 , "Total value failed." print("Problem 4 Test 5: Success!")