The exercises below come from a practical-work handout written for Matlab by [[Person:Gautier List|M. Gautier List]]<ref>{{#cite:Q3739}}</ref>, translated here and solved with NumPy and Matplotlib. They cover <code>if</code> statements, user-defined [[Function|functions]], the trace of a matrix, parametric curves, the golden ratio, and the Fibonacci sequence.
The five exercises below come from a practical-work handout written for Matlab created by my beloved teacher [[Person:Gautier List|M. Gautier List]]<ref>{{#cite:Q3739}}</ref>, which we solve here with NumPy and SymPy.
<blockquote>
3 differences with Matlab are worth stating once:
* a Python function is declared with <code>def</code>, and its body is delimited by indentation instead of being closed by <code>end</code>;
* inside an <code>if</code> the equality test is <code>==</code>, conjunction is <code>and</code>, and disjunction is <code>or</code>, where Matlab writes <code>==</code>, <code>&&</code>, and <code>||</code>;
* a snippet marked <code>class="pyodide"</code> below runs in the reader's browser, so every program on this page can be executed in place.
</blockquote>
== Exercise 1 ==
== Exercise 1 ==
# What does the instruction <code>max(2, 3)</code> return?
# What does <code>max(2, 3)</code> return?
# Write a program that returns the maximum of 3 numbers $a$, $b$, and $c$, without using the function <code>max</code>.
# Write a function $m=\mathrm{mymax}(a,b,c)$ that returns the maximum of a set of n numbers $\max \{x_1,\dots,x_n\}$, without using the function <code>max</code>.
# Write the same thing again as a function $m=\mathrm{monmax}(a,b,c)$.
{{Hidden solution}}
{{Hidden solution}}
'''Question 1.''' <code>max</code> of two scalars compares them one against the other and returns the larger, so
In Python <code>max</code> accepts either several arguments (<code>max(2, 3)</code>) or one iterable (<code>max([2, 3])</code>); in Matlab the same instruction is <code>max(2,3)</code>, which also returns <code>3</code>, since <code>max</code> compares two arrays element by element.
<blockquote>
In Python <code>max</code> accepts either several arguments (<code>max(1, 2, 3, 4)</code>) or one iterable (<code>max([2, 3, 4, 6])</code>)
</blockquote>
'''Question 2.''' The comparison is written by hand, one candidate at a time: take $a$ as the provisional maximum, then let $b$, then $c$, take its place whenever it is larger.
if a >= b and a >= c: # a is at least as large as the 2 others
def mymax(number_list: list[int]) -> int: # do not name number_list "list": overshadowing Python built-in keywords breaks code unpredicatably and make you an unhappy individual
m = a
maximum = number_list[0] # initialize from the first element so comparison works; again do not name maximum "max".
elif b >= c: # otherwise b, if it beats c
for number in number_list[1:]:
m = b
if number > maximum:
else: # otherwise c
maximum = number
m = c
return maximum
print(f"the maximum of {a}, {b}, and {c} is {m}")
print(f"test list max: {mymax(test_list)}")
</syntaxhighlight>
</syntaxhighlight>
Line 49:
Line 42:
<pre>
<pre>
the maximum of 3, 7, and 5 is 7
test list max: 6
</pre>
</pre>
'''Question 3.''' The same comparison, wrapped in a function. The 2 tests are now independent, which is the usual way of writing it: the provisional maximum is updated twice, in sequence.
"""Return the largest of three numbers, without max()."""
m = a
if b > m: # strictly greater: a tie keeps the earlier value
m = b
if c > m:
m = c
return m
print(monmax(3, 7, 5))
print(monmax(-1, -5, -3))
print(monmax(2, 2, 1))
print(monmax(0.5, -2, 0.25))
mismatches = [(a, b, c)
for a, b, c in itertools.product(range(-5, 6), repeat=3)
if monmax(a, b, c) != max(a, b, c)]
print()
print("triples tested:", 11**3)
print("mismatches: ", len(mismatches))
</syntaxhighlight>
Expected output:
<pre>
7
-1
2
0.5
triples tested: 1331
mismatches: 0
</pre>
<blockquote>
* Assignment: <code>=</code>
The last lines check the function against the built-in <code>max</code> on every triple of integers between $-5$ and $5$: the count of disagreements is $0$.
* Commparison: <code>==</code>
</blockquote>
<blockquote>
Writing <code>if a = b:</code> buys a syntax error in Python, and <code>if a == b:</code> where <code>a</code> is supposed to be assigned to <code>b</code> causes a silent logic error: the assignment is not made, but Python doesn't complain.
Caution: the assignment is <code>=</code>, and the comparison inside an <code>if</code> is <code>==</code>. Writing <code>if a = b:</code> is a syntax error in Python, so the mistake is caught immediately, but <code>if a == b:</code> where an assignment was meant is not.
</blockquote>
</blockquote>
Line 104:
Line 59:
# Build a square $3\times3$ matrix of integers of your choice, named $A$.
# Build a square $3\times3$ matrix of integers of your choice, named $A$.
# Test the commands <code>trace(A)</code> and <code>sum(diag(A))</code>.
# Test the commands <code>np.trace(A)</code> and <code>np.sum(diag(A))</code> from <code>numpy</code>
# Write a function $t=\mathrm{matrace}(A)$ that computes the trace of a square $3\times3$ matrix, without using the functions <code>trace</code>, <code>sum</code>, or <code>diag</code>.
# Write a function $t=\mathrm{mytrace}(A)$ that computes the trace of any matrix, without using the functions <code>trace</code>, <code>sum</code>, or <code>diag</code>.
Recall that the trace of a square matrix $A=(a_{ij})$ is the sum of its diagonal entries,
Recall that the trace of a square matrix $A=(a_{ij})$ is the sum of its diagonal entries,
Line 142:
Line 97:
sum(diag(A)) = 3
sum(diag(A)) = 3
</pre>
</pre>
The diagonal entries are $4$, $5$, and $-6$, and $4+5-6=3$.
'''Question 3.''' Walk down the diagonal with a single index and accumulate the entries in a variable initialised at $0$.
'''Question 3.''' Walk down the diagonal with a single index and accumulate the entries in a variable initialised at $0$.
not square -> mytrace handles only square matrixs of dimension n*n;[[1, 2, 3], [4, 5, 6]] is not a square matrix
5
7
not square -> matrace expects a square matrix
1x1: matrace = 7 np.trace = 7 agree = True
2x2: matrace = -1 np.trace = -1 agree = True
3x3: matrace = -1 np.trace = -1 agree = True
5x5: matrace = -2 np.trace = -2 agree = True
10x10: matrace = 24 np.trace = 24 agree = True
</pre>
</pre>
<blockquote>
<blockquote>
The function is written for any size $n$ rather than for $3\times3$ alone, and for any container that supports <code>len</code> and indexing: a list of lists as well as a NumPy array. It also refuses a rectangular matrix, where <code>np.trace</code> happily sums $a_{00}$, $a_{11}$, and so on as far as the shape allows.
<code>np.diag</code> is versatile:
</blockquote>
<blockquote>
* on a 2D array it extracts the diagonal;
Careful with <code>np.diag</code>: on a 2D array it extracts the diagonal, but on a 1D array it does the opposite and builds a diagonal matrix. Matlab's <code>diag</code> has the same two behaviours.
* on an 1D array it does the opposite and builds a diagonal matrix.
</blockquote>
</blockquote>
{{End hidden}}
{{End hidden}}
Line 228:
Line 158:
$$
$$
'''3.''' A curve in space,
'''3.''' A curve in 3D Euclidean space,
$$
$$
Line 239:
Line 169:
{{Hidden solution}}
{{Hidden solution}}
The 3 curves share one figure, as a row of 3 panels. The sampling step must be small with respect to the period $2\pi$ of the sines: about 600 points over the interval, so that the curves are drawn as curves and not as polygons.
ax = fig.add_subplot(1, 3, 1) # set plot target (ax) to plot 1 of a plot panel with 1 row and 3 columns (fig),
ax.plot(np.sin(2 * t), np.sin(3 * t), lw=1.2)
ax.plot(np.sin(2 * t), np.sin(3 * t), lw=1.2)
ax.set_aspect("equal") # same scale on both axes: a circle stays round
ax.set_aspect("equal") # same scale on both axes: a circle stays round
Line 256:
Line 184:
ax.set_xlabel("$x(t)$")
ax.set_xlabel("$x(t)$")
ax.set_ylabel("$y(t)$")
ax.set_ylabel("$y(t)$")
ax.grid(alpha=0.3)
ax.grid(alpha=0.3) # semi-transparent gridlines
# 2. the same, with a linear drift
# 2. the same, with a linear drift
Line 270:
Line 198:
# 3. a curve in space
# 3. a curve in space
t = np.linspace(-2 * np.pi, 2 * np.pi, 601)
t = np.linspace(-2 * np.pi, 2 * np.pi, 601)
ax = fig.add_subplot(1, 3, 3, projection="3d")
ax = fig.add_subplot(1, 3, 3, projection="3d") # plot handles 3D just like 2D once you specify projection="3d"
ax.plot(t * np.cos(t), t * np.sin(t), t, lw=1.2)
ax.plot(t * np.cos(t), t * np.sin(t), t, lw=1.2)
ax.set_title(r"$x=t\cos t$, $y=t\sin t$, $z=t$")
ax.set_title(r"$x=t\cos t$, $y=t\sin t$, $z=t$")
Line 281:
Line 209:
</syntaxhighlight>
</syntaxhighlight>
[[File:Three parametric curves.png|thumbnail|300px|alt=Three panels: a closed Lissajous figure with crossing loops; a straight diagonal line decorated with small loops; a three-dimensional corkscrew widening away from the origin|Left: the Lissajous curve of question 1. Middle: the curve of question 2, a diagonal drift decorated with loops. Right: the spatial curve of question 3, a corkscrew whose radius grows with $t$.]]
[[File:Three parametric curves.png|thumbnail|500px|alt=Three panels: a closed Lissajous figure with crossing loops; a straight diagonal line decorated with small loops; a three-dimensional corkscrew widening away from the origin|Left: the Lissajous curve (Q1). Middle: a diagonal drift decorated with loops (Q2) Right: the spatial curve (Q3), a corkscrew whose radius grows with $t$.]]
<blockquote>
<blockquote>
Notice the periods: $x$ has period $\pi$ and $y$ has period $2\pi/3$, so their common period is $2\pi$, and the interval $-2\pi\le t\le2\pi$ describes the closed curve twice. Without <code>set_aspect("equal")</code> the figure is stretched by the shape of the axes, and a closed symmetric curve looks distorted.
Notice the periods for curve 1&2: $x$ has period $\pi$ and $y$ has period $2\pi/3$, so their common period is $2\pi$, and the interval $-2\pi\le t\le2\pi$ consists of two periods.
Without <code>set_aspect("equal")</code> the figure is stretched by the shape of the axes, and curves look distorted.
The third curve needs <code>projection="3d"</code> in <code>add_subplot</code>: its radius $\sqrt{x^{2}+y^{2}}=|t|$ grows with $|t|$, which draws a corkscrew opening outwards.
The third curve has radius $\sqrt{x^{2}+y^{2}}=|t|$ which grows with $|t|$, drawing a corkscrew opening outwards.
</blockquote>
</blockquote>
{{End hidden}}
{{End hidden}}
== Problem: the golden ratio and the Fibonacci sequence ==
== Exercise 3: the golden ratio and the Fibonacci sequence ==
The golden ratio is a proportion originally defined in geometry as the ratio of 2 lengths $a$ and $b$, noted $\varphi$, such that
The golden ratio is a proportion originally defined in geometry as the ratio of 2 lengths $a$ and $b$, noted $\varphi$, such that
Line 299:
Line 229:
$$
$$
with $a>b>0$. It appears in painting, sculpture, and architecture, and in nature.
with $a>b>0$. Said ratio appears not only in human-creations (paintings, sculptures, architecture, etc.), but also in nature.
=== 1. The equation satisfied by the golden ratio ===
=== 1. The equation satisfied by the golden ratio ===
Line 310:
Line 240:
{{Hidden solution}}
{{Hidden solution}}
Start from the 2nd form of the ratio and split the fraction:
Python has no real square root of a negative number, but <code>cmath.sqrt</code> returns a complex one, so a single code path covers $\Delta>0$, $\Delta=0$, and $\Delta<0$.
Because <code>cmath.sqrt</code> always returns a complex number, so do the roots, even when they are real: they carry a zero imaginary part. To stay in the reals, branch on the sign of $\Delta$ and use <code>math.sqrt</code> when $\Delta\ge0$:
Note <code>cmath.sqrt</code> always returns a complex number, so our <code>quadratic_solve</code> function does as well. When the imaginary part 0j, the root is acutally real.$:
Since $a>b>0$, $\varphi=\dfrac{a}{b}>1$, which selects the positive root:
By convention, on takes the positive value $\varphi=\frac{1+\sqrt{5}}{2}$.
$$
\varphi=\frac{1+\sqrt{5}}{2}\approx1.618
$$
The other root is $-\dfrac{1}{\varphi}$, the conjugate golden ratio, whose product with $\varphi$ is $-1$, as Vieta's relation $x_1x_2=\dfrac{c}{a}=-1$ requires.
Alternatively, we can also be lazy and solve (1) with the <code>quadratic_solve</code> function we just handrolled.
Sympy confirms the value exactly, and confirms that it cancels the polynomial of question 1:
phi = max(roots, key=lambda r: float(r)) # Pick the positive root, notice since roots are sympy objects, direct comparison does not work without float conversion.
print("positive root:", roots[1])
print("difference from (1 + sqrt(5))/2:", sp.simplify(roots[1] - phi_exact))
print(f"F_{n:<2} = {f}") # <2 renders n as a string of minimum width 2.
</syntaxhighlight>
</syntaxhighlight>
Line 514:
Line 399:
<blockquote>
<blockquote>
Python integers have no fixed width, so the loop does not overflow the way a Matlab <code>double</code> would: it can run to $N=1000$ and beyond. The terms only lose their exactness when they are converted to a float, which happens for $F_n$ at $n=1477$, the 1st term above $1.8\cdot10^{308}$.
Python integers have no fixed upper limit, so the iterative loop can run to $N=1000$ and beyond (warning: it will take a while). The terms are automatically converted to a float approximation above $1.8\cdot10^{308}$, which happens for $F_n$ at $n=1477$.
</blockquote>
</blockquote>
{{End hidden}}
{{End hidden}}
Line 523:
Line 408:
{{Hidden solution}}
{{Hidden solution}}
Print the ratio, its distance to $\varphi$, and that distance multiplied by $\varphi^{2n}$:
print(f"{n:3d} {r:16.12f} {err:10.2e}") # 16.12f: minimum width 16, 12 digits after decimal point, fixed-point notation (f). 10.2e: exponential notation (e)
</syntaxhighlight>
</syntaxhighlight>
Line 562:
Line 445:
19 1.618033963167 2.56e-08 2.236068
19 1.618033963167 2.56e-08 2.236068
</pre>
</pre>
As seen, the error decreases as $n$ increases, demonstrating convergence.
The ratio does converge, and the last column is the interesting one: it settles at $2.236068$, which is $\sqrt{5}$, to 6 digits. The distance to $\varphi$ therefore behaves as
[[File:Fibonacci ratio convergence.png|thumbnail|400px|alt=Two panels: the ratio of consecutive Fibonacci numbers oscillating about the golden ratio and settling on it; the absolute error on logarithmic axes following a straight line of slope minus 2 in the exponent|Left: the ratio $F_{n+1}/F_n$ against $\varphi$. Right: the absolute error $|F_{n+1}/F_n-\varphi|$ on a logarithmic scale, which interestingly follow the line $\sqrt{5}\,\varphi^{-2n}$. (Illustration, not an output of the script above)]]
Since $\psi<0$, the ratio alternates above and below $\varphi$, as the 2nd column of the output shows; and since $|\psi|^{n}=\varphi^{-n}$ while $F_n\approx\varphi^{n}/\sqrt{5}$, the distance falls like $\varphi^{-2n}$, that is, by a factor $\varphi^{2}\approx2.618$ at each step.
[[File:Fibonacci ratio convergence.png|thumbnail|400px|alt=Two panels: the ratio of consecutive Fibonacci numbers oscillating about the golden ratio and settling on it; the absolute error on logarithmic axes following a straight line of slope minus 2 in the exponent|Left: the ratio $F_{n+1}/F_n$ against $\varphi$. Right: the absolute error $|F_{n+1}/F_n-\varphi|$ on a logarithmic scale, along the line $\sqrt{5}\,\varphi^{-2n}$. The convergence is geometric, with ratio $\varphi^{2}$. Illustration, not an output of the script above.]]
{{End hidden}}
{{End hidden}}
=== 6. The golden spiral ===
=== 6. The golden spiral ===
The golden spiral, built on the proportions of the golden ratio, is approximated by the logarithmic spiral of polar equation
The golden spiral, built on the proportions of the golden ratio, can be drawn as the [[polar equation]]
or, as a parametric curve in Cartesian coordinates,
where $A$ is the scale factor.
$$
\begin{cases}
x=r(\theta)\cos(\theta) \\
y=r(\theta)\sin(\theta)
\end{cases}
$$
Plot the golden spiral, choosing an arbitrary value for $A$.
Plot the golden spiral choosing an arbitrary value for $A$.
{{Hidden solution}}
{{Hidden solution}}
$A$ is only a scale: it does not change the shape of the spiral, and $A=1$ is the simplest choice. The constant $B$ is set so that a quarter turn multiplies the radius by $\varphi$,
$A$ is only a scale: it does not change the shape of the spiral, so choose the simplest choice $A=1$.
print("two whole turns multiply r by ", np.exp(B * 4 * np.pi))
print("phi**8 = ", phi**8)
</syntaxhighlight>
</syntaxhighlight>
Expected output:
[[File:Golden spiral.png|thumbnail|400px|alt=A spiral coiling outwards from the centre, each quarter turn wider by the golden ratio|The golden spiral for $A=1$ and $0\le\theta\le4\pi$]]
<pre>
one quarter turn multiplies r by 1.618033988749895
two whole turns multiply r by 46.97871376374778
phi**8 = 46.978713763747805
</pre>
[[File:Golden spiral.png|thumbnail|400px|alt=A spiral coiling outwards from the centre, each quarter turn wider by the golden ratio|The golden spiral for $A=1$ and $0\le\theta\le4\pi$: 2 turns, during which the radius is multiplied by $\varphi^{8}\approx46.98$. The marks sit at each quarter turn.]]
<blockquote>
During 2 turns the radius grows by a factor $\varphi^{8}\approx47$, so the outer coil is far wider than the inner one and the 2 cannot both be seen in detail on the same figure. <code>set_aspect("equal")</code> keeps the circles centred on the origin circular: without it, the spiral looks elliptical.
</blockquote>
{{End hidden}}
{{End hidden}}
Line 667:
Line 489:
<references />
<references />
[[Category:Function]]
Latest revision as of 10:26, 20 November 2026
The five exercises below come from a practical-work handout written for Matlab created by my beloved teacher M. Gautier List[1], which we solve here with NumPy and SymPy.
Exercise 1
What does max(2, 3) return?
Write a function $m=\mathrm{mymax}(a,b,c)$ that returns the maximum of a set of n numbers $\max \{x_1,\dots,x_n\}$, without using the function max.
Question 1.
print(max(2,3))
Expected output:
3
In Python max accepts either several arguments (max(1, 2, 3, 4)) or one iterable (max([2, 3, 4, 6]))
Question 2/3.
test_list=[1,2,3,4,5,6]defmymax(number_list:list[int])->int:# do not name number_list "list": overshadowing Python built-in keywords breaks code unpredicatably and make you an unhappy individualmaximum=number_list[0]# initialize from the first element so comparison works; again do not name maximum "max".fornumberinnumber_list[1:]:ifnumber>maximum:maximum=numberreturnmaximumprint(f"test list max: {mymax(test_list)}")
Expected output:
test list max: 6
Caution:
Assignment: =
Commparison: ==
Writing if a = b: buys a syntax error in Python, and if a == b: where a is supposed to be assigned to b causes a silent logic error: the assignment is not made, but Python doesn't complain.
Exercise 2
Build a square $3\times3$ matrix of integers of your choice, named $A$.
Test the commands np.trace(A) and np.sum(diag(A)) from numpy
Write a function $t=\mathrm{mytrace}(A)$ that computes the trace of any matrix, without using the functions trace, sum, or diag.
Recall that the trace of a square matrix $A=(a_{ij})$ is the sum of its diagonal entries,
$$
\operatorname{tr}(A)=\sum_{i=1}^{n}a_{ii}.
$$
Questions 1 and 2.np.trace computes the trace directly, while np.sum(np.diag(A)) extracts the diagonal with np.diag and then sums it: two routes to the same number.
Question 3. Walk down the diagonal with a single index and accumulate the entries in a variable initialised at $0$.
defmytrace(A):"""Trace of a square matrix, without numpy's trace(), sum(), or diag()."""n=len(A)ifany(len(row)!=nforrowinA):raiseValueError(f"mytrace handles only square matrixs of dimension n*n;{A} is not a square matrix")t=0foriinrange(n):t+=A[i][i]# A[i][i] on a list of lists, or on an arrayreturntprint(mytrace([[1,2,3],[4,5,6],[7,8,9]]))try:mytrace([[1,2,3],[4,5,6]])exceptValueErroraserr:print("not square ->",err)
Expected output:
15
not square -> mytrace handles only square matrixs of dimension n*n;[[1, 2, 3], [4, 5, 6]] is not a square matrix
np.diag is versatile:
on a 2D array it extracts the diagonal;
on an 1D array it does the opposite and builds a diagonal matrix.
importnumpyasnpimportmatplotlib.pyplotaspltfig=plt.figure(figsize=(13,4.2))# 1. the Lissajous curvet=np.linspace(-2*np.pi,2*np.pi,601)ax=fig.add_subplot(1,3,1)# set plot target (ax) to plot 1 of a plot panel with 1 row and 3 columns (fig), ax.plot(np.sin(2*t),np.sin(3*t),lw=1.2)ax.set_aspect("equal")# same scale on both axes: a circle stays roundax.set_title(r"$x=\sin(2t)$, $y=\sin(3t)$")ax.set_xlabel("$x(t)$")ax.set_ylabel("$y(t)$")ax.grid(alpha=0.3)# semi-transparent gridlines# 2. the same, with a linear driftt=np.linspace(-4*np.pi,4*np.pi,1201)ax=fig.add_subplot(1,3,2)ax.plot(t+np.sin(2*t),t+np.sin(3*t),lw=1.2)ax.set_aspect("equal")ax.set_title(r"$x=t+\sin(2t)$, $y=t+\sin(3t)$")ax.set_xlabel("$x(t)$")ax.set_ylabel("$y(t)$")ax.grid(alpha=0.3)# 3. a curve in spacet=np.linspace(-2*np.pi,2*np.pi,601)ax=fig.add_subplot(1,3,3,projection="3d")# plot handles 3D just like 2D once you specify projection="3d"ax.plot(t*np.cos(t),t*np.sin(t),t,lw=1.2)ax.set_title(r"$x=t\cos t$, $y=t\sin t$, $z=t$")ax.set_xlabel("$x$")ax.set_ylabel("$y$")ax.set_zlabel("$z$")fig.tight_layout()plt.show()
Left: the Lissajous curve (Q1). Middle: a diagonal drift decorated with loops (Q2) Right: the spatial curve (Q3), a corkscrew whose radius grows with $t$.
Notice the periods for curve 1&2: $x$ has period $\pi$ and $y$ has period $2\pi/3$, so their common period is $2\pi$, and the interval $-2\pi\le t\le2\pi$ consists of two periods.
Without set_aspect("equal") the figure is stretched by the shape of the axes, and curves look distorted.
The third curve has radius $\sqrt{x^{2}+y^{2}}=|t|$ which grows with $|t|$, drawing a corkscrew opening outwards.
Exercise 3: the golden ratio and the Fibonacci sequence
The golden ratio is a proportion originally defined in geometry as the ratio of 2 lengths $a$ and $b$, noted $\varphi$, such that
$$
\varphi=\frac{a}{b}=\frac{a+b}{a}
$$
with $a>b>0$. Said ratio appears not only in human-creations (paintings, sculptures, architecture, etc.), but also in nature.
importcmathdefquadratic_solve(a,b,c):"""Return the two roots of a*x**2 + b*x + c = 0."""ifa==0:raiseValueError("a is zero: this is not a second-degree equation")delta=b*b-4*a*c# the discriminantdelta_root=cmath.sqrt(delta)# cmath.sqrt handles square root with complex numbers, so this covers delta < 0 as wellreturn(-b+delta_root)/(2*a),(-b-delta_root)/(2*a)# testfora,b,cin[(1,-1,-1),(1,-3,2),(1,0,1),(2,4,2)]:print(f"a={a}, b={b}, c={c} -> {quadratic_solve(a,b,c)}")
Note cmath.sqrt always returns a complex number, so our quadratic_solve function does as well. When the imaginary part 0j, the root is acutally real.$:
By convention, on takes the positive value $\varphi=\frac{1+\sqrt{5}}{2}$.
Alternatively, we can also be lazy and solve (1) with the quadratic_solve function we just handrolled.
Or with Sympy:
importsympyasspx=sp.symbols("x")roots=sp.solve(x**2-x-1,x)# x**2 - x - 1 = 0print("roots:",roots)phi=max(roots,key=lambdar:float(r))# Pick the positive root, notice since roots are sympy objects, direct comparison does not work without float conversion.print("Phi:",phi)print(f"Phi (approximate): {float(phi):.3f}")
Python integers have no fixed upper limit, so the iterative loop can run to $N=1000$ and beyond (warning: it will take a while). The terms are automatically converted to a float approximation above $1.8\cdot10^{308}$, which happens for $F_n$ at $n=1477$.
5. Numerical convergence of the ratios
Verify numerically that the ratio $\dfrac{F_{n+1}}{F_n}$ tends to the golden ratio.
importnumpyasnpphi=(1+np.sqrt(5))/2print(f"{'n':>3}{'F_{n+1}/F_n':>16}{'|error|':>10}")forninrange(1,N):r=F[n]/F[n-1]# F_{n+1} / F_nerr=abs(r-phi)print(f"{n:3d}{r:16.12f}{err:10.2e}")# 16.12f: minimum width 16, 12 digits after decimal point, fixed-point notation (f). 10.2e: exponential notation (e)
As seen, the error decreases as $n$ increases, demonstrating convergence.
Left: the ratio $F_{n+1}/F_n$ against $\varphi$. Right: the absolute error $|F_{n+1}/F_n-\varphi|$ on a logarithmic scale, which interestingly follow the line $\sqrt{5}\,\varphi^{-2n}$. (Illustration, not an output of the script above)
The golden spiral, built on the proportions of the golden ratio, can be drawn as the polar equation
$$
r(\theta)=A\varphi^{2\theta/\pi}
$$
where $A$ is the scale factor.
Plot the golden spiral choosing an arbitrary value for $A$.
$A$ is only a scale: it does not change the shape of the spiral, so choose the simplest choice $A=1$.
importnumpyasnpimportmatplotlib.pyplotaspltphi=(1+np.sqrt(5))/2theta=np.linspace(0,6*np.pi,1000)r=phi**(2*theta/np.pi)# A = 1fig=plt.figure(figsize=(5,5))ax=fig.add_subplot(projection="polar")# or plt.subplots(subplot_kw={"projection": "polar"})ax.plot(theta,r,color="goldenrod",lw=2)ax.set_rlim(0,r.max())ax.set_rticks([5,25,100])ax.set_rlabel_position(67.5)# where the r labels sit, in degreesplt.show()
The golden spiral for $A=1$ and $0\le\theta\le4\pi$