程序代写代做代考 python MATH 210 Quiz 2¶

MATH 210 Quiz 2¶
In [1]:
import numpy as np

Notation¶
\begin{align*} \Delta x &= \frac{b – a}{N} \\ & \\ x_n &= a + n \Delta x \\ & \\ R_N(f) &= \sum_{n=1}^N f(x_n) \Delta x \\ & \\ E^R_N(f) &= \left| \ \int_a^b f(x) \, dx – R_N(f) \ \right| \leq \frac{(b – a)^2}{2 N} K_1 \ \ , \ \ | f'(x) | \leq K_1 \ \ \text{for all} \ \ x \in [a,b] \\ & \\ L_N(f) &= \sum_{n=1}^N f(x_{n-1}) \Delta x \\ & \\ E^L_N(f) &= \left| \ \int_a^b f(x) \, dx – L_N(f) \ \right| \leq \frac{(b – a)^2}{2 N} K_1 \ \ , \ \ | f'(x) | \leq K_1 \ \ \text{for all} \ \ x \in [a,b] \\ & \\ M_N(f) &= \sum_{n=1}^N f(m_n) \Delta x \ \ , \ \ m_n = \frac{x_{n-1} + x_n}{2} \\ & \\ E^M_N(f) &= \left| \ \int_a^b f(x) \, dx – M_N(f) \ \right| \leq \frac{(b – a)^3}{24 N^2} K_2 \ \ , \ \ | f”(x) | \leq K_2 \ \ \text{for all} \ \ x \in [a,b] \\ & \\ T_N(f) &= \sum_{n=1}^N \frac{f(x_{n-1}) + f(x_n)}{2} \Delta x \\ & \\ E^T_N(f) &= \left| \ \int_a^b f(x) \, dx – T_N(f) \ \right| \leq \frac{(b – a)^3}{12 N^2} K_2 \ \ , \ \ | f”(x) | \leq K_2 \ \ \text{for all} \ \ x \in [a,b] \\ f(x) &= f(a) + f'(a)(x – a) + \frac{f”(a)}{2}(x – a)^2 + \cdots + \frac{f^{(n)}(a)}{n!}(x – a)^n + \frac{f^{(n+1)}(c)}{(n+1)!}(x – a)^{n+1} \end{align*}

Loops¶
Determine the output of the following Python code in terms of the variable x.
In [2]:
x = 2
for n in range(0,3):
x = 1 – x**2
print(x)

-63

$$ – x^{8} + 4 x^{6} – 4 x^{4} + 1 $$
In [3]:
x = 2
– x**8 + 4*x**6 – 4*x**4 + 1
Out[3]:
-63
In [4]:
x = 3
for n in range(0,3):
x = 1 + x**2
print(x)

10202

$$ x^{8} + 4 x^{6} + 8 x^{4} + 8 x^{2} + 5 $$
In [5]:
x = 3
x**8 + 4*x**6 + 8*x**4 + 8*x**2 + 5
Out[5]:
10202
In [6]:
x = -2
for n in range(0,2):
x = x*(1 – x)
print(x)

-42

$$ – x^{4} + 2 x^{3} – 2 x^{2} + x $$
In [7]:
x = -2
– x**4 + 2*x**3 – 2*x**2 + x
Out[7]:
-42
In [8]:
x = 5
for n in range(0,2):
x = x*(1 + x)
print(x)

930

$$ x^{4} + 2 x^{3} + 2 x^{2} + x $$
In [9]:
x = 5
x**4 + 2*x**3 + 2*x**2 + x
Out[9]:
930

The following Python code computes an approximation of what exact value?
In [10]:
xn = 1
for n in range(0,100):
xn = xn – (xn**2 – 4*xn – 4)/(2*xn – 4)
print(xn)

-0.8284271247461901
In [11]:
2*(1 – 2**0.5)
Out[11]:
-0.8284271247461903
In [12]:
xn = 3
for n in range(0,100):
xn = xn – (xn**2 – 4*xn – 4)/(2*xn – 4)
print(xn)

4.82842712474619
In [13]:
2*(1 + 2**0.5)
Out[13]:
4.82842712474619
In [14]:
xn = 3
for n in range(0,100):
xn = xn – (xn**2 – 8*xn + 8)/(2*xn – 8)
print(xn)

1.17157287525381
In [15]:
2*(2 – 2**0.5)
Out[15]:
1.1715728752538097
In [16]:
xn = 5
for n in range(0,100):
xn = xn – (xn**2 – 8*xn + 8)/(2*xn – 8)
print(xn)

6.82842712474619
In [17]:
2*(2 + 2**0.5)
Out[17]:
6.82842712474619

Plotting¶
Determine the correct order of lines to plot the function $y = e^{-t/2} \cos(15t)$ over the interval $[0,10]$.
In [19]:
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0,10,300)
y = np.exp(-t/2)*np.cos(15*t)
plt.plot(t,y)
plt.show()

NumPy arrays¶
Determine the output of the Python code below in terms of the value a.
In [20]:
a = 2
x = np.array([a**n for n in range(0,5)])
S = np.sum(x[1:])
print(S)

30
In [21]:
a + a**2 + a**3 + a**4
Out[21]:
30
In [22]:
x = np.array([a**n for n in range(0,5)])
S = np.sum(x[:-1])
print(S)

15
In [23]:
1 + a + a**2 + a**3
Out[23]:
15

Riemann sums¶
• If $f'(x) \geq 0$ for all $x \in [a,b]$ then $R_N(f) \geq \int_a^b f(x) dx$. TRUE
• Consider the definite integral $\int_a^b f(x) \, dx$. Then $E^M_N(f) \leq E^R_N(f)$ for any $N$. FALSE
• Consider the definite integral $\int_a^b f(x) \, dx$. If $f(x)$ is not constant, then $E^R_N(f) \not= 0$ for any $N$. FALSE
• Consider the definite integral $\int_1^2 x^x \, dx$. Then $E^L_N(f) < E^M_N(f) < E^R_N(f)$ for any $N$. TRUE • Consider the definite integral $\int_0^1 e^{-x^2} \, dx$. Then $E^L_N(f) < E^M_N(f) < E^R_N(f)$ for any $N$. FALSE The following Python code computes an approximation of what exact value? In [24]: import numpy as np x = np.linspace(0,1,1001) y = np.exp(-x) I = np.sum(y[1:])*0.001 print(I) 0.6318045512258558 In [25]: 1 - 1/np.exp(1) Out[25]: 0.6321205588285577 In [26]: import numpy as np x = np.linspace(0,1,10001) y = np.exp(x) I = np.sum(y[1:])*0.0001 print(I) 1.7183677439823697 In [27]: np.exp(1) - 1 Out[27]: 1.718281828459045 In [28]: import numpy as np x = np.linspace(0,np.pi,10001) y = 1 - np.cos(x) I = np.pi*np.sum(y[1:])/10000 print(I) 3.141906812855152 In [29]: import numpy as np x = np.linspace(0,np.pi,10001) y = 1 + np.sin(x) I = np.pi*np.sum(y[1:])/10000 print(I) 5.141592637140453 In [30]: 2 + np.pi Out[30]: 5.141592653589793 Trapezoid rule I¶ • If $f''(x) \geq 0$ for all $x \in [a,b]$ then $T_N(f) \geq \int_a^b f(x) \, dx$. TRUE • If $f''(x) \geq 0$ for all $x \in [a,b]$ then $T_N(f) \leq \int_a^b f(x) \, dx$. FALSE Trapezoid rule II¶ • If $f''(x) \leq 0$ for all $x \in [a,b]$ then $T_N(f) \geq \int_a^b f(x) \, dx$. FALSE • If $f''(x) \leq 0$ for all $x \in [a,b]$ then $T_N(f) \leq \int_a^b f(x) \, dx$. TRUE Trapezoid rule III¶ • If $R_N(f) = 1.2$ and $L_N(f) = 1.3$ then $T_N(f) = (R_N(f) + L_N(f))/2 = 1.25$ Trapezoid rule IV¶ In [31]: x = np.linspace(0,5,200) y = np.arctan(1 + x**2) dy = 2*x/((x**2 + 1)**2 + 1) d2y = -8*x**2*(x**2 + 1)/((x**2 + 1)**2 + 1)**2 + 2/((x**2 + 1)**2 + 1) plt.figure(figsize=(15,4)) plt.subplot(1,3,1) plt.plot(x,y), plt.grid(True), plt.xlim([0,5]), plt.title('$f(x)$') plt.subplot(1,3,2) plt.plot(x,dy), plt.grid(True), plt.xlim([0,5]), plt.title("$f'(x)$") plt.subplot(1,3,3) plt.plot(x,d2y), plt.grid(True), plt.xlim([0,5]), plt.title("$f''(x)$") plt.show()  In [32]: ((5 - 0)**3/(12*0.001))**0.5 Out[32]: 102.06207261596575 Error formulas¶ • Consider the definite integral $\int_a^b f(x) \, dx$. For any $N$ there exists a $N_1$ such that $E^R_{N_1}(f) < E^T_{N}(f)$. TRUE • If $N_1 < N_2$ then $E^R_{N_2}(f) < E^R_{N_1}(f)$. FALSE Functions¶ Write a Python function called p_integral which takes input parameters p and N and returns the approximation of the integral $$ \int_0^1 \frac{1}{\sqrt{1 + x^p}} dx $$ using the right Riemann sum with $N$ subintervals. Do not import NumPy or any other Python package. Use only builtin Python functions. In [33]: def p_integral(p,N): dx = 1/N x = [n*dx for n in range(1,N+1)] y = [1/(1 + x[n]**p)**0.5 for n in range(0,len(x))] I = sum(y)*dx return I In [34]: p_integral(1,1000) Out[34]: 0.8282807050720556 In [35]: 2*(1 + 1)**0.5 - 2*(1 + 0)**0.5 Out[35]: 0.8284271247461903 Write a Python function called p_integral which takes input parameters p and N and returns the approximation of the integral $$ \int_0^1 x \sqrt{1 + x^p} dx $$ using the right Riemann sum with $N$ subintervals. Do not import NumPy or any other Python package. Use only builtin Python functions. In [36]: def p_integral(p,N): dx = 1/N x = [n*dx for n in range(1,N+1)] y = [x[n]*(1 + x[n]**p)**0.5 for n in range(0,len(x))] I = sum(y)*dx return I In [37]: p_integral(2,2000) Out[37]: 0.6098292850001642 In [38]: 1/3*(1 + 1**2)**1.5 - 1/3*(1 + 0**2)**1.5 Out[38]: 0.6094757082487301 Error formulas II¶ Problem I¶ Consider the definite integral $$ \int_1^{e^{\pi/2}} \sin(\ln(x)) \, dx $$ Without plotting the function or its derivatives, find an integer $N$ which gurantees $E_N^R(f) < 0.01$. Show all your work. Solution. The error formula is $$ E^R_N(f) = \left| \ \int_a^b f(x) \, dx - R_N(f) \ \right| \leq \frac{(b - a)^2}{2 N} K_1 $$ Find a bound $K_1$. Compute the derivative of $f(x) = \sin(\ln(x))$ $$ f'(x) = \frac{\cos(\ln(x))}{x} $$ Consider the numerator and denominator of $f'(x)$ separately. We know $\ln(x)$ is increasing on $[1,e^{\pi/2}]$ from $\ln(1) = 0$ to $\ln(e^{\pi/2}) = \pi/2$. Since $\cos(x)$ is decreasing on $[0,\pi/2]$, we see that $\cos(\ln(x))$ is decreasing on $[1,e^{\pi/2}]$ from 1 to 0. This means that $\cos(\ln(x)) \leq 1$ for all $x \in [1,e^{\pi/2}]$. Clearly, the denominator $x$ is increasing on $[1,e^{\pi/2}]$. Altogether we have $$ | f'(x) | = \frac{\cos(\ln(x))}{x} \leq \frac{1}{x} \leq 1 $$ for all $x \in [1,e^{\pi/2}]$. Alternatively, compute $$ f''(x) = \frac{-(\sin(\ln(x)) + \cos(\ln(x)))}{x^2} $$ Since $0 \leq \ln(x) \leq \pi/2$ for all $x \in [1,e^{\pi/2}]$ we see that $\sin(\ln(x)) \geq 0$ and $\cos(\ln(x)) \geq 0$ for all $x \in [1,e^{\pi/2}]$, and clearly $x^2 > 0$. Therefore $f”(x) \leq 0$ on $[1,e^{\pi/2}]$ and so $f'(x)$ is decreasing. Check the endpoints $f'(1) = 1$ and $f'(e^{\pi/2}) = 0$ and conclude $| f'(x) | \leq 1$ for all $x \in [1,e^{\pi/2}]$.
The error fomrula yields
$$ E^R_N(f) \leq \frac{(1 – 0)^2}{2 N} (1) < 0.01 \ \ \Rightarrow \ \ N > 50 \ \ \Rightarrow \ \ N \geq 51 $$

Problem II¶
Consider the definite integral
$$ \int_0^{\pi/2} \ln(2 + \cos(x)) \, dx $$
Without plotting the function or its derivatives, find an integer $N$ which gurantees $E_N^R(f) < 0.01$. Show all your work. Solution. The error formula is $$ E^R_N(f) = \left| \ \int_a^b f(x) \, dx - R_N(f) \ \right| \leq \frac{(b - a)^2}{2 N} K_1 $$ Find a bound $K_1$. Compute the derivative of $f(x) = \ln(2 + \cos(x))$ $$ f'(x) = -\frac{\sin(x)}{2 + \cos(x)} $$ Consider the numerator and denominator of $f'(x)$ separately. We know $\sin(x)$ is increasing on $[0,\pi/2]$ from 0 to 1. The denominator $2 + \cos(x)$ is decreasing on $[0,\pi/2]$ from 3 to 2. Altogether we have $$ | f'(x) | = \frac{\sin(x)}{2 + \cos(x)} \leq \frac{1}{2 + \cos(x)} \leq \frac{1}{2} $$ for all $x \in [0,\pi/2]$. Alternatively, compute $$ f''(x) = -\frac{(2 + \cos(x))\cos(x) + \sin^2(x)}{(2 + \cos(x))^2} = -\frac{2\cos(x) + 1}{(2 + \cos(x))^2} $$ Since $0 \leq \cos(x) \leq 1$ for all $x \in [0,\pi/2]$ we see that $2\cos(x) + 1 > 0$ for all $x \in [0,\pi/2]$, and clearly $(2 + \cos(x))^2 > 0$. Therefore $f”(x) \leq 0$ on $[0,\pi/2]$ and so $f'(x)$ is decreasing. Check the endpoints $f'(0) = 0$ and $f'(\pi/2) = 1/2$ and conclude $| f'(x) | \leq 1/2$ for all $x \in [0,\pi/2]$.
The error fomrula yields
$$ E^R_N(f) \leq \frac{(\pi/2 – 0)^2}{2 N} (1/2) < 0.01 \ \ \Rightarrow \ \ N > \frac{25\pi^2}{4} \ \ \Rightarrow \ \ N \geq 62 $$

Problem III¶
Consider the definite integral
$$ \int_1^2 \sin(\sqrt{x}) dx $$
Without plotting the function or its derivatives, find an integer $N$ which gurantees $E_N^R(f) < 0.01$. Show all your work. Solution. The error formula is $$ E^R_N(f) = \left| \ \int_a^b f(x) \, dx - R_N(f) \ \right| \leq \frac{(b - a)^2}{2 N} K_1 $$ Find a bound $K_1$. Compute the derivative of $f(x) = \sin(\sqrt{x})$ $$ f'(x) = \frac{\cos(\sqrt{x})}{2\sqrt{x}} $$ Consider the numerator and denominator of $f'(x)$ separately. Since $\sqrt{2} < \pi/2$ we know $\cos(\sqrt{x})$ is decreasing on $[1,2]$. The denominator $2\sqrt{x}$ is clearly increasing. Altogether we have $$ | f'(x) | = \frac{\cos(\sqrt{x})}{2\sqrt{x}} \leq \frac{\cos(1)}{2\sqrt{x}} \leq \frac{\cos(1)}{2} $$ for all $x \in [1,2]$. Alternatively, compute $$ f''(x) = \frac{-\sin(\sqrt{x}) - \cos(\sqrt{x})/\sqrt{x}}{4x} $$ Since $\sin(\sqrt{x}) > 0$ and $\cos(\sqrt{x}) > 0$ for all $x \in [1,2]$ we see that $f”(x) \leq 0$ on $[1,2]$ and so $f'(x)$ is decreasing. Check the endpoints $f'(1) = \cos(1)/2$ and $f'(2) = \cos(\sqrt{2})/2\sqrt{2}$ and conclude $| f'(x) | \leq \cos(1)/2$ for all $x \in [1,2]$.
The error fomrula yields
$$ E^R_N(f) \leq \frac{(2 – 1)^2}{2 N} \frac{\cos(1)}{2} < 0.01 \ \ \Rightarrow \ \ N > 25\cos(1) \ \ \Rightarrow \ \ N \geq 14 $$