Worksheet 2
Workspace Setup
Ensure your MATLAB path is set to
<INSTALLDIR>/i2sc_worksheets/Worksheet2
where <INSTALLDIR> is the directory where you extracted the course content (see the Getting Started page)
1. Interpolation
Complete the MATLAB script
\[y=a x+b\]problem_1_1.mto fit and plot a straight line through the points \((1,3)\) and \((2,4)\). The equation for a straight line is given byso you will need to set-up and solve an appropriate linear system. This Resource sheet gives you more information on the system you will need to solve. When plotting, consider formatting your graph appropriately. An example final plot could look something like:
Example solution to problem 1.1 Complete the MATLAB script
\[y=dx^2+ex+f\]problem_1_2.mto fit and plot (on the same graph) both a linear equation and a quadratic equation through the points \((1,3)\), \((2,4)\) and \((1.5,4)\). The quadratic equation is given byWhen plotting, consider formatting your graph appropriately. An example final plot could look something like:
Example solution to problem 1.2 Complete the MATLAB script
problem_1_3.mand use the polyfit function to fit a quadratic curve to a set of historic airline passenger data (the data file is stored in the data subfolder, i.e.data/passengerdata.mat). Plot the known data points (i.e. those in the file) and the quadratic fit, and format your graph appropriately. An example final plot could look something like:Example solution to problem 1.3
2. Numerical Integration
A definite integral of a function \(f(x)\) between the values of \(x=a\) and \(x=b\) is defined as:
\[\int_a^b f(x) dx\]Numerically, the method for evaluating this integral is to split the interval into \(N\) sections and work out the area of each section and add them together. It is common to use trapeziums as the sections, so:
\[\int_a^b f(x) dx \approx \sum_{i=1}^N \Delta x \frac{f(x_{i-1})+f(x_{i})}{2}\]The Task
Complete the following tasks in the supplied file problem_2.m
Calculate the solution of the following integral by hand
\[\int_0^{10} \left(2+0.05x^2+\sin(x) \right) dx\]Write a MATLAB function with the signature
val = calcMyInt(N)in the filecalcMyInt.mthat takes an input of \(N\) and evaluates the above integral using the trapezium method. Where \(N\) is the number of trapeziums to split the integral up into. Cap \(N\) to a value of 1000Use this function to plot how the value of the numerical integral varies as \(N\) is changed between 2 and 1000
Determine the value of \(N\) required to evaluate the integral numerically to within 0.001% of the true value.
3. Estimate the Value of \(\pi\)
The value of \(\pi\) can be estimated by a Monte-Carlo approach. In this, imagine a circle of radius \(r\), that fits perfectly within a square with sides \(2r\) in length. By randomly `throwing darts’ at the square and counting how many also fall in the circle, an estimate of \(\pi\) is generated. Figure shows this and gives the relationship for how to calculate \(\pi\) given the ratio of the number of darts in the circle to the total number of darts thrown (it is assumed a dart always lands in the square).
\[\begin{aligned} \frac{A_{circ}}{A_{square}} &= \frac{\pi r^2}{(2r)^2} = \frac{\pi}{4} \\ \therefore \pi &\approx 4\frac{N_{circ}}{N_{square}} \end{aligned}\]The Task
Complete a MATLAB script problem_3.m to estimate the value of \(\pi\):
- Ensure the script stops when the estimate reaches a suitable degree of accuracy (the error between the estimate and real value is within a desired tolerance, perhaps 0.1%) HINT: use a while loop
- Plot a log-scale graph of the error against the number of darts thrown
4. Estimate the Area of a Circle
Consider a square area with the side length \(a = 10 cm\). Create a square grid of \(N\times N\) elements covering the area of interest - Figure . Organise a loop checking whether the centre of each element, \((x_c,y_c)\), is inside or outside the circle with the radius \(R<a/2\). Use the following criterion
\[x_c^2 + y_c^2 \le R^2\]Complete the following tasks in the supplied file problem_4.m
- Write a MATALB function with the signature
[A,err] = estimateArea(N)to calculate the total area of all the elements attributed to the circle and the error against the theoretical analytical solution, for aNsquares per axis. - Calculate the required
Nto have a maximum error of 0.001% - Plot a log-scale graph of the error against the number elements up to an
Nof 1000