MATH 210 Quiz 1¶
In [1]:
import numpy as np
Jupyter notebooks¶
• There are several types of cells in a Jupyter notebook. Classify which actions are executed in the corresponding types of cell.
▪ Code cell
◦ Write Python code
▪ Markdown cell
◦ Write LaTeX code
◦ Write markdown code
◦ Write HTML code
◦ Write plain text
• MATH 210 students must download Python and Jupyter software to their own computers to use Syzygy.
Answer: FALSE
• There are several kinds of modes when working in a Jupyter notebook including:
▪ Edit mode
▪ Command mode
▪ Jupyter mode
▪ Python mode
▪ LaTex mode
▪ Markdown mode
Markdown¶
Write markdown code to display the list of lists:
1. Calculus
• Integration
• Differentiation
2. Linear Algebra
• Determinant
• Cross product
3. Differential Equations
• First order equations
• Laplace transforms
Write markdown code to display the table:
Province
Capital
Alberta
Edmonton
Manitoba
Winnipeg
Nova Scotia
Halifax
Write markdown code to display the link CBC.
LaTeX¶
Write LaTeX code to display the formula
$$ \int x e^{x^2} dx = \frac{e^{x^2}}{2} + C $$$$ \int \frac{2x}{1 + x^2} dx = \ln(1 + x^2) + C $$$$ \arctan(x) = \sum_{n = 0}^{\infty} \frac{(-1)^n x^{2n + 1}}{2n + 1} $$$$ \sum_{k = 0}^N r^k = \frac{1 – r^{N+1}}{1 – r} $$$$ \lim_{x \to 0} \frac{\sin(x)}{x} = 1 $$$$ \lim_{x \to \infty} \arctan(x) = \frac{\pi}{2} $$$$ \sin(\alpha + \beta) = \sin(\alpha) \cos(\beta) + \sin(\beta) \cos(\alpha) $$
Variables¶
• Python does not allow assigning a value to variable name which is already a builtin Python function such as sum, min, max.
Answer: FALSE
• The Python code 2legit2quit = 222 will generate an error.
Answer: TRUE
Numeric types I¶
Suppose x and y are Python variables.
• If type(x) is int and type(y) is int, then type(x + y) is:
▪ int CORRECT
▪ float
▪ complex
In [20]:
x = 1; y = 1
In [21]:
type(x)
Out[21]:
int
In [22]:
type(y)
Out[22]:
int
In [23]:
type(x + y)
Out[23]:
int
• If type(x) is int and type(y) is int, then type(x * y) is:
▪ int CORRECT
▪ float
▪ complex
In [24]:
x = 1; y = 1;
In [25]:
type(x)
Out[25]:
int
In [26]:
type(y)
Out[26]:
int
In [27]:
type(x * y)
Out[27]:
int
• If type(x) is int and type(y) is int, then type(x / y) is:
▪ int
▪ float CORRECT
▪ complex
In [28]:
x = 1; y = 1;
In [29]:
type(x)
Out[29]:
int
In [30]:
type(y)
Out[30]:
int
In [31]:
type(x / y)
Out[31]:
float
• If type(x) is int and type(y) is float, then type(x + y) is:
▪ int
▪ float CORRECT
▪ complex
In [32]:
x = 1; y = 2.0;
In [33]:
type(x)
Out[33]:
int
In [34]:
type(y)
Out[34]:
float
In [35]:
type(x + y)
Out[35]:
float
• If type(x) is int and type(y) is float, then type(x * y) is:
▪ int
▪ float CORRECT
▪ complex
In [36]:
x = 1; y = 2.0;
In [37]:
type(x)
Out[37]:
int
In [38]:
type(y)
Out[38]:
float
In [39]:
type(x * y)
Out[39]:
float
• If type(x) is int and type(y) is float, then type(x / y) is:
▪ int
▪ float CORRECT
▪ complex
In [40]:
x = 1; y = 2.0;
In [41]:
type(x)
Out[41]:
int
In [42]:
type(y)
Out[42]:
float
In [43]:
type(x / y)
Out[43]:
float
Numeric types II¶
• If type(x) is int and type(y) is int, then type(x ** y) is:
▪ always int
▪ always float
▪ always complex
▪ either int or float CORRECT
▪ either float or complex
In [44]:
x = 1; y = 1;
In [45]:
type(x)
Out[45]:
int
In [46]:
type(y)
Out[46]:
int
In [47]:
type(x ** y)
Out[47]:
int
• If type(x) is int and type(y) is float, then type(x ** y) is:
▪ always int
▪ always float
▪ always complex
▪ either int or float
▪ either float or complex CORRECT
In [56]:
x = -1; y = 2.1;
In [57]:
type(x)
Out[57]:
int
In [58]:
type(y)
Out[58]:
float
In [59]:
type(x ** y)
Out[59]:
complex
• If type(x) is float and type(y) is int, then type(x ** y) is:
▪ always int
▪ always float CORRECT
▪ always complex
▪ either int or float
▪ either float or complex
In [60]:
x = 1.1; y = 2;
In [61]:
type(x)
Out[61]:
float
In [62]:
type(y)
Out[62]:
int
In [63]:
type(x ** y)
Out[63]:
float
Sequence types¶
If sequence = [-10, -7, -4, -1, …, 44, 47, 50, 53, 56] then sequence[11] is ___.
In [2]:
sequence = [-10 + 3*n for n in range(0,23)]
print(sequence)
sequence[11]
[-10, -7, -4, -1, 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 56]
Out[2]:
23
If sequence = [-12, -8, -4, 0, …, 44, 48, 52, 56, 60] then sequence[7] is ___.
In [3]:
sequence = [-12 + 4*n for n in range(0,19)]
print(sequence)
sequence[7]
[-12, -8, -4, 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60]
Out[3]:
16
If sequence = [-9, -5, -1, 3, …, 51, 55, 59, 63] then sequence[13] is ___.
In [4]:
sequence = [-9 + 4*n for n in range(0,19)]
print(sequence)
sequence[13]
[-9, -5, -1, 3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51, 55, 59, 63]
Out[4]:
43
Logic¶
• The Python expression 1.00000000000000000000000000000000000000000000000001 == 1.0 evaluates to
1. int
2. float
3. True CORRECT
4. False
5. bool
6. Error
7. None
In [70]:
1.00000000000000000000000000000000000000000000000001 == 1.0
Out[70]:
True
Bisection method¶
Let $f(x)$ be a continuous function such that $f(a)f(b) < 0$ where $|b - a| = I$. Determine the minimum number of iterations $N$ of the bisection method required to guarantee that the midpoint $x_N$ of the $N$th subinterval satisfies $|x_N - c| < E$ where $c$ is a solution $f(c) = 0$.
In [65]:
err = 0.0001
interval = 0.5
N = np.log(interval/err)/np.log(2) - 1
N = np.ceil(N)
N
Out[65]:
12.0
In [66]:
err = 0.001
interval = 0.2
N = np.log(interval/err)/np.log(2) - 1
N = np.ceil(N)
N
Out[66]:
7.0
In [67]:
err = 0.0001
interval = 1.0
N = np.log(interval/err)/np.log(2) - 1
N = np.ceil(N)
N
Out[67]:
13.0
In [69]:
err = 0.0001
interval = 0.25
N = np.log(interval/err)/np.log(2) - 1
N = np.ceil(N)
N
Out[69]:
11.0
Newton's Method I¶
• Let $f(x)$ be a differentiable function and let $c$ be a number such that $f(c) = 0$. If $x_0$ and $y_0$ are numbers such that $| x_0 - c| < | y_0 - c|$ and if $x_1$ and $y_1$ are the approximations of $c$ given by one iteration of Newton's method starting with $x_0$ and $y_0$ respectively, then $| x_1 - c| < | y_1 - c|$. In other words, if $x_0$ is closer to $c$ than $y_0$, then $x_1$ is closer to $c$ than $y_1$.
Answer: FALSE
Newton's Method II¶
Let $f(x) = x^7 + x^2 - 1$.
• Find intervals where $f(x)$ is increasing/decreasing.
• Use part (a) to prove that $f(x) = 0$ has exactly one solution.
• Find an integer $n$ such that the solution of $f(x) = 0$ lies in the interval $[n-1,n]$. Justify your answer.
• Implement 1 iteration of Newton's method with $x_0 = n$ to approximate the solution of $f(x) = 0$.
• If $c$ is the exact solution $f(c) = 0$, determine whether $c < x_1$ or $c > x_1$. Justify your answer.
• Find a starting value $x_0$ such that Newton’s method fails for this equation.
Solution. Compute $f'(x) = 7x^6 + 2x$, set $f'(x) = x(7x^5 + 2) = 0$ and we find critical points $x=-(2/7)^{1/5}$ and $x=0$. Clearly $f'(x) > 0$ for $x < -(2/7)^{1/5}$ and $x > 0$ therefore $f(x)$ is increasing on $(-\infty,-(2/7)^{1/5})$ and $(0,\infty)$, and decreasing on $(-(2/7)^{1/5},0)$.
Use a calculator (or Python) to compute $f(-(2/7)^{1/5})$:
In [4]:
x = -(2/7)**(1/5)
y = x**7 + x**2 – 1
print(y)
-0.5672423571752407
Since $f(x)$ is increasing on $(-\infty,-(2/7)^{1/5})$, $f(-(2/7)^{1/5}) < 0$ and $f(x)$ is decreasing on $(-(2/7)^{1/5},0)$, we conclude that $f(x) < 0$ for all $x < 0$. Since $f(x)$ is increasing on $(0,\infty)$ there is at most one value $c > 0$ where $f(c) = 0$.
Plug in $f(0) = -1$ and $f(1) = 1$ therefore there exists $c \in [0,1]$ such that $f(c) = 0$ by the Intermediat Value Theorem. And so $n=1$.
$$ x_1 = x_0 – \frac{f(x_0)}{f'(x_0)} = 1 – \frac{1}{9} = \frac{8}{9} $$
Compute $f”(x) = 42x^5 + 2$ and we see that $f(x)$ is concave up for all $x > 0$. Therefore the linear approximation at $x_0 = 1$ is an underestimate of $f(x)$ and so $c < x_1$.
Newton's method fails when $f'(x_0) = 0$ therefore $x_0 = 0$ is an example.
Let $f(x) = x^5 - x + 1$.
• Find intervals where $f(x)$ is increasing/decreasing.
• Use part (a) to prove that $f(x) = 0$ has exactly one solution.
• Find an integer $n$ such that the solution of $f(x) = 0$ lies in the interval $[n - 1,n]$. Justify your answer.
• Implement 1 iteration of Newton's method with $x_0 = n$ to approximate the solution of $f(x) = 0$.
• If $c$ is the exact solution $f(c) = 0$, determine whether $c < x_1$ or $c > x_1$. Justify your answer.
• Find a starting value $x_0$ such that Newton’s method fails for this equation.
Solution. Compute $f'(x) = 5x^4 – 1$, set $f'(x) = 5x^4 – 1 = 0$ and we find critical points $x=-(1/5)^{1/4}$ and $x=(1/5)^{1/4}$. Clearly $f'(x) < 0$ for $-(1/5)^{1/4} < x < (1/5)^{1/4}$ therefore $f(x)$ is increasing on $(-\infty,-(1/5)^{1/4})$ and $((1/5)^{1/4},\infty)$, and decreasing on $(-(1/5)^{1/4},(1/5)^{1/4})$.
Use a calculator (or Python) to compute $f(-(1/5)^{1/4})$ and $f((1/5)^{1/4})$:
In [6]:
x1 = -(1/5)**(1/4)
y1 = x1**5 - x1 + 1
print(y1)
x2 = (1/5)**(1/4)
y2 = x2**5 - x2 + 1
print(y2)
1.5349922439811376
0.46500775601886235
Since $f(x)$ is decreasing on $(-(1/5)^{1/4},(1/5)^{1/4})$, $f((1/5)^{1/4}) > 0$ and $f(x)$ is increasing on $(-(1/5)^{1/4},\infty)$, we conclude that $f(x) > 0$ for all $x > -(1/5)^{1/4}$. Since $f(x)$ is increasing on $(-\infty,-(1/5)^{1/4})$ there is at most one value $c < -(1/5)^{1/4}$ where $f(c) = 0$.
Plug in $f(-2) = -29$ and $f(-1) = 1$ therefore there exists $c \in [-2,-1]$ such that $f(c) = 0$ by the Intermediat Value Theorem. And so $n=-1$.
$$ x_1 = x_0 - \frac{f(x_0)}{f'(x_0)} = -1 - \frac{1}{4} = -\frac{5}{4} $$
Compute $f''(x) = 20x^3$ and we see that $f(x)$ is concave down for all $x < 0$. Therefore the linear approximation at $x_0 = -1$ is an overestimate of $f(x)$ and so $c > x_1$.
Newton’s method fails when $f'(x_0) = 0$ therefore $x_0 = -(1/5)^{1/4}$ is an example.
Let $f(x) = x^5 – x^2 + 1$.
• Find intervals where $f(x)$ is increasing/decreasing.
• Use part (a) to prove that $f(x) = 0$ has exactly one solution.
• Find an integer $n$ such that the solution of $f(x) = 0$ lies in the interval $[n,n + 1]$. Justify your answer.
• Implement 1 iteration of Newton’s method with $x_0 = n$ to approximate the solution of $f(x) = 0$.
• If $c$ is the exact solution $f(c) = 0$, determine whether $c < x_1$ or $c > x_1$. Justify your answer.
• Find a starting value $x_0$ such that Newton’s method fails for this equation.
Solution. Compute $f'(x) = 5x^4 – 2x$, set $f'(x) = x(5x^3 – 2) = 0$ and we find critical points $x=0$ and $x=(2/5)^{1/3}$. Clearly $f'(x) < 0$ for $0 < x < (2/5)^{1/3}$ therefore $f(x)$ is increasing on $(-\infty,0)$ and $(2/5)^{1/3},\infty)$, and decreasing on $(0,2/5)^{1/3})$. Use a calculator (or Python) to compute $f(2/5)^{1/3})$: In [11]: x = (2/5)**(1/3) y = x**5 - x**2 + 1 print(y) 0.6742698860086113 Since $f(x)$ is decreasing on $(0,(2/5)^{1/3})$, $f((2/5)^{1/3}) > 0$ and $f(x)$ is increasing on $((2/5)^{1/3},\infty)$, we conclude that $f(x) > 0$ for all $x > 0$. Since $f(x)$ is increasing on $(-\infty,0)$ there is at most one value $c < 0$ where $f(c) = 0$. Plug in $f(-1) = -1$ and $f(0) = 1$ therefore there exists $c \in [-1,0]$ such that $f(c) = 0$ by the Intermediat Value Theorem. And so $n=-1$. $$ x_1 = x_0 - \frac{f(x_0)}{f'(x_0)} = -1 - \frac{-1}{7} = -\frac{6}{7} $$ Compute $f''(x) = 20x^3 - 2$ and we see that $f(x)$ is concave down for all $x < 0$. Therefore the linear approximation at $x_0 = -1$ is an overrestimate of $f(x)$ and so $x_1 < c$. Newton's method fails when $f'(x_0) = 0$ therefore $x_0 = 0$ is an example.