0% found this document useful (0 votes)
7 views3 pages

Week2 Tutorial

The Week 2 tutorial for AMME2000 and BMET2960/9960 focuses on numerical methods in engineering, specifically interpolation of data sets using MATLAB. Students will practice linear and spline interpolation techniques on a given data set, learn to plot the results, and debug their code. The tutorial emphasizes understanding the mathematical foundations of interpolation and the importance of accurate data representation in engineering applications.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Week2 Tutorial

The Week 2 tutorial for AMME2000 and BMET2960/9960 focuses on numerical methods in engineering, specifically interpolation of data sets using MATLAB. Students will practice linear and spline interpolation techniques on a given data set, learn to plot the results, and debug their code. The tutorial emphasizes understanding the mathematical foundations of interpolation and the importance of accurate data representation in engineering applications.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Week 2 Tutorial

1. INTRODUCTION
AMME2000 and BMET2960/9960 are designed to teach you the fundamentals of numerical methods in
engineering. “Numerical methods” is a big field, but it boils down to finding approximate solutions to
complex problems. When a complex real-world problem is modelled mathematically, very often there
will be integrals and/or derivatives involved. Therefore, a key part of numerical methods is computing
approximate solutions for areas and gradients (Fig. 1).

Fig. 1. A set of sampled data points for which we may want to know the area (left) or local gradients
(right).

However, there are two problems. To get more accurate estimates of area, we need to know the
mathematical form of the curve between points so that we can form narrower ‘strips’ to sum (Fig. 2, left).
The second problem is that individual points such as those in Fig. 1 (right) do not have a gradient! Recall,
calculus requires continuity in the vicinity of the point. To achieve this continuity, we require the
mathematical form of the curve between points and also that this curve is ‘smooth’ (Fig. 2, right).
Computing such a curve through a set of data points is called interpolation (black curve in Fig. 2).

Fig. 2. Interpolated data points (black curve) to compute area (left) and local gradients (right).

DISCUSS: What types of interpolation have you seen before? What did you use interpolation for?

The aim of the Week 2 tutorial is to continue practicing MATLAB in the context of interpolating a simple
data set. Then, in Week 3, we’ll learn how to compute areas and gradients using simple but accurate
numerical approaches.
2. DATA SET
Consider the following data set which we’ll use for this tutorial:

𝒙𝒙 𝒚𝒚
0.0 4.3
2.0 2.7
4.0 0.85
6.0 0.12
8.0 -0.36
10.0 -1.3
12.0 -3.9
14.0 -5.4
16.0 -5.1
18.0 -3.1
20.0 -1.9
22.0 -0.65
24.0 -0.81
26.0 -1.9
28.0 2.8
30.0 3.1

DISCUSS: On your tables, look up how different data sets can be incorporated into MATLAB. What are
some of the methods? What are some important considerations when loading a data set into MATLAB
(or any software)?

TASK 1: The data set above is small enough that you can copy or enter it directly into MATLAB. Using
the MATLAB tutorial2.m template script provided, copy across the data set to create a 2D array (one
dimension for the 𝑥𝑥 values and one dimension for the 𝑦𝑦 values).

TASK 2: Now update your script to plot the data as a series of black circles. How would you adjust your
plot command to plot a red line through all the points instead? (For information on using the MATLAB
plot function, type “help plot” in the MATLAB command line.)

3. LINEAR INTERPOLATION
Now we’ll attempt to linearly interpolate our data set.

DISCUSS: If we use linear interpolation for our data set:


- What will the interpolated curve look like?
- How many interpolates will we have?

TASK 3: On your tables, review the linear_interp function distributed as part of the Tutorial 2
materials:
• How many inputs does the function have? What are they?
• How many outputs does the function have? What are they?
• What does the function do?
• How does the ‘n’ input affect the function output?
• How could this function be improved?

TASK 4: In your main script, call the linear_interp function for the first two data points in the set and
obtain 11 evenly spaced interpolates inclusive of the endpoints.
It’s always good to be skeptical of your code and to assume there are bugs until you have checked that
there are not. A simple but powerful way to debug is to check the values of variables at certain points to
make sure they have the value, size and type that you expect.

TASK 5: Practice debugging by doing the following:


• Insert a break point in the linear_interp function, at the line where 𝑦𝑦 is calculated
• Run your main script and notice how program execution pauses at the break point
• ‘Step over’ the calculation and check the value of idata
• DISCUSS: What are two different ways you could check the value of this variable?

TASK 6: Now it’s time to fix the bug we’ve identified. On your tables:
• Identify the cause of the bug
• Fix the bug
• Re-run the code to check the value of idata

TASK 7: Earlier you plotted the original data points as black circles. Now plot the interpolates you’ve
computed for the first two data points in red and over the top of your existing plot. Which MATLAB
command helps you to overplot easily?

TASK 8: Now repeat the interpolation for the next segment (from point 2 to point 3) and again overplot
the interpolates on the original plot in a different colour.

Clearly, you could continue this process and progressively compute the interpolates for each of the 15
segments. However, this is not very efficient – and it would not be practical if you had, for example,
thousands of data points.

DISCUSS: On your tables, discuss how you could generate the interpolated data more efficiently for
the multiple segments, even allowing for the number of interpolates per segment to be easily updated.
• What programming structure will you use?
• How are you going to store all the interpolation results?

TASK 9: Implement the code you’ve discussed so that your script now computes the interpolates for
all segments at once. Overlay all of the interpolates on your original plot of the data points.

Whenever we present a plot in an assignment or industry report, it must be interpretable on its own.

DISCUSS: What should you add to your plot so that it can be properly interpreted?

TASK 10: Assuming the 𝑥𝑥 values refer to distance in metres and the 𝑦𝑦 values refer to temperature in
Kelvin, complete the plot then export it as an image file (which you could import into another doc).

4. SPLINE INTERPOLATION

DISCUSS: What do you recall about spline interpolation? In what situations is it useful?

TASK 11: Look up the documentation on the MATLAB spline function. Adapt your MATLAB script to
compute cubic spline interpolates as well as the linear interpolates. Plot them together and use a
legend to distinguish the two interpolation methods.

5. FURTHER PRACTICE OF INTERPOLATION & FITTING


For the last part of the tutorial, work through the Week 1 Practice Problems 1-5.

You might also like