Jump to content

Function/Practice/Functions with Python

From Wikibase
Revision as of 07:24, 20 November 2026 by RonzzWikiCowriterAI (talk | contribs) (Create practice page with Python solutions to the Matlab handout exercises. AI-assisted (RonzzWikiCowriter) (via create-page on MediaWiki MCP Server))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Languages: English · français · Esperanto

The exercises below come from a practical-work handout written for Matlab by M. Gautier List[1], translated here and solved with NumPy and Matplotlib. They cover if statements, user-defined functions, the trace of a matrix, parametric curves, the golden ratio, and the Fibonacci sequence.

3 differences with Matlab are worth stating once:

  • a Python function is declared with def, and its body is delimited by indentation instead of being closed by end;
  • inside an if the equality test is ==, conjunction is and, and disjunction is or, where Matlab writes ==, &&, and ||;
  • a snippet marked class="pyodide" below runs in the reader's browser, so every program on this page can be executed in place.

Exercise 1

  1. What does the instruction max(2, 3) return?
  2. Write a program that returns the maximum of 3 numbers $a$, $b$, and $c$, without using the function max.
  3. Write the same thing again as a function $m=\mathrm{monmax}(a,b,c)$.

Exercise 2

  1. Build a square $3\times3$ matrix of integers of your choice, named $A$.
  2. Test the commands trace(A) and sum(diag(A)).
  3. Write a function $t=\mathrm{matrace}(A)$ that computes the trace of a square $3\times3$ 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}. $$

Exercise 3

Plot the following parametric curves.

1. A Lissajous curve,

$$ \begin{cases} x(t)=\sin(2t) \\ y(t)=\sin(3t) \end{cases},\,-2\pi\le t\le2\pi $$

2. The same pair of sines, with a linear drift,

$$ \begin{cases} x(t)=t+\sin(2t) \\ y(t)=t+\sin(3t) \end{cases},\,-4\pi\le t\le4\pi $$

3. A curve in space,

$$ \begin{cases} x(t)=t\cos(t) \\ y(t)=t\sin(t) \\ z(t)=t \end{cases},\,-2\pi\le t\le2\pi $$

Problem: 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$. It appears in painting, sculpture, and architecture, and in nature.

1. The equation satisfied by the golden ratio

Show that $\varphi$ satisfies

$$ \varphi^{2}-\varphi-1=0. $$

2. A function for the quadratic equation

Write a function degre2(a, b, c) that gives the solutions of

$$ ax^{2}+bx+c=0. $$

3. The value of the golden ratio

Deduce that

$$ \varphi=\frac{1+\sqrt{5}}{2}\approx1.618. $$

4. The Fibonacci sequence

Let $F_n$ be the Fibonacci sequence defined by

$$ F_{n+2}=F_{n+1}+F_n $$

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$.

5. Numerical convergence of the ratios

Verify numerically that the ratio $\dfrac{F_{n+1}}{F_n}$ tends to the golden ratio.

6. The golden spiral

The golden spiral, built on the proportions of the golden ratio, is approximated by the logarithmic spiral of polar equation

$$ r(\theta)=Ae^{B\theta},\,B=\frac{\ln(\varphi)}{\pi/2}, $$

or, as a parametric curve in Cartesian coordinates,

$$ \begin{cases} x=r(\theta)\cos(\theta) \\ y=r(\theta)\sin(\theta) \end{cases} $$

Plot the golden spiral, choosing an arbitrary value for $A$.

References