Lab Sheet 1¶
from header import *
Exercise 2.1
Here we do some basic arithmetic in Python
2+2
4
print((3+7+10)*(1000-8) / (900+90+2) - 17) # result is a float
print((3+7+10)*(1000-8) // (900+90+2) - 17) # result is an integer
3.0 3
Exercise 2.2
print(6**20 * 15**20 // 9**20)
100000000000000000000
The answer is one followed by 20 zeros, or in other words $ 10^{20} $. This is easy to see by hand, because $ \frac{6^{20} 15^{20}}{9^{20}}=\left(\frac{6\times\,15}{9}\right)^{20} $ and $(6 \times 15)/9$ is just $10$.
print((10**10-1) // 99)
101010101
The answer is $ 1+100+10000+1000000+100000000 $, or in other words $ 1+100+100^{2}+100^{3}+100^{4} $. The standard geometric progression formula says that this is the same as $ \frac{100^{5}-1}{100-1} $, or in other words $ \frac{10^{10}-1}{99} $.
print((10**10 - 10 - 9**2) // 9**2)
123456789
To check this by hand, let $ x $ be the number $ 123456789 $. Then $ 10 x =1234567890 $ and so $ 10 x +9=1234567899 $. If we subtract $ x $ from this the digits mostly cancel and we get $ 9 x +9=1111111110 $. Multiply by 9 again to get $ 9^{2} (x +1)=9999999990 $, which is $ 10^{10}-10 $. Rearrange this to get $ x +1=\frac{10^{10}-10}{9^{2}} $ and so $ x =\frac{10^{10}-10-9^{2}}{9^{2}} $.
print((10**9+1)*(10**10 - 10 - 9**2) // 9**2)
123456789123456789
Assuming part (c), we have $ \frac{10^{10}-10-9^{2}}{9^{2}}=123456789 $ and so $ \frac{10^{9} (10^{10}-10-9^{2})}{9^{2}}=123456789000000000 $. These two numbers can be added without any carrying, to give $ \frac{(10^{9}+1) (10^{10}-10-9^{2})}{9^{2}}=123456789000000000+123456789 $ = 123456789123456789.
Exercise 2.3
The numbers in this exercise are approximations to $\pi$; in a certain sense, they are actually the best possible approximations.
pi_approx = [
3 + sp.Rational(1,7),
3 + 1/(7 + sp.Rational(1, 16)),
3 + 1/(7 + 1/(15 + 1/(1 + sp.Rational(1, 293))))
]
print(pi_approx) # rational approximations to pi
print([float(x) - np.pi for x in pi_approx]) # errors in approximations
[22/7, 355/113, 104348/33215] [0.0012644892673496777, 2.667641894049666e-07, 3.3162805834763276e-10]
Exercise 2.4
Here we calculate the first 4997 digits of $e$, which end with 66666. This is the first place where a digit is repeated five times in a row.
N = 4997
x = sp.N(sp.exp(1),N)
s = str(x)
m = 100
for i in range(0, len(s), m):
print(s[i:i+m])
2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642 7427466391932003059921817413596629043572900334295260595630738132328627943490763233829880753195251019 0115738341879307021540891499348841675092447614606680822648001684774118537423454424371075390777449920 6955170276183860626133138458300075204493382656029760673711320070932870912744374704723069697720931014 1692836819025515108657463772111252389784425056953696770785449969967946864454905987931636889230098793 1277361782154249992295763514822082698951936680331825288693984964651058209392398294887933203625094431 1730123819706841614039701983767932068328237646480429531180232878250981945581530175671736133206981125 0996181881593041690351598888519345807273866738589422879228499892086805825749279610484198444363463244 9684875602336248270419786232090021609902353043699418491463140934317381436405462531520961836908887070 1676839642437814059271456354906130310720851038375051011574770417189861068739696552126715468895703503 5402123407849819334321068170121005627880235193033224745015853904730419957777093503660416997329725088 6876966403555707162268447162560798826517871341951246652010305921236677194325278675398558944896970964 0975459185695638023637016211204774272283648961342251644507818244235294863637214174023889344124796357 4370263755294448337998016125492278509257782562092622648326277933386566481627725164019105900491644998 2893150566047258027786318641551956532442586982946959308019152987211725563475463964479101459040905862 9849679128740687050489585867174798546677575732056812884592054133405392200011378630094556068816674001 6984205580403363795376452030402432256613527836951177883863874439662532249850654995886234281899707733 2761717839280349465014345588970719425863987727547109629537415211151368350627526023264847287039207643 1005958411661205452970302364725492966693811513732275364509888903136020572481765851180630364428123149 6550704751025446501172721155519486685080036853228183152196003735625279449515828418829478761085263981 3955990067376482922443752871846245780361929819713991475644882626039033814418232625150974827987779964 3730899703888677822713836057729788241256119071766394650706330452795466185509666618566470971134447401 6070462621568071748187784437143698821855967095910259686200235371858874856965220005031173439207321139 0803293634479727355955277349071783793421637012050054513263835440001863239914907054797780566978533580 4896690629511943247309958765523681285904138324116072260299833053537087613893963917795745401613722361 8789365260538155841587186925538606164779834025435128439612946035291332594279490433729908573158029095 8631382683291477116396337092400316894586360606458459251269946557248391865642097526850823075442545993 7691704197778008536273094171016343490769642372229435236612557250881477922315197477806056967253801718 0776360346245927877846585065605078084421152969752189087401966090665180351650179250461950136658543663 2712549639908549144200014574760819302212066024330096412704894390397177195180699086998606636583232278 7093765022601492910115171776359446020232493002804018677239102880978666056511832600436885088171572386 6984224220102495055188169480322100251542649463981287367765892768816359831247788652014117411091360116 4995076629077943646005851941998560162647907615321038727557126992518275687989302761761146162549356495 9037980458381823233686120162437365698467037858533052758333379399075216606923805336988795651372855938 8349989470741618155012539706464817194670834819721448889879067650379590366967249499254527903372963616 2658976039498576741397359441023744329709355477982629614591442936451428617158587339746791897571211956 1873857836447584484235555810500256114923915188930994634284139360803830916628188115037152849670597416 2562823609216807515017772538740256425347087908913729172282861151591568372524163077225440633787593105 9826760944203261924285317018781772960235413060672136046000389661093647095141417185777014180606443636 8154644400533160877831431744408119494229755993140118886833148328027065538330046932901157441475631399 9722170380461709289457909627166226074071874997535921275608441473782330327033016823719364800217328573 4935947564334129943024850235732214597843282641421684878721673367010615094243456984401873312810107945 1272237378861260581656680537143961278887325273738903928905068653241380627960259303877276977837928684 0932536588073398845721874602100531148335132385004782716937621800490479559795929059165547050577751430 8175112698985188408718564026035305583737832422924185625644255022672155980274012617971928047139600689 1638286652770097527670697770364392602243728418408832518487704726384403795301669054659374616193238403 6389313136432713768884102681121989127522305625675625470172508634976536728860596675274086862740791285 6576996313789753034660616669804218267724560530660773899624218340859882071864682623215080288286359746 8396543588566855037731312965879758105012149162076567699506597153447634703208532156036748286083786568 03073062657633469774295634643716709397193060876963495328846833613038829431040800296873869117066666
Exercise 2.5
It is an interesting fact (involving some very deep mathematics) that $e^{\pi\sqrt{163}}$ is very close to being an integer. Here we check that numerically.
x = sp.N(sp.exp(sp.pi * sp.sqrt(163)),40)
z = sp.ceiling(x)
display(Latex(r"$\exp(\pi\sqrt{163})\approx" + str(x) + "$"))
display(Latex(r"This is very close to the integer $" + str(z) + "$"))
display(Latex(r"The difference is $" + str(x - z) + "$"))
del x, z
Exercise 3.1
Here we convert the expression $A=(x^2-4y^2)(x^3-xy^2)$ into various different forms to see which is simplest.
x, y = sp.symbols('x y')
A = (x**2 - 4*y**2)*(x**3 - x*y**2)
display(Latex(r"$A = " + sp.latex(A) + "$"))
display(Latex("Simplified: $" + sp.latex(sp.simplify(A)) + "$"))
display(Latex("Factored: $" + sp.latex(sp.factor(A)) + "$"))
display(Latex("Expanded: $" + sp.latex(sp.expand(A)) + "$"))
display(Latex("Horner form: $" + sp.latex(sp.polys.polyfuncs.horner(A)) + "$"))
#del x, y, A
I would vote for $ (x -2 y) (x +2 y) x (x -y) (x +y) $ as the most useful version, but of course it depends what you want to use it for.
Exercise 3.2
Here we simplify the expression $A=\frac{2x}{x^2-1}+\frac{1}{x+x^2}+\frac{1}{x-x^2}$
x = sp.symbols('x')
A = 2*x/(x**2-1) + 1/(x+x**2) + 1/(x-x**2)
display(A)
display(sp.simplify(A))
del x, A
To do this by hand, note that $ x^{2}-1=(x +1)(x -1) $ and $ x +x^{2}=x (x +1) $ and $ x -x^{2}=-x (x -1) $. Thus, our expression is $ \frac{2 x^{2}}{x (x +1) (x -1)}+\frac{x -1}{x (x +1) (x -1)}-\frac{x +1}{x (x +1) (x -1)} $, which simplifies to $ \frac{2 x^{2}-2}{x (x +1) (x -1)} $. The numerator here factors as $ 2 (x +1) (x -1) $ and so everything cancels to leave $ \frac{2}{x} $.
Exercise 4.1
Here we plot $\sin(x)$ together with its 7th order Taylor series.
x = np.linspace(-4, 4, 201)
y = np.sin(x)
z = x - x**3/6 + x**5/120 - x**7/5040
fig = go.Figure()
fig.add_trace(go.Scatter(x=x, y=y))
fig.add_trace(go.Scatter(x=x, y=z))
x = np.linspace(-4, 4, 200)
y = np.sin(x)
z = x - x**3/6 + x**5/120 - x**7/5040
fig = plotly.subplots.make_subplots(rows=1, cols=3, subplot_titles=('sin(x)', 'Taylor series', 'Comparison'))
# Subplot 1: sin(x)
fig.add_trace(go.Scatter(x=x, y=y, mode='lines', name=r'$\sin(x)$', line=dict(color='blue')), row=1, col=1)
fig.add_trace(go.Scatter(x=x, y=z, mode='lines', name='Taylor series', line=dict(color='red')), row=1, col=2)
fig.add_trace(go.Scatter(x=x, y=y, mode='lines', line=dict(color='blue'), showlegend=False), row=1, col=3)
fig.add_trace(go.Scatter(x=x, y=z, mode='lines', line=dict(color='red'), showlegend=False), row=1, col=3)
fig.update_layout(height=400, width=1200, showlegend=True)
fig.show()
del x, y, z
The two graphs are very close together for $x$ between about $-3$ and $3$, but outside that range they move apart very rapidly.
Exercise 5.1
Here we solve the simultaneous equations $x^2+y^2=(x-1)^2+(y-1)^2=1$.
x, y = sp.symbols('x y')
eqs = [sp.Eq(x**2 + y**2,1), sp.Eq((x-1)**2 + (y-1)**2,1)]
print('Equations:')
for e in eqs:
display(e)
print('Solutions:')
sol = sp.solve(eqs)
for s in sol:
display(s)
Equations:
Solutions:
{x: 0, y: 1}
{x: 1, y: 0}
To obtain this by hand, expand out the second equation to get $x^{2}-2 x +1+y^{2}-2 y +1=1$, or equivalently $ x^{2}+y^{2}=2 x +2 y -1 $. Subtracting the first equation gives $ 0=2 x +2 y -2 $, so $ x +y =1 $. Squaring this gives $ x^{2}+y^{2}+2 x y =1 $, and subtracting the first equation gives $ 2 x y =0 $. This means that either $ x $ or $ y $ must be zero, and the equation $ x +y =1 $ means that the other one must be $ 1 $.
fig = go.Figure()
# First circle
fig.add_shape(type='circle', x0=-1, y0=-1, x1=1, y1=1, line=dict(color='blue'), name='Circle 1')
fig.add_shape(type='circle', x0=0, y0=0, x1=2, y1=2, line=dict(color='red'), name='Circle 2')
scatter_x = [float(s[x]) for s in sol]
scatter_y = [float(s[y]) for s in sol]
fig.add_trace(go.Scatter(x=scatter_x, y=scatter_y, mode='markers', marker=dict(color='black'), name='Points'))
# Ensure equal aspect ratio
fig.update_layout(
width=400,
height=400,
xaxis=dict(
scaleanchor="x",
scaleratio=1,
constrain='domain'
),
yaxis=dict(
scaleanchor="y",
scaleratio=1,
constrain='domain'
)
)
fig.show()
del x, y, eqs, sol
Geometrically, the equation $ x^{2}+y^{2}=1 $ describes the circle of radius one centred at the origin, and the equation $ (x -1)^{2}+(y -1)^{2}=1 $ describes the circle of radius one centred at (1,1). The two circles intersect at the points (1,0) and (0,1), which give the two solutions to our equations.
Exercise 6.1
Here we evaluate and simplify the derivatives of three different functions.
x = sp.symbols('x')
fx = sp.log(sp.log(sp.log(x)))
dfx = sp.diff(fx, x)
display(Latex(r"$\Large\frac{d}{dx}" + sp.latex(fx) + "=" + sp.latex(dfx) + "$"))
gx = (3*x+4)/(2*x+3)
dgx = sp.diff(gx, x)
dgx1 = sp.simplify(dgx)
display(Latex(r"$\Large\frac{d}{dx}" + sp.latex(gx) + "=" + sp.latex(dgx) + "=" + sp.latex(dgx1) + "$"))
hx = (1+x**2+x**4/2) * sp.exp(-x**2)
dhx = sp.diff(hx, x)
dhx1 = sp.simplify(dhx)
display(Latex(r"$\Large\frac{d}{dx}" + sp.latex(hx) + "=" + sp.latex(dhx) + "=" + sp.latex(dhx1) + "$"))
del x, fx, gx, hx
Exercise 6.2
Here we evaluate $\int_{x=0}^1\sqrt{1-x^2}\,dx$.
x = sp.symbols('x')
fx = sp.sqrt(1-x**2)
A = sp.integrate(fx, (x,0,1))
display(Latex("$\\large\\int_0^1" + sp.latex(fx) + "\\,dx = " + sp.latex(A) + "$"))
del x, fx, A