Solutions Q 14 - 26
Contents
Solutions Q 14 - 26#
# added here as used later on
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import sympy as sp
sp.init_printing() # allows printing of SymPy results in typeset maths format
plt.rcParams.update({'font.size': 14}) # set font size for plots
Q14 answer#
The series expansion of \(\tan(x)\) is obtained with repeated differentiation and then evaluating each term at \(x = 0\),
and so on. The series is then
and when multiplied by that for \(\displaystyle e^x\) produces,
and multiplying out produces
The calculation is trivial using a computer algebra programme; SymPy is used below;
x = sp.symbols('x') # using SymPy
f01 = sp.tan(x)*sp.exp(x)
s = sp.expand(sp.series( f01, x ))
s
Q15 answer#
Rearranging produces \(\displaystyle x\frac{(1+x)}{(x-1)^2}=x\frac{(1+x)}{(x-1)(x-1)}=x\frac{(1+x)}{(1-x)^2}\)
where the last term is obtained by multiplying top and bottom by \((-1)\) twice. Using the series expansion formula for the denominator and then multiplying out gives
which produces the required result after some rearranging. As in the last question SymPy can do this calculation directly.
Q16 answer#
Rearranging the equation and expanding produces,
and at low speeds when \(v/c \lt 1\) then \((v/c)^2 < v/c\) therefore \(E= mc^2(1+v/2c+\cdots)\) which becomes \(E = mc^2\) when \(v \ll 2c\).
Q17 answer#
Using the formula \(\displaystyle x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}\) the square root can be expanded provided \(4ac/b^2 \lt 1\) and re-writing as \(\displaystyle x=\frac{ -b\pm b\sqrt{1-4ac/b^2} }{2a} \)
allows the square-root to be expanded. The result is
Because \(b^2 \gg ac\), the terms in \(b^4\) and higher powers will be small. Taking the positive root gives \(x \approx c/b\), and as \(b\) is larger than \(c\) this root will be small. The negative root gives \(x \approx b/a\) which is large.
Q18 answer#
Expanding the exponential as a ratio for small \(x\) gives
and dividing by \(x\) gives
and when \(x \rightarrow 0\) this produces \(1\) as the limit. Separately differentiating top and bottom of the original function according to l’Hopital’s rule (Chapter 3.8) produces \(\displaystyle e^{-x}\) and in the limit \(x \rightarrow 0\) this is \(1\).
Q19 answer#
(a) The expansion is \(\displaystyle \sum_{k=1} e^{ikx} = e^{ix} + e^{2ix} + e^{3ix} + e^{4ix} +\cdots \)
Removing the first term and substituting \(\displaystyle e^{inx} = w^n\) gives \(\displaystyle \sum_{k=1}e^{ikx} =e^{ix}(1+w^2 +w^3 +w^4 +\cdots+)\)
This series looks familiar; to check use the table of series expansions (Chapter 5.6.4) or try with SymPy.
w = sp.symbols('w')
sp.series( 1/(1-w),w )
which then gives \(\displaystyle \sum_{k=1}e^{ikx}=\frac{e^{ix}}{1-e^{ix}}\).
(b) The summation \(\sum_{k=1}^\infty e^{ikx}/k\) is very close to the integral of \(\sum_{k=1}^\infty e^{ikx}\) calculated in (a). Integrating this sum multiplied by \(i\) gives
Integrating the result from part (a) also multiplied by \(i\) gives
Q20 answer#
The expansion is
where the result is obtained by canceling terms. Instead of canceling the series expansion \(1/(1-p)=1+p+p^2 +\cdots\) could be used directly.
The average is calculated as \(\langle k \rangle = (1 - p)\sum\limits_{k=1}^\infty kp^{k-1}\) and a denominator is not needed because the distribution is normalized.
and this last result could also have been obtained directly by differentiating both sides of \(\sum_{k=1}^\infty p^k =1/(p-1)\) with respect to \( p\).
The average \(\langle k^2 \rangle\) is
The final series is the sum \(\displaystyle 1+\sum_{k=1}(2k+1)p^k\) as may be seen by putting \(k = 1, 2,\cdots\). To sum this use the results for \(\displaystyle \sum_{k=1} kp^k\) and \(\displaystyle \sum_{k=1} p^k\) both of which have been met before or are in the table. Alternatively SymPy could be used.
k, p = sp.symbols('k, p')
inf = sp.oo # sp.oo is SymPy infinity
s = (1 - p)*sp.summation(k**2*p**(k - 1),(k,1,inf))
sp.simplify(s)
The standard deviation is
which for large \(p\) becomes smaller but only as as \(1/\sqrt p\) as \(p\) increases. This behaviour is similar to that of repeated measurements reducing standard deviation as \(1/\sqrt N\).
Q21 answer#
The function is rewritten as \(\displaystyle \left(\frac{\sin(x)}{x}\right)^2\) and expanding the \(\sin(x)\) as a Taylor series about \(x = 0\) and dividing by \(x\) produces,
squaring the first three terms
which produces \(\displaystyle \left(\frac{\sin(x)}{x}\right)^2 \to 1\) when \(x \to 0\) because all the squared and higher terms are small compared to unity. The graph is shown below. The maximum is \(1\), as predicted, and there are repeated zeros at \(\pm n\pi\) where \(n\) is an integer because \(\sin(\pm n\pi) = 0\). These are also shown on the next plot.
# calculation of sinc function
fig1 = plt.figure(figsize=(5,4))
x = np.linspace(-3*np.pi,3*np.pi,100)
sinc2 = lambda x: (np.sin(x)/x)**2
plt.plot(x,sinc2(x),color='blue')
for i in range(1,4):
v = i*np.pi
plt.axvline( v,linestyle='--',color='gray',linewidth=1)
plt.axvline(-v,linestyle='--',color='gray',linewidth=1)
plt.xlabel(r'$x$')
plt.ylabel(r'$sinc^2(x)$')
plt.axhline(0,color='gray',linewidth=1)
plt.show()
Figure 24. Graph of \((\sin(x)/x)^2\). The vertical lines are at the zeros of the function that occur at \(n\pm \pi\) where \(n\) = 1, 2, \(\cdots\).
Q22 answer#
(a) The power series expansion has the form \(\displaystyle f(s)=e^{-s^2}=1-sf^{'}(0)+\frac{s^2}{s!}f^{''}(0)+\cdots \) and the derivatives are
When \(s = 0\) the Maclaurin series is \(\quad f(0) = 1,\quad f'(0) = 0,\quad f''(0) = -2,\quad f'''(0) = 0,\quad f^4(0) = 12\) where each odd-power derivative is zero. Substituting into the series expansion for \(\displaystyle e^{\large{{-s^2}}}\) gives
and integrating this series term by term produces,
(b) The next calculation compares approximation with exact values
from scipy.special import erf # import error function as this is not
# otherwise defined in python
apprx_erf = lambda x: (2/np.sqrt(np.pi))*(x - x**3/3 + x**5/10 - x**7/42 + x**9/216)
print('{:s}'.format(' x erf(x) approx erf(x)'))
for x in [0.1,0.5,0.8,1.0]:
print('{:6.3f}{:12.8f}{:12.8f}'.format( x,erf(x),apprx_erf(x)))
x erf(x) approx erf(x)
0.100 0.11246292 0.11246292
0.500 0.52049988 0.52050028
0.800 0.74210096 0.74216826
1.000 0.84270079 0.84344850
which shows that the series approximation is quite good even up to \(x = 1\) if not too much precision is required. Naturally increasing the number of terms in the series will improve the precision.
Q23 answer#
The series expansion can be worked out from first principles or looked up and based on \(\displaystyle \frac{1}{1-ax} =1+ax+(ax)^2 +(ax)^3 +\cdots\) with \(a=1,\,x=t^2\) and provided that \(t^2 \lt 1\) the expansion will be valid;
Integrating term by term gives \(\displaystyle \tanh^{-1}(x) \approx \left[t + t^3/3 + t^5/5 + \cdots \right]_0^{x} = x + x^3/3 + x^5/5 + \cdots\).
This series can be summed and compared to an accurate calculation;
x = 0.5
atanh = sum( [x**n/n for n in range(1,15,2)] ) # make list then sum, increment by 2 from 1 to 15
print('{:s}{:f}{:s}{:f}'.format('tanh(',x,') summation ',atanh))
print('{:s}{:f}{:s}{:f}'.format('tanh(',x,') direct ',np.arctanh(x)))
tanh(0.500000) summation 0.549304
tanh(0.500000) direct 0.549306
Q24 answer#
Expanding \(\psi\) about some arbitrary point \(x_0\) and then adding and subtracting \(h\) from \(x\) gives
Combining the terms as in the question, removes \(x_0\) and greatly simplifies the expression, and produces the required result;
Q25 answer#
The expansion is closely related to a standard one; see table of series expansions, giving \(\displaystyle (1+u^2)^{-1} =1-u^2 +u^4 -u^6 +\cdots\)
Integrating produces
Trying values of x gives \(\displaystyle \tan^{-1}(1) = \pi/4\), which is the same as saying that the tangent of \(45^\text{o}\) is 1, which of course you already knew. The series is therefore
Evaluating this you will find that it converges very, very slowly; the alternating positive and negative terms are clearly the cause of this therefore this is a very poor way to find \(\pi\), even to a few decimal places, as shown below where even 500 terms produce a poor estimation.
i = 500 # number of terms in summation
s = 4*sum( [ (-1)**(n+1.0)/(2*n-1.0) for n in range(1,i)] )
print('{:s}{:20.16f}'.format('series ', s))
print('{:s}{:20.16f}'.format('accurate', np.pi))
series 3.1435966595937890
accurate 3.1415926535897931
Q26 answer#
Expanding the cosine inside the integral gives
where the last summation term is obtained after some guessing and by copying the similar terms for the cosine and cubing and adding 1 to the power, (because we integrated) and also dividing by 6\(n\) + 1 for the same reason.
In calculating this series, several terms will be needed and using SymPy makes it relatively easy. A variable \(s\) holds the sum and it is added to each time round the loop. The result is plotted so that it can be seen how quickly or not the series converges to a result.
# Use our own factorial function using recursion; good for small values of n
#-------------------
def fact(n):
if n == 0 or n == 1:
return 1
else:
return n*fact(n-1)
#--------------------
fig2 = plt.figure(figsize=(5,4))
nmax = 20
x = 2.0
val = [0.0 for i in range(nmax)]
s = 0.0
for n in range(nmax):
s = s + (-1)**n*x**(6*n + 1)/( (6*n + 1)*(fact(2*n)) )
val[n] = s
plt.scatter(n, val[n], s=50,color='blue',facecolor='white',zorder=10)
#print('{:4d}{:16.10f}'.format(n,s)) # print each value if needed
pass
print('{:s} {:f} {:s} {:f}'.format('sum to ',x, ' is ',s ))
plt.plot(val,color='blue',linewidth=1)
plt.xlabel('n')
plt.ylabel('summed value')
plt.axhline(0,color='grey',linewidth=1)
plt.show()
sum to 2.000000 is 0.855475
Figure 25a. Terms in the summation as it extends with powers of \(n\).
By direct integration the similar answer is obtained.
x = sp.symbols('x')
ans = sp.integrate(sp.cos(x**3), (x,0,2),conds='none')
ans.evalf()
If a simple summation expression cannot be found, you can always try making a series with SymPy. Then repeat the calculation with more terms in the series.
Generally, one would proceed cautiously with this method. Some functions, such as the one examined here, are ‘perverse’ and may take hundreds of terms to converge and then the risk of numerical ‘rounding’ errors is large, which occurs when two large and similar real numbers are subtracted. In this example, the function oscillates wildly with increasing frequency as \(x\) becomes larger; this means that numerical errors dominate the integration when the upper limit approaches \(3\). Preferably a proper numerical method, see Chapters 10 & 11 or the Euler-Maclaurin formula, Section 7, should be used.
# calculate series of a general function in z and plot with original function.
# Try some functions such as cos(z^3)sin(z).
# this is a ***very, very slow*** calculation as the symbolic result is found first.
fig3 = plt.figure(figsize=(5,4))
z = sp.symbols('z') # define symbolic variable
f01 = sp.cos(z**3)*sp.exp(-z) # function to expand into series
num_terms = 100
s = sp.series(f01,z,0,num_terms).removeO() # get series and remove 'big O' as last term 150 terms in series
f03 = sp.lambdify(z,s,'numpy') # make into numpy function after series expansion
numx = 1000
maxx = 4
x = np.linspace(0,maxx,numx) # make set of numx points starting at 0 ending at 4
y = [ f03(x[i]) for i in range(numx) ] # calculate function at points x[i] and put into array for plotting
plt.plot(x,y,color='red',label='series')
afun = sp.lambdify(z,f01,'numpy') # make original function into numpy function
plt.plot(x,afun(x),color='blue',label = f01,linewidth=1)
plt.axis([0,maxx,-0.5,1.25])
plt.legend()
plt.title('series expansion & its function ',fontsize=12)
plt.show()
Figure 25b. \(\cos(x^3)e^{-x}\) and its series expansion up to \(x^{100}\). Notice how the series solution (red line) suddenly fails at about \(x = 2.3\). This will cause huge errors in the integration if the limit is taken this far.