-2

Interpolate the following data into a smooth curve in the interval ∈ [−2 4.9]. Plot the actual data and the interpolated curve on the same figure.

x| -2 -1.7 -1.4 -1.1 -0.8 -0.5 -0.2 0.1 0.4 0.7 1 1.3
y| 0.1029 0.1174 0.1316 0.1448 0.1566 0.1662 0.1733 0.1775 0.1785 0.1764 0.1711 0.1630

x| 1.6 1.9 2.2 2.5 2.8 3.1 3.4 3.7 4 4.3 4.6 4.9
y| 0.1526 0.1402 0.1266 0.1122 0.0977 0.0835 0.0702 0.0579 0.0469 0.0373 0.0291 0.0224

My current code is...but I'm having trouble doing the actual interpolated curve,would it help putting this data into an excel sheet? First time dealing with this.

close all
clear all
clc

x = [-2, -1.7, -1.4, -1.1, -0.8, -0.5, -0.2, 0.1, 0.4, 0.7, 1, 1.3, 1.6, 1.9, 2.2, 2.5, 2.8, 3.1, 3.4, 3.7, 4, 4.3, 4.6, 4.9];
y = [0.1029, 0.1174, 0.1316, 0.1448, 0.1566, 0.1662, 0.1733, 0.1775, 0.1785, 0.1764, 0.1711, 0.1630, 0.1526, 0.1402, 0.1266, 0.1122, 0.0977, 0.0835, 0.0702, 0.0579, 0.0469, 0.0373, 0.0291, 0.0224];
plot(x,y)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Stan-Lee
  • 59
  • 1
  • 6
  • 3
    So you haven't done anything yet and want us to do this homework question for you? A Google search for "matlab interpolation" gives this as the first result: https://au.mathworks.com/help/matlab/ref/interp1.html. Stackoverflow is for programming problems that you are stuck with *after reaserching yourself*. – David Nov 21 '14 at 00:32
  • You really think I didn't researched it, I looked all over my textbook and online, this was my only resort, not even my classmates have any idea. Don't be accusing me of anything. All I was asking was for some guidance not accusations. – Stan-Lee Nov 21 '14 at 00:35
  • If you are trying to do something with Matlab and you are stuck, the first step should almost always be to Google "Matlab" and what you are trying to do. – David Nov 21 '14 at 00:37
  • That was the first thing I did, I couldn't find an example with an x and y, data points. I had no idea how to interpolate it. If you're not going to help why are you commenting, all I wanted was hints or a starting point, not the answer. – Stan-Lee Nov 21 '14 at 00:41
  • 2
    If you read the documentation for `interp1`, which I linked in my first comment, you will find examples of exactly what you are trying to do. – David Nov 21 '14 at 00:42

1 Answers1

0

This post that I just recently answered pretty much solves your problem. However, I'll throw you a bone and help you out. Use interp1 to do this interpolation for you. What you do is you specify the (x,y) control points, which are those points that are part of your dataset, then you specify a slew of other x points, which have more granularity and have values in between those values of the original x values that you originally specified, and you can see what the output is.

Here are the steps you'd need to perform to do this interpolation:

  1. Define your (x,y) points (which is what you have already done).
  2. Define a grid of x points that are further granular in comparison to what you have. You would specify x values that are in between the original x values that you have. Use linspace to generate a set number of points in between a minimum and maximum... which in your case is -2 and 4.9 respectively. We can hide this away by using min and max on your x points so that you can adapt this to any dataset you want to use.
  3. Use interp1 with the grid of points in step (2) and find the interpolated y points.
  4. Plot the points.

What I'm going to do is do the above procedure, then produce a plot where I'll draw what the original points that were given, as well as a dashed line that shows the interpolated curve. I'm also going to use spline interpolation to smooth out any irregularities.

As such:

%// Define your points - Step #1
x = [-2 -1.7 -1.4 -1.1 -0.8 -0.5 -0.2 0.1 0.4 0.7 1 1.3 1.6 1.9 2.2 2.5 2.8 3.1 3.4 3.7 4 4.3 4.6 4.9];
y = [0.1029 0.1174 0.1316 0.1448 0.1566 0.1662 0.1733 0.1775 0.1785 0.1764 0.1711 0.1630 0.1526 0.1402 0.1266 0.1122 0.0977 0.0835 0.0702 0.0579 0.0469 0.0373 0.0291 0.0224];

%// Step #2 - Define grid of x points - Define 100 points in between min and max
xval = linspace(min(x), max(x), 100);

%// Step #3 - Use interp1
yval = interp1(x, y, xval, 'spline');

%// Step #4 - Plot the stuff
plot(x, y, 'bo', xval, yval, 'r--');

%// Throw in a grid too
grid;

This is the plot I get:

enter image description here

Cool?

Community
  • 1
  • 1
rayryeng
  • 102,964
  • 22
  • 184
  • 193