MAS2008 Scientific Computing: Lab 3
Plotting with Matplotlib


The following notebooks and videos are relevant for this lab:
The bisection method View Download
The Bessel function $J_2(x)$ View Download
Tour of matplotlib View Download
Parametric and implicit plots View Download
Prime numbers View Download
Statistics plots View Download
Stirling's approximation for $n!$ View Download

Task 1: Graphs of two functions

Write Python code to plot the graphs of the functions $f(x)=x(1-x)$ and $g(x)=x(1-x)(1-2x)$ from $x=0$ to $x=1$. When you are sure that your code is correct, you should enter it in the online test on Moodle. (If you have set up your environment in a nonstandard way, you may be used to calling to display the plot. You should not do this in the code that you enter in test system.)

Task 2: Lissajous curves

Write a Python function plot_lissajous(n, m) to plot the Lissajous curve $(x, y)=(\sin(n t),\cos(m t))$ for $0\leq t\leq 2\pi$. You should again enter your code (without in the online test on Moodle.

Task 3: An implicit plot

Make an implicit plot of the curve where $$ x^2 + y^2 + 20 (\sin(2\pi x) + \cos(2\pi y)) = 100, $$ with $-11\leq x,y\leq 11$. To get a good picture you will need to start with a fine grid, of size $1000\times 1000$ say. You could also add a dotted circle of radius $10$ in a different colour. The picture should look a bit bizarre; for extra credit you can try to explain it. (This task is not part of the online test.)

Task 4: Illustrating the bisection method

Download the notebook bisect.ipynb. It defines a function bisect(f, a, b) which searches for a root of $f(x)$ in the interval $[a,b]$ (by a rather mediocre method). It returns a list cs of approximate roots. Write a function show_bisect(f, a, b) which calls bisect(f, a, b) and then generates a plot as follows: Test your code with $f(x)=10\cos(x)-\cos(10x)$ and $[a,b]=[0,\pi]$ and in some other examples of your choice. You can also critique and improve the function bisect(f, a, b). I do not suggest that you change the basic method, but various other things could be done better. (None of this task is on the online test.)