Clear Filters
Clear Filters

Nearest Interpolation in Matlab?

2 views (last 30 days)
Nick Haufler
Nick Haufler on 18 Feb 2016
Answered: John D'Errico on 18 Feb 2016
For the attached document can I get any confirmation as to whether my interpolation is correct or not. Should I be getting a negative number like I did or something else?

Answers (2)

the cyclist
the cyclist on 18 Feb 2016
I think the -6cm/sec is correct, but I don't understand where the 3cm came from (and I think it is wrong). I'd give that a more careful look.

John D'Errico
John D'Errico on 18 Feb 2016
No. Linear interpolation between data points that are entirely positive, can NEVER yield a value that is negative. PERIOD. So you made a mistake.
In your title, it says something about nearest, so that too is in question, since nearest neighbor interpolation would also never yield negative numbers.
Since you did do some work there (even though there is NO MATLAB content here) I'll offer a better result.
@x = 6, we have y = 10
@x = 6.5, we have y = 7
So in MATLAB, I would do this:
t = (6.15 - 6)/(6.5 - 6);
y = 10*(1-t) + 7*t
y =
9.1
There the interpolation was written in the form of what one might call a convex linear combination of the function between those two endpoints.
Alternatively, we might choose to use a scheme that does interpolation by putting a line between those points. Effectively, the slope of the line is delta_y/delta_x, so using the point/slope form for a line, we can write the line as:
y(x) = y0 + (delta_y/delta_x)*(x-x0)
where the line is known to pass through the point (x0,y0).
10 + (7 - 10)/(6.5 - 6)*(6.15 - 6)
ans =
9.1
So that agrees with my first answer. Just lucky I guess. Or, we could do it as a function in MATLAB, since there must be some MATLAB content.
y_x = @(x) 10 + (7 - 10)/(6.5 - 6)*(x - 6);
y_x(6.15)
ans =
9.1
Whew! Again, the same answer. So I must be on a hot streak.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!