Physics 281 - Computational Physics

Wednesday/Friday Section

Fall 2009

Exercise 6 - Interpolation

Assignment

In this exercise, we will create a script to make interpolations of a tabulated function. Your script should:

1. Set up necessary arrays:

2. Now write the rest of the script to make an interpolation of the y array at the values of the independent variables in the tt array. Use the linear formula for the interpolation.

Note: for this step, I want you to write your own linear interpolation. Do not use the special MATLAB functions which do the same thing! However, you are welcome to use that function to check your program.

Hint: The trick here is to create a relationship between the indices of the t, y and tt arrays. Then use a for loop to iterate through the tt array and calculate the interpolated value. For example, the elements 1-11 of the tt array have a values that fall in between the values of the first and second elements of the t and y arrays. The next 11 elements (11-21) of tt fall between the second and third element of t and y, and so on. A relationship between indices will allow you to correctly select the correct elements in the table of values y to do the interpolation.

Hint 2: A useful function is the floor function. This function takes a real number and rounds it down to the nearest integer. Thus, z = floor(5.3) would set z equal to the integer 5.

3. Finally, create a second interpolated array using splines. For this you may use the MATLAB spline function.

4. Plot the original set of points and overlay the two interpolated arrays on the plot for comparison. Export the final plot as a jpeg image.

The name of the program file should be something like exercise6.m so that it can be distinguished from other submissions. Please also name the jpeg file something like exercise_6_plot.jpg so that I can identify it.

Please write your name in a comment field near the beginning of the script.

Copy your completed program and jpeg file into the directory ~schloerb/ph281/username, where username is your user name. See this link for examples of how to copy a file to this directory.

Go to Ph281 Home

Go to Ph281 Topics