Lab Sheet 10¶

In [1]:
from header import *

Exercise 1.1

Here we study the Taylor series for $y=\ln\left(\sqrt{\frac{1+x}{1-x}}\right)$

In [3]:
x = sp.symbols('x', real=True)
y = sp.log(sp.sqrt((1+x)/(1-x)))
display(Latex("$y=" + sp.latex(y) + "$"))
$y=\log{\left(\sqrt{\frac{x + 1}{1 - x}} \right)}$

The cell below shows that the coefficient of $x^5$ in the Taylor series is $a_5=1/5$.

In [39]:
y5 = sp.factor(sp.diff(y,x,5))
y50 = y5.subs({x : 0})
a5 = y50 / sp.factorial(5)
display(Latex(f"$y = {sp.latex(y)}$"))
display(Latex("$\\frac{d^5y}{dx^5}=" + sp.latex(y5) + "$"))
display(Latex("$\\left.\\frac{d^5y}{dx^5}\\right|_{x=0}=" + sp.latex(y50) + "$"))
display(Latex("$a_5 = " + sp.latex(a5) + "$"))
$y = \log{\left(\sqrt{\frac{x + 1}{1 - x}} \right)}$
$\frac{d^5y}{dx^5}=- \frac{24 \cdot \left(5 x^{4} + 10 x^{2} + 1\right)}{\left(x - 1\right)^{5} \left(x + 1\right)^{5}}$
$\left.\frac{d^5y}{dx^5}\right|_{x=0}=24$
$a_5 = \frac{1}{5}$
In [40]:
a = lambda n : sp.diff(y,x,n).subs({x : 0}) / sp.factorial(n)

The cell below calculates the coefficients $a_0,\dotsc,a_9$. We see that $a_n=0$ when $n$ is even, and $a_n=1/n$ when $n$ is odd.

In [41]:
[a(n) for n in range(10)]
Out[41]:
[0, 1, 0, 1/3, 0, 1/5, 0, 1/7, 0, 1/9]

We set y_approx0 to be the Taylor series based on the coefficients as calculated above, and y_approx1 to be the Taylor series calculated using sp.series(). As expected, they are the same.

In [42]:
y_approx0 = sp.Add(*[a(n) * x ** n for n in range(10)])
y_approx1 = sp.series(y, x, 0, 10).removeO() 
display(y_approx0)
display(y_approx1)
$\displaystyle \frac{x^{9}}{9} + \frac{x^{7}}{7} + \frac{x^{5}}{5} + \frac{x^{3}}{3} + x$
$\displaystyle \frac{x^{9}}{9} + \frac{x^{7}}{7} + \frac{x^{5}}{5} + \frac{x^{3}}{3} + x$
In [5]:
n = sp.symbols('n', integer=True)
s = sp.Sum(x **(2*n+1) / (2*n+1), (n, 0, sp.oo)).doit()
s = s.args[0] * s.args[1].args[0][0]
display(s)
display(sp.simplify(s - y))
$\displaystyle \operatorname{atanh}{\left(x \right)}$
$\displaystyle - \frac{\log{\left(- \frac{x + 1}{x - 1} \right)}}{2} + \operatorname{atanh}{\left(x \right)}$
In [44]:
del x, y, y5, y50, a5, a, y_approx0, y_approx1, n, s

Exercise 1.2

In [2]:
x = sp.symbols('x', real=True)
s = sp.series(sp.sin(x),x,0,12).removeO()
s_fun = sp.lambdify(x, s, 'numpy')
s
Out[2]:
$\displaystyle - \frac{x^{11}}{39916800} + \frac{x^{9}}{362880} - \frac{x^{7}}{5040} + \frac{x^{5}}{120} - \frac{x^{3}}{6} + x$

Here we plot $\sin(x)$ along with its 12th order Taylor approximation. The graphs are similar when $|x|\leq 5$ or so, but they diverge quickly outside of that range.

In [5]:
xs = np.linspace(-8,8,400)
fig = go.Figure()
fig.update_layout(width=1000, height=500, yaxis_range=[-10,10])
use_pi_ticks(fig, -2,2)
move_legend(fig,0.9)
fig.add_trace(go.Scatter(x=xs, y=np.sin(xs), mode='lines', name=r'$\sin(x)$'))
fig.add_trace(go.Scatter(x=xs, y=s_fun(xs), mode='lines', name=r'Taylor series'))
In [6]:
def T(n):
    return sp.series(sp.sin(x),x,0,n).removeO()

def T_fun(n):
    return sp.lambdify(x, T(n), 'numpy')

def show_T(n, R, L=10):
    xs = np.linspace(-R,R,400)
    fig = go.Figure()
    fig.update_layout(width=1000, height=500, yaxis_range=[-L,L])
    use_pi_ticks(fig, x0=-R,x1=R)
    fig.add_trace(go.Scatter(x=xs, y=np.sin(xs), mode='lines', name=r'$\sin(x)$'))
    fig.add_trace(go.Scatter(x=xs, y=T_fun(n)(xs), mode='lines', name='Taylor'))
    fig.show()
In [9]:
show_T(20,10,3)
In [14]:
xs = np.linspace(0,20,400)
fig = go.Figure()
fig.update_layout(width=1000, height=500, yaxis_range=[-2,10], showlegend=False)
use_pi_ticks(fig, 0, 6)
fig.add_trace(go.Scatter(x=xs, y=np.sin(xs), mode='lines', name=r'$\sin(x)$'))
for n in range(2,48,4):
    fig.add_trace(go.Scatter(x=xs, y=T_fun(n)(xs), mode='lines', line_dash='dot', name=f'Taylor {n}'))
fig.show()
In [15]:
xs = np.linspace(0,20,400)
fig = go.Figure()
fig.update_layout(width=1000, height=500, yaxis_range=[-10,2], showlegend=False)
use_pi_ticks(fig, 0, 6)
fig.add_trace(go.Scatter(x=xs, y=np.sin(xs), mode='lines', name=r'$\sin(x)$'))
for n in range(4,48,4):
    fig.add_trace(go.Scatter(x=xs, y=T_fun(n)(xs), mode='lines', line_dash='dot', name=f'Taylor {n}'))
fig.show()
In [17]:
def R(n):
    return sp.series(sp.cos(x),x,0,n).removeO()

def R_fun(n):
    return sp.lambdify(x, R(n), 'numpy')
In [18]:
sp.expand(T(10)**2 + R(10)**2)
Out[18]:
$\displaystyle \frac{x^{18}}{131681894400} - \frac{x^{16}}{2090188800} + \frac{x^{14}}{60963840} - \frac{x^{12}}{4354560} + \frac{x^{10}}{1814400} + 1$
In [19]:
xs = np.linspace(-5,5,200)
err = 1 - T_fun(10)(xs)**2 - R_fun(10)(xs)**2
fig = go.Figure()
fig.update_layout(width=1000, height=500, yaxis_range=[-1,1], showlegend=False)
fig.add_trace(go.Scatter(x=xs, y=err, mode='lines', name='Error'))
In [76]:
del x, s, s_fun, fig, ax, xs, T, T_fun, show_T, R, R_fun, err

Exercise 1.3

Here we consider the Taylor series for $y=\frac{x(1+x)}{(1-x)^3}$.

In [9]:
x = sp.symbols('x', real=True)
y = x*(1+x)/(1-x)**3
y_series = sp.series(y,x,0,11)
display(Latex("$y=" + sp.latex(y) + "\\approx " + sp.latex(y_series) + "$"))
$y=\frac{x \left(x + 1\right)}{\left(1 - x\right)^{3}}\approx x + 4 x^{2} + 9 x^{3} + 16 x^{4} + 25 x^{5} + 36 x^{6} + 49 x^{7} + 64 x^{8} + 81 x^{9} + 100 x^{10} + O\left(x^{11}\right)$

It seems clear from the above that $y=\sum_{n=1}^\infty n^2x^n$ (at least when $|x|<1$). We can check this as follows.

In [12]:
n = sp.symbols('n', integer=True)
y1 = sp.Sum(n**2*x**n, (n, 0, sp.oo)).doit()
display(y1)
y2 = y1.args[0][0]
display(y2)
display(sp.simplify(y - y2))
$\displaystyle \begin{cases} \frac{x \left(- x - 1\right)}{\left(x - 1\right)^{3}} & \text{for}\: \left|{x}\right| < 1 \\\sum_{n=0}^{\infty} n^{2} x^{n} & \text{otherwise} \end{cases}$
$\displaystyle \frac{x \left(- x - 1\right)}{\left(x - 1\right)^{3}}$
$\displaystyle 0$
In [13]:
del x, y, y_series, n, y1, y2

Exercise 1.4

Here we have the 12th order Taylor series of $\sin(x)$ around $x=\pi/2$. Because $\sin(\pi/2+t)=\cos(t)$, this has the same coefficients as the Taylor series of $\cos(t)$ around $t=0$.

In [87]:
x = sp.symbols('x', real=True)
f_series = sp.series(sp.sin(x),x,sp.pi/2,12)
display(f_series)
g_series = sp.series(sp.cos(x),x,0,12)
display(g_series)
g_series.removeO().subs({x : x - sp.pi/2}) - f_series.removeO()
$\displaystyle 1 - \frac{\left(x - \frac{\pi}{2}\right)^{2}}{2} + \frac{\left(x - \frac{\pi}{2}\right)^{4}}{24} - \frac{\left(x - \frac{\pi}{2}\right)^{6}}{720} + \frac{\left(x - \frac{\pi}{2}\right)^{8}}{40320} - \frac{\left(x - \frac{\pi}{2}\right)^{10}}{3628800} + O\left(\left(x - \frac{\pi}{2}\right)^{12}; x\rightarrow \frac{\pi}{2}\right)$
$\displaystyle 1 - \frac{x^{2}}{2} + \frac{x^{4}}{24} - \frac{x^{6}}{720} + \frac{x^{8}}{40320} - \frac{x^{10}}{3628800} + O\left(x^{12}\right)$
Out[87]:
$\displaystyle 0$
In [88]:
del x, f_series, g_series

Exercise 2.1

Here we evaluate $\cos(\ln(\pi+20))$ to 20 decimal places. (It is very small, but that is just a coincidence.)

In [15]:
mpmath.mp.dps = 20
print(1 + mpmath.cos(mpmath.log(mpmath.pi + 20)))
7.5631986690063713859e-10

Exercise 2.2

Here we calculate $f(f(f(-1/4)))$, where $f(x)=x^2-29/16$. We find that the answer is just $-1/4$ again (which is an interesting example in the theory of the Mandelbrot set).

In [16]:
x = sp.symbols('x')
f = lambda x: x*x - sp.Rational(29,16)
In [17]:
x0 = sp.Rational(-1,4)
x1 = f(x0)
x2 = f(x1)
x3 = f(x2)
print([x0,x1,x2,x3])
[-1/4, -7/4, 5/4, -1/4]
In [94]:
del x, f, x0, x1, x2, x3

Exercise 2.3

In [ ]:
x = sp.symbols('x')
a = (1+x)**5-3*(1+x)**4+5*(1+x)**3-3*(1+x)**2+3*(1+x)+3
display(Latex("$a=" + sp.latex(a) + "$"))
b = (7*x**2-6*x-x**8)/(x-1)**2
display(b)
$\displaystyle 3 x + \left(x + 1\right)^{5} - 3 \left(x + 1\right)^{4} + 5 \left(x + 1\right)^{3} - 3 \left(x + 1\right)^{2} + 6$
$\displaystyle \frac{- x^{8} + 7 x^{2} - 6 x}{\left(x - 1\right)^{2}}$
In [100]:
display(sp.simplify(a))
display(sp.factor(b))
display(sp.simplify(a*x + b))
$\displaystyle x^{5} + 2 x^{4} + 3 x^{3} + 4 x^{2} + 5 x + 6$
$\displaystyle - x \left(x^{5} + 2 x^{4} + 3 x^{3} + 4 x^{2} + 5 x + 6\right)$
$\displaystyle 0$
In [101]:
del x, a, b

Exercise 2.4

In [102]:
x = sp.symbols('x')
y = ((x**12-1)*(x**2-1)/((x**6-1)*(x**4-1)))**(10)
display(y)
$\displaystyle \frac{\left(x^{2} - 1\right)^{10} \left(x^{12} - 1\right)^{10}}{\left(x^{4} - 1\right)^{10} \left(x^{6} - 1\right)^{10}}$
In [105]:
display(sp.factor(y))
$\displaystyle \left(x^{4} - x^{2} + 1\right)^{10}$
In [107]:
sp.expand(sp.factor(y)).coeff(x,6)
Out[107]:
$\displaystyle -210$
In [108]:
del x, y

Exercise 2.5

In [110]:
x, y, z = sp.symbols('x y z')

sp.solve([
 sp.Eq(x**2 + y**2 + z**2, 9),
 sp.Eq((x-1)**2 + (y-1)**2 + (z-1)**2,2),
 sp.Eq(4*x**2+y*z,2*x*y+2*x*z)
],[x,y,z])
Out[110]:
[(1, 2, 2), (8/7, 11/7, 16/7), (8/7, 16/7, 11/7)]
In [111]:
del x, y, z

Exercise 2.6

In [22]:
theta = sp.symbols('theta')
f = sp.sin(theta)**2-3*sp.cos(theta)**2
display(sp.solveset(f))
f_fun = sp.lambdify(theta, f, 'numpy')
theta = np.linspace(-2*np.pi,2*np.pi,400)
fig = go.Figure()
fig.update_layout(width=1000, height=500, yaxis_range=[-3.5,1.5])
use_pi_ticks(fig, -2,2)
fig.add_trace(go.Scatter(x=theta, y=f_fun(theta), mode='lines', name=r'$\sin^2(\theta)-3\cos^2(\theta)$'))
$\displaystyle \left\{2 n \pi + \frac{4 \pi}{3}\; \middle|\; n \in \mathbb{Z}\right\} \cup \left\{2 n \pi + \frac{2 \pi}{3}\; \middle|\; n \in \mathbb{Z}\right\} \cup \left\{2 n \pi + \frac{5 \pi}{3}\; \middle|\; n \in \mathbb{Z}\right\} \cup \left\{2 n \pi + \frac{\pi}{3}\; \middle|\; n \in \mathbb{Z}\right\}$
In [116]:
del theta, f, f_fun

Exercise 2.7

In [127]:
x, y = sp.symbols('x y', real=True, positive=True)
x0, y0 = sp.solve([x**2-y**2-1,2*x*y-1],[x,y])[0]
x0 = sp.simplify(x0)
y0 = sp.simplify(y0)
display(x0)
display(y0)
$\displaystyle \frac{\sqrt{-1 + \sqrt{2}} \left(\sqrt{2} + 2\right)}{2}$
$\displaystyle \frac{\sqrt{-2 + 2 \sqrt{2}}}{2}$
In [128]:
del x, y, x0, y0

Exercise 2.8

In [129]:
sc.optimize.root_scalar(lambda x: x - np.log(x+20),x0=3)
Out[129]:
      converged: True
           flag: converged
 function_calls: 6
     iterations: 3
           root: 3.141633302801037
In [19]:
x = [1,2,3]
y = x.copy()
y.append(99)
print(x)
[1, 2, 3]
In [20]:
x.insert(0,999)
print(x)
[999, 1, 2, 3]
In [ ]: