# MAS2008 Scientific Computing: Lab 3 Plotting with Matplotlib

## Instructions

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$.
• The axes should be labelled $x$ and $y$ respectively.
• There should be a legend labelling the two graphs as "f(x)" and "g(x)" respectively.
• The two graphs should be thicker than the default thickness.
• The two graphs should be in different colours.
• The plot should have a title: "Two functions".
• Each graph should have at least 500 points.
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 plt.show() to display the plot. You should not do this in the code that you enter in test system.)

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$.
• Your should use the figsize option to make the plot have size $5\times 5$.
• Your plot should show the region where $-2\leq x,y\leq 2$ (i.e. $-2\leq x\leq 2$ and $-2\leq y\leq 2$).
• You should show the curve on its own without any axes, title or other extraneous elements.
• The curve should be shown as a dashed line, brick red in colour (hex code aa4a44)
• You should generate at least 500 points on the curve.
You should again enter your code (without plt.show()) 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:
• Plot $f(x)$ for $a\leq x\leq b$ in blue.
• Draw the horizontal line $y=0$ in black.
• For each $c$ in the list of approximate roots, draw a dotted line from $(c,0)$ to the corresponding point on the graph. Make the line red if $f(c)\gt 0$ and green if $f(c)\lt 0$.
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.)