<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wikibase.ronzz.org/index.php?action=history&amp;feed=atom&amp;title=Function%2FPractice%2FFunctions_with_Python</id>
	<title>Function/Practice/Functions with Python - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wikibase.ronzz.org/index.php?action=history&amp;feed=atom&amp;title=Function%2FPractice%2FFunctions_with_Python"/>
	<link rel="alternate" type="text/html" href="https://wikibase.ronzz.org/index.php?title=Function/Practice/Functions_with_Python&amp;action=history"/>
	<updated>2026-11-23T23:21:41Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.46.0</generator>
	<entry>
		<id>https://wikibase.ronzz.org/index.php?title=Function/Practice/Functions_with_Python&amp;diff=18153&amp;oldid=prev</id>
		<title>Rongzhou at 10:26, 20 November 2026</title>
		<link rel="alternate" type="text/html" href="https://wikibase.ronzz.org/index.php?title=Function/Practice/Functions_with_Python&amp;diff=18153&amp;oldid=prev"/>
		<updated>2026-11-20T10:26:54Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;a href=&quot;https://wikibase.ronzz.org/index.php?title=Function/Practice/Functions_with_Python&amp;amp;diff=18153&amp;amp;oldid=18144&quot;&gt;Show changes&lt;/a&gt;</summary>
		<author><name>Rongzhou</name></author>
	</entry>
	<entry>
		<id>https://wikibase.ronzz.org/index.php?title=Function/Practice/Functions_with_Python&amp;diff=18144&amp;oldid=prev</id>
		<title>RonzzWikiCowriterAI: Create practice page with Python solutions to the Matlab handout exercises. AI-assisted (RonzzWikiCowriter) (via create-page on MediaWiki MCP Server)</title>
		<link rel="alternate" type="text/html" href="https://wikibase.ronzz.org/index.php?title=Function/Practice/Functions_with_Python&amp;diff=18144&amp;oldid=prev"/>
		<updated>2026-11-20T07:24:48Z</updated>

		<summary type="html">&lt;p&gt;Create practice page with Python solutions to the Matlab handout exercises. AI-assisted (RonzzWikiCowriter) (via create-page on MediaWiki MCP Server)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;The exercises below come from a practical-work handout written for Matlab by [[Person:Gautier List|M. Gautier List]]&amp;lt;ref&amp;gt;{{#cite:Q3739}}&amp;lt;/ref&amp;gt;, translated here and solved with NumPy and Matplotlib. They cover &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt; statements, user-defined [[Function|functions]], the trace of a matrix, parametric curves, the golden ratio, and the Fibonacci sequence.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
3 differences with Matlab are worth stating once:&lt;br /&gt;
&lt;br /&gt;
* a Python function is declared with &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt;, and its body is delimited by indentation instead of being closed by &amp;lt;code&amp;gt;end&amp;lt;/code&amp;gt;;&lt;br /&gt;
* inside an &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt; the equality test is &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt;, conjunction is &amp;lt;code&amp;gt;and&amp;lt;/code&amp;gt;, and disjunction is &amp;lt;code&amp;gt;or&amp;lt;/code&amp;gt;, where Matlab writes &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;&amp;amp;amp;&amp;amp;amp;&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;||&amp;lt;/code&amp;gt;;&lt;br /&gt;
* a snippet marked &amp;lt;code&amp;gt;class=&amp;quot;pyodide&amp;quot;&amp;lt;/code&amp;gt; below runs in the reader&amp;#039;s browser, so every program on this page can be executed in place.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Exercise 1 ==&lt;br /&gt;
&lt;br /&gt;
# What does the instruction &amp;lt;code&amp;gt;max(2, 3)&amp;lt;/code&amp;gt; return?&lt;br /&gt;
# Write a program that returns the maximum of 3 numbers $a$, $b$, and $c$, without using the function &amp;lt;code&amp;gt;max&amp;lt;/code&amp;gt;.&lt;br /&gt;
# Write the same thing again as a function $m=\mathrm{monmax}(a,b,c)$.&lt;br /&gt;
&lt;br /&gt;
{{Hidden solution}}&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Question 1.&amp;#039;&amp;#039;&amp;#039; &amp;lt;code&amp;gt;max&amp;lt;/code&amp;gt; of two scalars compares them one against the other and returns the larger, so&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot; class=&amp;quot;pyodide&amp;quot; copy&amp;gt;&lt;br /&gt;
print(max(2, 3))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Expected output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Python &amp;lt;code&amp;gt;max&amp;lt;/code&amp;gt; accepts either several arguments (&amp;lt;code&amp;gt;max(2, 3)&amp;lt;/code&amp;gt;) or one iterable (&amp;lt;code&amp;gt;max([2, 3])&amp;lt;/code&amp;gt;); in Matlab the same instruction is &amp;lt;code&amp;gt;max(2,3)&amp;lt;/code&amp;gt;, which also returns &amp;lt;code&amp;gt;3&amp;lt;/code&amp;gt;, since &amp;lt;code&amp;gt;max&amp;lt;/code&amp;gt; compares two arrays element by element.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Question 2.&amp;#039;&amp;#039;&amp;#039; 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.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot; class=&amp;quot;pyodide&amp;quot; copy&amp;gt;&lt;br /&gt;
a, b, c = 3, 7, 5                    # the 3 numbers to compare&lt;br /&gt;
&lt;br /&gt;
if a &amp;gt;= b and a &amp;gt;= c:                # a is at least as large as the 2 others&lt;br /&gt;
    m = a&lt;br /&gt;
elif b &amp;gt;= c:                         # otherwise b, if it beats c&lt;br /&gt;
    m = b&lt;br /&gt;
else:                                # otherwise c&lt;br /&gt;
    m = c&lt;br /&gt;
&lt;br /&gt;
print(f&amp;quot;the maximum of {a}, {b}, and {c} is {m}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Expected output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
the maximum of 3, 7, and 5 is 7&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Question 3.&amp;#039;&amp;#039;&amp;#039; 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.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot; class=&amp;quot;pyodide&amp;quot; copy&amp;gt;&lt;br /&gt;
import itertools&lt;br /&gt;
&lt;br /&gt;
def monmax(a, b, c):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Return the largest of three numbers, without max().&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    m = a&lt;br /&gt;
    if b &amp;gt; m:                        # strictly greater: a tie keeps the earlier value&lt;br /&gt;
        m = b&lt;br /&gt;
    if c &amp;gt; m:&lt;br /&gt;
        m = c&lt;br /&gt;
    return m&lt;br /&gt;
&lt;br /&gt;
print(monmax(3, 7, 5))&lt;br /&gt;
print(monmax(-1, -5, -3))&lt;br /&gt;
print(monmax(2, 2, 1))&lt;br /&gt;
print(monmax(0.5, -2, 0.25))&lt;br /&gt;
&lt;br /&gt;
mismatches = [(a, b, c)&lt;br /&gt;
              for a, b, c in itertools.product(range(-5, 6), repeat=3)&lt;br /&gt;
              if monmax(a, b, c) != max(a, b, c)]&lt;br /&gt;
print()&lt;br /&gt;
print(&amp;quot;triples tested:&amp;quot;, 11**3)&lt;br /&gt;
print(&amp;quot;mismatches:    &amp;quot;, len(mismatches))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Expected output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
7&lt;br /&gt;
-1&lt;br /&gt;
2&lt;br /&gt;
0.5&lt;br /&gt;
&lt;br /&gt;
triples tested: 1331&lt;br /&gt;
mismatches:     0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
The last lines check the function against the built-in &amp;lt;code&amp;gt;max&amp;lt;/code&amp;gt; on every triple of integers between $-5$ and $5$: the count of disagreements is $0$.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Caution: the assignment is &amp;lt;code&amp;gt;=&amp;lt;/code&amp;gt;, and the comparison inside an &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;==&amp;lt;/code&amp;gt;. Writing &amp;lt;code&amp;gt;if a = b:&amp;lt;/code&amp;gt; is a syntax error in Python, so the mistake is caught immediately, but &amp;lt;code&amp;gt;if a == b:&amp;lt;/code&amp;gt; where an assignment was meant is not.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{End hidden}}&lt;br /&gt;
&lt;br /&gt;
== Exercise 2 ==&lt;br /&gt;
&lt;br /&gt;
# Build a square $3\times3$ matrix of integers of your choice, named $A$.&lt;br /&gt;
# Test the commands &amp;lt;code&amp;gt;trace(A)&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;sum(diag(A))&amp;lt;/code&amp;gt;.&lt;br /&gt;
# Write a function $t=\mathrm{matrace}(A)$ that computes the trace of a square $3\times3$ matrix, without using the functions &amp;lt;code&amp;gt;trace&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;sum&amp;lt;/code&amp;gt;, or &amp;lt;code&amp;gt;diag&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Recall that the trace of a square matrix $A=(a_{ij})$ is the sum of its diagonal entries,&lt;br /&gt;
&lt;br /&gt;
$$&lt;br /&gt;
\operatorname{tr}(A)=\sum_{i=1}^{n}a_{ii}.&lt;br /&gt;
$$&lt;br /&gt;
&lt;br /&gt;
{{Hidden solution}}&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Questions 1 and 2.&amp;#039;&amp;#039;&amp;#039; &amp;lt;code&amp;gt;np.trace&amp;lt;/code&amp;gt; computes the trace directly, while &amp;lt;code&amp;gt;np.sum(np.diag(A))&amp;lt;/code&amp;gt; extracts the diagonal with &amp;lt;code&amp;gt;np.diag&amp;lt;/code&amp;gt; and then sums it: two routes to the same number.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot; class=&amp;quot;pyodide&amp;quot; copy&amp;gt;&lt;br /&gt;
import numpy as np&lt;br /&gt;
&lt;br /&gt;
A = np.array([[ 4, -1,  2],&lt;br /&gt;
              [ 0,  5,  3],&lt;br /&gt;
              [ 7,  2, -6]])&lt;br /&gt;
&lt;br /&gt;
print(&amp;quot;A =&amp;quot;)&lt;br /&gt;
print(A)&lt;br /&gt;
print()&lt;br /&gt;
print(&amp;quot;trace(A)     =&amp;quot;, np.trace(A))&lt;br /&gt;
print(&amp;quot;sum(diag(A)) =&amp;quot;, np.sum(np.diag(A)))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Expected output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
A =&lt;br /&gt;
[[ 4 -1  2]&lt;br /&gt;
 [ 0  5  3]&lt;br /&gt;
 [ 7  2 -6]]&lt;br /&gt;
&lt;br /&gt;
trace(A)     = 3&lt;br /&gt;
sum(diag(A)) = 3&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The diagonal entries are $4$, $5$, and $-6$, and $4+5-6=3$.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Question 3.&amp;#039;&amp;#039;&amp;#039; Walk down the diagonal with a single index and accumulate the entries in a variable initialised at $0$.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot; class=&amp;quot;pyodide&amp;quot; copy&amp;gt;&lt;br /&gt;
import numpy as np&lt;br /&gt;
&lt;br /&gt;
def matrace(A):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Trace of a square matrix, without trace(), sum(), or diag().&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    n = len(A)&lt;br /&gt;
    if any(len(row) != n for row in A):&lt;br /&gt;
        raise ValueError(&amp;quot;matrace expects a square matrix&amp;quot;)&lt;br /&gt;
    t = 0&lt;br /&gt;
    for i in range(n):&lt;br /&gt;
        t += A[i][i]                 # A[i][i] on a list of lists, or on an array&lt;br /&gt;
    return t&lt;br /&gt;
&lt;br /&gt;
print(matrace([[1, 2, 3],&lt;br /&gt;
               [4, 5, 6],&lt;br /&gt;
               [7, 8, 9]]))&lt;br /&gt;
print(matrace(np.array([[ 4, -1,  2],&lt;br /&gt;
                        [ 0,  5,  3],&lt;br /&gt;
                        [ 7,  2, -6]])))&lt;br /&gt;
print(matrace([[1, 2], [3, 4]]))&lt;br /&gt;
print(matrace([[7]]))&lt;br /&gt;
&lt;br /&gt;
try:&lt;br /&gt;
    matrace([[1, 2, 3], [4, 5, 6]])&lt;br /&gt;
except ValueError as err:&lt;br /&gt;
    print(&amp;quot;not square -&amp;gt;&amp;quot;, err)&lt;br /&gt;
&lt;br /&gt;
rng = np.random.default_rng(seed=0)&lt;br /&gt;
for size in (1, 2, 3, 5, 10):&lt;br /&gt;
    M = rng.integers(-9, 10, size=(size, size))&lt;br /&gt;
    print(f&amp;quot;{size}x{size}: matrace = {matrace(M):4d}   np.trace = {np.trace(M):4d}   &amp;quot;&lt;br /&gt;
          f&amp;quot;agree = {matrace(M) == np.trace(M)}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Expected output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
15&lt;br /&gt;
3&lt;br /&gt;
5&lt;br /&gt;
7&lt;br /&gt;
not square -&amp;gt; matrace expects a square matrix&lt;br /&gt;
1x1: matrace =    7   np.trace =    7   agree = True&lt;br /&gt;
2x2: matrace =   -1   np.trace =   -1   agree = True&lt;br /&gt;
3x3: matrace =   -1   np.trace =   -1   agree = True&lt;br /&gt;
5x5: matrace =   -2   np.trace =   -2   agree = True&lt;br /&gt;
10x10: matrace =   24   np.trace =   24   agree = True&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
The function is written for any size $n$ rather than for $3\times3$ alone, and for any container that supports &amp;lt;code&amp;gt;len&amp;lt;/code&amp;gt; and indexing: a list of lists as well as a NumPy array. It also refuses a rectangular matrix, where &amp;lt;code&amp;gt;np.trace&amp;lt;/code&amp;gt; happily sums $a_{00}$, $a_{11}$, and so on as far as the shape allows.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Careful with &amp;lt;code&amp;gt;np.diag&amp;lt;/code&amp;gt;: on a 2D array it extracts the diagonal, but on a 1D array it does the opposite and builds a diagonal matrix. Matlab&amp;#039;s &amp;lt;code&amp;gt;diag&amp;lt;/code&amp;gt; has the same two behaviours.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{End hidden}}&lt;br /&gt;
&lt;br /&gt;
== Exercise 3 ==&lt;br /&gt;
&lt;br /&gt;
Plot the following parametric curves.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;1.&amp;#039;&amp;#039;&amp;#039; A Lissajous curve,&lt;br /&gt;
&lt;br /&gt;
$$&lt;br /&gt;
\begin{cases}&lt;br /&gt;
x(t)=\sin(2t) \\&lt;br /&gt;
y(t)=\sin(3t)&lt;br /&gt;
\end{cases},\,-2\pi\le t\le2\pi&lt;br /&gt;
$$&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;2.&amp;#039;&amp;#039;&amp;#039; The same pair of sines, with a linear drift,&lt;br /&gt;
&lt;br /&gt;
$$&lt;br /&gt;
\begin{cases}&lt;br /&gt;
x(t)=t+\sin(2t) \\&lt;br /&gt;
y(t)=t+\sin(3t)&lt;br /&gt;
\end{cases},\,-4\pi\le t\le4\pi&lt;br /&gt;
$$&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;3.&amp;#039;&amp;#039;&amp;#039; A curve in space,&lt;br /&gt;
&lt;br /&gt;
$$&lt;br /&gt;
\begin{cases}&lt;br /&gt;
x(t)=t\cos(t) \\&lt;br /&gt;
y(t)=t\sin(t) \\&lt;br /&gt;
z(t)=t&lt;br /&gt;
\end{cases},\,-2\pi\le t\le2\pi&lt;br /&gt;
$$&lt;br /&gt;
&lt;br /&gt;
{{Hidden solution}}&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot; class=&amp;quot;pyodide&amp;quot; copy&amp;gt;&lt;br /&gt;
import numpy as np&lt;br /&gt;
import matplotlib.pyplot as plt&lt;br /&gt;
&lt;br /&gt;
fig = plt.figure(figsize=(13, 4.2))&lt;br /&gt;
&lt;br /&gt;
# 1. the Lissajous curve&lt;br /&gt;
t = np.linspace(-2 * np.pi, 2 * np.pi, 601)&lt;br /&gt;
ax = fig.add_subplot(1, 3, 1)&lt;br /&gt;
ax.plot(np.sin(2 * t), np.sin(3 * t), lw=1.2)&lt;br /&gt;
ax.set_aspect(&amp;quot;equal&amp;quot;)                       # same scale on both axes: a circle stays round&lt;br /&gt;
ax.set_title(r&amp;quot;$x=\sin(2t)$, $y=\sin(3t)$&amp;quot;)&lt;br /&gt;
ax.set_xlabel(&amp;quot;$x(t)$&amp;quot;)&lt;br /&gt;
ax.set_ylabel(&amp;quot;$y(t)$&amp;quot;)&lt;br /&gt;
ax.grid(alpha=0.3)&lt;br /&gt;
&lt;br /&gt;
# 2. the same, with a linear drift&lt;br /&gt;
t = np.linspace(-4 * np.pi, 4 * np.pi, 1201)&lt;br /&gt;
ax = fig.add_subplot(1, 3, 2)&lt;br /&gt;
ax.plot(t + np.sin(2 * t), t + np.sin(3 * t), lw=1.2)&lt;br /&gt;
ax.set_aspect(&amp;quot;equal&amp;quot;)&lt;br /&gt;
ax.set_title(r&amp;quot;$x=t+\sin(2t)$, $y=t+\sin(3t)$&amp;quot;)&lt;br /&gt;
ax.set_xlabel(&amp;quot;$x(t)$&amp;quot;)&lt;br /&gt;
ax.set_ylabel(&amp;quot;$y(t)$&amp;quot;)&lt;br /&gt;
ax.grid(alpha=0.3)&lt;br /&gt;
&lt;br /&gt;
# 3. a curve in space&lt;br /&gt;
t = np.linspace(-2 * np.pi, 2 * np.pi, 601)&lt;br /&gt;
ax = fig.add_subplot(1, 3, 3, projection=&amp;quot;3d&amp;quot;)&lt;br /&gt;
ax.plot(t * np.cos(t), t * np.sin(t), t, lw=1.2)&lt;br /&gt;
ax.set_title(r&amp;quot;$x=t\cos t$, $y=t\sin t$, $z=t$&amp;quot;)&lt;br /&gt;
ax.set_xlabel(&amp;quot;$x$&amp;quot;)&lt;br /&gt;
ax.set_ylabel(&amp;quot;$y$&amp;quot;)&lt;br /&gt;
ax.set_zlabel(&amp;quot;$z$&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
fig.tight_layout()&lt;br /&gt;
plt.show()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[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$.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
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 &amp;lt;code&amp;gt;set_aspect(&amp;quot;equal&amp;quot;)&amp;lt;/code&amp;gt; the figure is stretched by the shape of the axes, and a closed symmetric curve looks distorted.&lt;br /&gt;
&lt;br /&gt;
The third curve needs &amp;lt;code&amp;gt;projection=&amp;quot;3d&amp;quot;&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;add_subplot&amp;lt;/code&amp;gt;: its radius $\sqrt{x^{2}+y^{2}}=|t|$ grows with $|t|$, which draws a corkscrew opening outwards.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{End hidden}}&lt;br /&gt;
&lt;br /&gt;
== Problem: the golden ratio and the Fibonacci sequence ==&lt;br /&gt;
&lt;br /&gt;
The golden ratio is a proportion originally defined in geometry as the ratio of 2 lengths $a$ and $b$, noted $\varphi$, such that&lt;br /&gt;
&lt;br /&gt;
$$&lt;br /&gt;
\varphi=\frac{a}{b}=\frac{a+b}{a}&lt;br /&gt;
$$&lt;br /&gt;
&lt;br /&gt;
with $a&amp;gt;b&amp;gt;0$. It appears in painting, sculpture, and architecture, and in nature.&lt;br /&gt;
&lt;br /&gt;
=== 1. The equation satisfied by the golden ratio ===&lt;br /&gt;
&lt;br /&gt;
Show that $\varphi$ satisfies&lt;br /&gt;
&lt;br /&gt;
$$&lt;br /&gt;
\varphi^{2}-\varphi-1=0.&lt;br /&gt;
$$&lt;br /&gt;
&lt;br /&gt;
{{Hidden solution}}&lt;br /&gt;
&lt;br /&gt;
Start from the 2nd form of the ratio and split the fraction:&lt;br /&gt;
&lt;br /&gt;
$$&lt;br /&gt;
\varphi=\frac{a+b}{a}=1+\frac{b}{a}=1+\frac{1}{\varphi},&lt;br /&gt;
$$&lt;br /&gt;
&lt;br /&gt;
since $\dfrac{a}{b}=\varphi$ implies $\dfrac{b}{a}=\dfrac{1}{\varphi}$.&lt;br /&gt;
&lt;br /&gt;
Multiplying both sides by $\varphi\neq0$,&lt;br /&gt;
&lt;br /&gt;
$$&lt;br /&gt;
\varphi^{2}=\varphi+1,&lt;br /&gt;
$$&lt;br /&gt;
&lt;br /&gt;
that is,&lt;br /&gt;
&lt;br /&gt;
$$&lt;br /&gt;
\varphi^{2}-\varphi-1=0.&lt;br /&gt;
$$&lt;br /&gt;
{{End hidden}}&lt;br /&gt;
&lt;br /&gt;
=== 2. A function for the quadratic equation ===&lt;br /&gt;
&lt;br /&gt;
Write a function &amp;lt;code&amp;gt;degre2(a, b, c)&amp;lt;/code&amp;gt; that gives the solutions of&lt;br /&gt;
&lt;br /&gt;
$$&lt;br /&gt;
ax^{2}+bx+c=0.&lt;br /&gt;
$$&lt;br /&gt;
&lt;br /&gt;
{{Hidden solution}}&lt;br /&gt;
&lt;br /&gt;
The discriminant decides the number of real roots,&lt;br /&gt;
&lt;br /&gt;
$$&lt;br /&gt;
x_{1,2}=\frac{-b\pm\sqrt{\Delta}}{2a},\,\Delta=b^{2}-4ac.&lt;br /&gt;
$$&lt;br /&gt;
&lt;br /&gt;
Python has no real square root of a negative number, but &amp;lt;code&amp;gt;cmath.sqrt&amp;lt;/code&amp;gt; returns a complex one, so a single code path covers $\Delta&amp;gt;0$, $\Delta=0$, and $\Delta&amp;lt;0$.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot; class=&amp;quot;pyodide&amp;quot; copy&amp;gt;&lt;br /&gt;
import cmath&lt;br /&gt;
&lt;br /&gt;
def degre2(a, b, c):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Return the two roots of a*x**2 + b*x + c = 0.&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    if a == 0:&lt;br /&gt;
        raise ValueError(&amp;quot;a is zero: this is not a second-degree equation&amp;quot;)&lt;br /&gt;
    delta = b * b - 4 * a * c            # the discriminant&lt;br /&gt;
    root = cmath.sqrt(delta)             # complex square root: covers delta &amp;lt; 0 as well&lt;br /&gt;
    return (-b + root) / (2 * a), (-b - root) / (2 * a)&lt;br /&gt;
&lt;br /&gt;
for a, b, c in [(1, -1, -1), (1, -3, 2), (1, 0, 1), (2, 4, 2)]:&lt;br /&gt;
    print(f&amp;quot;a={a}, b={b}, c={c} -&amp;gt; {degre2(a, b, c)}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Expected output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
a=1, b=-1, c=-1 -&amp;gt; ((1.618033988749895+0j), (-0.6180339887498949+0j))&lt;br /&gt;
a=1, b=-3, c=2 -&amp;gt; ((2+0j), (1+0j))&lt;br /&gt;
a=1, b=0, c=1 -&amp;gt; (1j, -1j)&lt;br /&gt;
a=2, b=4, c=2 -&amp;gt; ((-1+0j), (-1+0j))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Because &amp;lt;code&amp;gt;cmath.sqrt&amp;lt;/code&amp;gt; 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 &amp;lt;code&amp;gt;math.sqrt&amp;lt;/code&amp;gt; when $\Delta\ge0$:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot; class=&amp;quot;pyodide&amp;quot; copy&amp;gt;&lt;br /&gt;
import math&lt;br /&gt;
&lt;br /&gt;
def degre2_reel(a, b, c):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Return the two roots, as floats when the discriminant is positive.&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    if a == 0:&lt;br /&gt;
        raise ValueError(&amp;quot;a is zero: this is not a second-degree equation&amp;quot;)&lt;br /&gt;
    delta = b * b - 4 * a * c&lt;br /&gt;
    if delta &amp;lt; 0:&lt;br /&gt;
        return None                    # no real root&lt;br /&gt;
    d = math.sqrt(delta)&lt;br /&gt;
    return (-b + d) / (2 * a), (-b - d) / (2 * a)&lt;br /&gt;
&lt;br /&gt;
print(degre2_reel(1, -1, -1))          # the golden ratio and its conjugate&lt;br /&gt;
print(degre2_reel(1, -3, 2))&lt;br /&gt;
print(degre2_reel(1, 0, 1))            # None: no real root&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(1.618033988749895, -0.6180339887498949)&lt;br /&gt;
(2.0, 1.0)&lt;br /&gt;
None&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
{{End hidden}}&lt;br /&gt;
&lt;br /&gt;
=== 3. The value of the golden ratio ===&lt;br /&gt;
&lt;br /&gt;
Deduce that&lt;br /&gt;
&lt;br /&gt;
$$&lt;br /&gt;
\varphi=\frac{1+\sqrt{5}}{2}\approx1.618.&lt;br /&gt;
$$&lt;br /&gt;
&lt;br /&gt;
{{Hidden solution}}&lt;br /&gt;
&lt;br /&gt;
Apply the function of question 2 to $a=1$, $b=-1$, $c=-1$:&lt;br /&gt;
&lt;br /&gt;
$$&lt;br /&gt;
x_{1,2}=\frac{1\pm\sqrt{1+4}}{2}=\frac{1\pm\sqrt{5}}{2}.&lt;br /&gt;
$$&lt;br /&gt;
&lt;br /&gt;
The 2 roots are therefore&lt;br /&gt;
&lt;br /&gt;
$$&lt;br /&gt;
\begin{cases}&lt;br /&gt;
x_1=\dfrac{1+\sqrt{5}}{2}\approx1.6180339887 \\&lt;br /&gt;
x_2=\dfrac{1-\sqrt{5}}{2}\approx-0.6180339887&lt;br /&gt;
\end{cases}&lt;br /&gt;
$$&lt;br /&gt;
&lt;br /&gt;
Since $a&amp;gt;b&amp;gt;0$, $\varphi=\dfrac{a}{b}&amp;gt;1$, which selects the positive root:&lt;br /&gt;
&lt;br /&gt;
$$&lt;br /&gt;
\varphi=\frac{1+\sqrt{5}}{2}\approx1.618&lt;br /&gt;
$$&lt;br /&gt;
&lt;br /&gt;
The other root is $-\dfrac{1}{\varphi}$, the conjugate golden ratio, whose product with $\varphi$ is $-1$, as Vieta&amp;#039;s relation $x_1x_2=\dfrac{c}{a}=-1$ requires.&lt;br /&gt;
&lt;br /&gt;
Sympy confirms the value exactly, and confirms that it cancels the polynomial of question 1:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot; class=&amp;quot;pyodide&amp;quot; copy&amp;gt;&lt;br /&gt;
import sympy as sp&lt;br /&gt;
&lt;br /&gt;
x = sp.symbols(&amp;quot;x&amp;quot;)&lt;br /&gt;
roots = sp.solve(x**2 - x - 1, x)        # x**2 - x - 1 = 0&lt;br /&gt;
print(&amp;quot;roots:&amp;quot;, roots)&lt;br /&gt;
&lt;br /&gt;
phi_exact = (1 + sp.sqrt(5)) / 2&lt;br /&gt;
print(&amp;quot;positive root:&amp;quot;, roots[1])&lt;br /&gt;
print(&amp;quot;difference from (1 + sqrt(5))/2:&amp;quot;, sp.simplify(roots[1] - phi_exact))&lt;br /&gt;
print(&amp;quot;phi^2 - phi - 1 =&amp;quot;, sp.simplify(phi_exact**2 - phi_exact - 1))&lt;br /&gt;
print(&amp;quot;phi =&amp;quot;, sp.N(phi_exact, 20))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Expected output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
roots: [1/2 - sqrt(5)/2, 1/2 + sqrt(5)/2]&lt;br /&gt;
positive root: 1/2 + sqrt(5)/2&lt;br /&gt;
difference from (1 + sqrt(5))/2: 0&lt;br /&gt;
phi^2 - phi - 1 = 0&lt;br /&gt;
phi = 1.6180339887498948482&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
{{End hidden}}&lt;br /&gt;
&lt;br /&gt;
=== 4. The Fibonacci sequence ===&lt;br /&gt;
&lt;br /&gt;
Let $F_n$ be the Fibonacci sequence defined by&lt;br /&gt;
&lt;br /&gt;
$$&lt;br /&gt;
F_{n+2}=F_{n+1}+F_n&lt;br /&gt;
$$&lt;br /&gt;
&lt;br /&gt;
with $F_1=F_2=1$. Write a program that gives the terms of the sequence up to a rank $N$, set at the beginning of the program, for instance $N=20$.&lt;br /&gt;
&lt;br /&gt;
{{Hidden solution}}&lt;br /&gt;
&lt;br /&gt;
Each new term needs the 2 previous ones, so the terms are accumulated in a list, which keeps them all available for questions 5 and 6.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot; class=&amp;quot;pyodide&amp;quot; copy&amp;gt;&lt;br /&gt;
N = 20&lt;br /&gt;
&lt;br /&gt;
F = [1, 1]                              # F[0] = F_1 = 1 and F[1] = F_2 = 1&lt;br /&gt;
for n in range(2, N):&lt;br /&gt;
    F.append(F[n - 1] + F[n - 2])       # F_{n+1} = F_n + F_{n-1}&lt;br /&gt;
&lt;br /&gt;
for n, f in enumerate(F, start=1):&lt;br /&gt;
    print(f&amp;quot;F_{n:&amp;lt;2} = {f}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Expected output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
F_1  = 1&lt;br /&gt;
F_2  = 1&lt;br /&gt;
F_3  = 2&lt;br /&gt;
F_4  = 3&lt;br /&gt;
F_5  = 5&lt;br /&gt;
F_6  = 8&lt;br /&gt;
F_7  = 13&lt;br /&gt;
F_8  = 21&lt;br /&gt;
F_9  = 34&lt;br /&gt;
F_10 = 55&lt;br /&gt;
F_11 = 89&lt;br /&gt;
F_12 = 144&lt;br /&gt;
F_13 = 233&lt;br /&gt;
F_14 = 377&lt;br /&gt;
F_15 = 610&lt;br /&gt;
F_16 = 987&lt;br /&gt;
F_17 = 1597&lt;br /&gt;
F_18 = 2584&lt;br /&gt;
F_19 = 4181&lt;br /&gt;
F_20 = 6765&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Python integers have no fixed width, so the loop does not overflow the way a Matlab &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; 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}$.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
{{End hidden}}&lt;br /&gt;
&lt;br /&gt;
=== 5. Numerical convergence of the ratios ===&lt;br /&gt;
&lt;br /&gt;
Verify numerically that the ratio $\dfrac{F_{n+1}}{F_n}$ tends to the golden ratio.&lt;br /&gt;
&lt;br /&gt;
{{Hidden solution}}&lt;br /&gt;
&lt;br /&gt;
Print the ratio, its distance to $\varphi$, and that distance multiplied by $\varphi^{2n}$:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot; class=&amp;quot;pyodide&amp;quot; copy&amp;gt;&lt;br /&gt;
import numpy as np&lt;br /&gt;
&lt;br /&gt;
phi = (1 + np.sqrt(5)) / 2&lt;br /&gt;
&lt;br /&gt;
print(f&amp;quot;{&amp;#039;n&amp;#039;:&amp;gt;3}  {&amp;#039;F_{n+1}/F_n&amp;#039;:&amp;gt;16}  {&amp;#039;|error|&amp;#039;:&amp;gt;10}  {&amp;#039;|error| phi^(2n)&amp;#039;:&amp;gt;17}&amp;quot;)&lt;br /&gt;
for n in range(1, N):&lt;br /&gt;
    r = F[n] / F[n - 1]                 # F_{n+1} / F_n&lt;br /&gt;
    err = abs(r - phi)&lt;br /&gt;
    print(f&amp;quot;{n:3d}  {r:16.12f}  {err:10.2e}  {err * phi**(2 * n):17.6f}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Expected output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  n       F_{n+1}/F_n     |error|   |error| phi^(2n)&lt;br /&gt;
  1    1.000000000000    6.18e-01           1.618034&lt;br /&gt;
  2    2.000000000000    3.82e-01           2.618034&lt;br /&gt;
  3    1.500000000000    1.18e-01           2.118034&lt;br /&gt;
  4    1.666666666667    4.86e-02           2.284701&lt;br /&gt;
  5    1.600000000000    1.80e-02           2.218034&lt;br /&gt;
  6    1.625000000000    6.97e-03           2.243034&lt;br /&gt;
  7    1.615384615385    2.65e-03           2.233419&lt;br /&gt;
  8    1.619047619048    1.01e-03           2.237082&lt;br /&gt;
  9    1.617647058824    3.87e-04           2.235681&lt;br /&gt;
 10    1.618181818182    1.48e-04           2.236216&lt;br /&gt;
 11    1.617977528090    5.65e-05           2.236012&lt;br /&gt;
 12    1.618055555556    2.16e-05           2.236090&lt;br /&gt;
 13    1.618025751073    8.24e-06           2.236060&lt;br /&gt;
 14    1.618037135279    3.15e-06           2.236071&lt;br /&gt;
 15    1.618032786885    1.20e-06           2.236067&lt;br /&gt;
 16    1.618034447822    4.59e-07           2.236068&lt;br /&gt;
 17    1.618033813400    1.75e-07           2.236068&lt;br /&gt;
 18    1.618034055728    6.70e-08           2.236068&lt;br /&gt;
 19    1.618033963167    2.56e-08           2.236068&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
$$&lt;br /&gt;
\left|\frac{F_{n+1}}{F_n}-\varphi\right|\approx\sqrt{5}\,\varphi^{-2n}.&lt;br /&gt;
$$&lt;br /&gt;
&lt;br /&gt;
That law follows from Binet&amp;#039;s formula,&lt;br /&gt;
&lt;br /&gt;
$$&lt;br /&gt;
F_n=\frac{\varphi^{n}-\psi^{n}}{\sqrt{5}},\,\psi=\frac{1-\sqrt{5}}{2}=-\frac{1}{\varphi},&lt;br /&gt;
$$&lt;br /&gt;
&lt;br /&gt;
from which, since $\varphi-\psi=\sqrt{5}$,&lt;br /&gt;
&lt;br /&gt;
$$&lt;br /&gt;
F_{n+1}-\varphi F_n=\frac{-\psi^{n+1}+\varphi\psi^{n}}{\sqrt{5}}=\frac{\psi^{n}\,(\varphi-\psi)}{\sqrt{5}}=\psi^{n},&lt;br /&gt;
$$&lt;br /&gt;
&lt;br /&gt;
so that&lt;br /&gt;
&lt;br /&gt;
$$&lt;br /&gt;
\frac{F_{n+1}}{F_n}-\varphi=\frac{\psi^{n}}{F_n}.&lt;br /&gt;
$$&lt;br /&gt;
&lt;br /&gt;
Since $\psi&amp;lt;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.&lt;br /&gt;
&lt;br /&gt;
[[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.]]&lt;br /&gt;
{{End hidden}}&lt;br /&gt;
&lt;br /&gt;
=== 6. The golden spiral ===&lt;br /&gt;
&lt;br /&gt;
The golden spiral, built on the proportions of the golden ratio, is approximated by the logarithmic spiral of polar equation&lt;br /&gt;
&lt;br /&gt;
$$&lt;br /&gt;
r(\theta)=Ae^{B\theta},\,B=\frac{\ln(\varphi)}{\pi/2},&lt;br /&gt;
$$&lt;br /&gt;
&lt;br /&gt;
or, as a parametric curve in Cartesian coordinates,&lt;br /&gt;
&lt;br /&gt;
$$&lt;br /&gt;
\begin{cases}&lt;br /&gt;
x=r(\theta)\cos(\theta) \\&lt;br /&gt;
y=r(\theta)\sin(\theta)&lt;br /&gt;
\end{cases}&lt;br /&gt;
$$&lt;br /&gt;
&lt;br /&gt;
Plot the golden spiral, choosing an arbitrary value for $A$.&lt;br /&gt;
&lt;br /&gt;
{{Hidden solution}}&lt;br /&gt;
&lt;br /&gt;
$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$,&lt;br /&gt;
&lt;br /&gt;
$$&lt;br /&gt;
\frac{r(\theta+\pi/2)}{r(\theta)}=e^{B\pi/2}=\varphi.&lt;br /&gt;
$$&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot; class=&amp;quot;pyodide&amp;quot; copy&amp;gt;&lt;br /&gt;
import numpy as np&lt;br /&gt;
import matplotlib.pyplot as plt&lt;br /&gt;
&lt;br /&gt;
phi = (1 + np.sqrt(5)) / 2&lt;br /&gt;
B = np.log(phi) / (np.pi / 2)            # quarter turn = one factor phi&lt;br /&gt;
A = 1.0                                  # arbitrary scale&lt;br /&gt;
&lt;br /&gt;
theta = np.linspace(0, 4 * np.pi, 800)&lt;br /&gt;
r = A * np.exp(B * theta)&lt;br /&gt;
&lt;br /&gt;
plt.figure(figsize=(6, 5.6))&lt;br /&gt;
plt.plot(r * np.cos(theta), r * np.sin(theta), lw=1.6, color=&amp;quot;darkgoldenrod&amp;quot;)&lt;br /&gt;
plt.plot([0], [0], &amp;quot;k+&amp;quot;, ms=12)          # the pole&lt;br /&gt;
for k in range(8):                       # one mark at each quarter turn&lt;br /&gt;
    turn = k * np.pi / 2&lt;br /&gt;
    plt.plot(A * np.exp(B * turn) * np.cos(turn),&lt;br /&gt;
             A * np.exp(B * turn) * np.sin(turn), &amp;quot;o&amp;quot;, ms=3, color=&amp;quot;black&amp;quot;)&lt;br /&gt;
plt.gca().set_aspect(&amp;quot;equal&amp;quot;)            # without this the spiral is not a spiral&lt;br /&gt;
plt.xlabel(&amp;quot;$x$&amp;quot;)&lt;br /&gt;
plt.ylabel(&amp;quot;$y$&amp;quot;)&lt;br /&gt;
plt.title(r&amp;quot;Golden spiral, $r(\theta)=Ae^{B\theta}$&amp;quot;)&lt;br /&gt;
plt.grid(alpha=0.3)&lt;br /&gt;
plt.show()&lt;br /&gt;
&lt;br /&gt;
print(&amp;quot;one quarter turn multiplies r by&amp;quot;, np.exp(B * np.pi / 2))&lt;br /&gt;
print(&amp;quot;two whole turns multiply r by    &amp;quot;, np.exp(B * 4 * np.pi))&lt;br /&gt;
print(&amp;quot;phi**8 =                         &amp;quot;, phi**8)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Expected output:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
one quarter turn multiplies r by 1.618033988749895&lt;br /&gt;
two whole turns multiply r by     46.97871376374778&lt;br /&gt;
phi**8 =                          46.978713763747805&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[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.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
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. &amp;lt;code&amp;gt;set_aspect(&amp;quot;equal&amp;quot;)&amp;lt;/code&amp;gt; keeps the circles centred on the origin circular: without it, the spiral looks elliptical.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
{{End hidden}}&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Function]]&lt;/div&gt;</summary>
		<author><name>RonzzWikiCowriterAI</name></author>
	</entry>
</feed>