Jump to content

Ordinary derivative/Practice/Calculation and plotting with Python: Difference between revisions

From Wikibase
No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
Reference: [[Ordinary derivative#Formal definition|Formal definition]]
The four exercises below come from a practical-work handout written for Matlab created by my beloved teacher [[Person:Gautier List|M. Gautier List]]<ref>{{#cite:Q3733}}</ref>, which we solve here with NumPy and Matplotlib.
The four exercises below come from a practical-work handout written for Matlab created by my beloved teacher [[Person:Gautier List|M. Gautier List]]<ref>{{#cite:Q3733}}</ref>, which we solve here with NumPy and Matplotlib.



Latest revision as of 06:08, 20 November 2026

The four 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 Matplotlib.

Exercise 1

Plot, on the same graph, with legends, a title, and symbols, the two Gaussian functions on $[-3,3]$:

$$ y_1(x)=\frac{\exp\left(-0.25x^{2}\right)}{\sqrt{2\pi}} $$

$$ y_2(x)=\frac{\exp\left(-0.5x^{2}\right)}{\sqrt{2\pi}} $$

Exercise 2

Plot, on the same graph, with legends, a title, and symbols, the four functions:

  1. (a) $\exp(-0.5x^{2})/\sqrt{2\pi}$
  2. (b) $\sin(x)$
  3. (c) $\cos(x)$
  4. (d) $|x|$

Exercise 3

Determine numerically the derivative of $\sin(x)$ for the steps $h=0.5$, $h=0.1$, and $h=0.01$, then compare the errors with the exact solution $\cos(x)$.

Reminder, from the definition of the derivative:

$$ \frac{df}{dx}=f'(x)=\lim_{h\to0}\frac{f(x+h)-f(x)}{h} $$

The limit is approximated by a finite $h\ll1$, which may be taken forward, backward, or centred:

$$ \begin{cases} f'(x)\approx\dfrac{f(x+h)-f(x)}{h} \\ f'(x)\approx\dfrac{f(x)-f(x-h)}{h} \\ f'(x)\approx\dfrac{f(x+h)-f(x-h)}{2h} \end{cases} $$

Exercise 4

Let $f$ be the function

$$ f(x)=2x^{3}+4x^{2}-3x+1 $$

for $-2.8\le x\le2.8$. Write a script that:

  1. builds the vectors $X$ and $Y$ holding $x$ and $y=f(x)$ with a step $h=0.1$;
  2. plots the curve $y=f(x)$ from $X$ and $Y$;
  3. computes numerically, by several methods, the first derivative of $f$;
  4. computes numerically, by several methods, the second derivative of $f$;
  5. plots, on one graph, the first derivatives of 3 against the exact first derivative;
  6. plots, on one graph, the second derivatives of 4 against the exact second derivative.

References