File Exchange

image thumbnail

Linear Interpolation

version (1.29 KB) by Judah S
This program calculate approximated value of x at a certain value of t using linear interpolation.


Updated 18 Dec 2010

View License

If we have a given function based on two vectors, parameter and resultant, then we can find the resultant of a different parameter if it is unknown.

Cite As

Judah S (2021). Linear Interpolation (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (7)

Neo Lee

deepak sheoran

do you have any interpolation code followed by least squares estimates?


not a very good program

Sean de

Yes, but this is not what you've done! You have a hardwired function. That means the user has to change things in the function and understand the poorly commented and written code! What advantage does this have over INTERP1?

INTERP1 offers all sorts of variety of interpolation, is vectorized, allows for using more than just the two adjacent points, allows for interpolating at more than one point at a time, is a stock MATLAB function so _everyone_ who uses MATLAB has it.

I'm sorry to be harsh and I realize this is your first contribution (keep trying if you develop new things!); but there is absolutely no reason or benefit from your function here that I can see. If there is, please provide a detailed description. It does not demonstrate a good interpolation scheme, good programming practice, or anything new/revolutionary.

John D'Errico

Sigh. The author is the one who fails to make sense. He hardwires a function INSIDE the code. So you need to edit this poor code to provide your own. Defining a vector x prior to use of this function does nothing, since this is a FUNCTION. The author does not understand that a function workspace cannot see data that is defined outside of it.

Of course, since the well written, well documented matlab copde interp1 exists already, this submission is totally useless.

How about the code itself? Also poor. It presumes that your vector t is presorted in increasing order, yet fails to state that as a requirement. No test is made to verify this fact. In fact, no error checks are done at all.

Is the code fast? Not really, since it is not vectorized. In fact, it cannot even interpolate more than one point at a time. Try it.

>> t = 0:.1:1;
>> l_intp(t,[.42 .43])
??? Error using ==> gt
Matrix dimensions must agree.

Error in ==> l_intp at 15

Beyond that, the author makes use of inefficient methods to determine which interval the point lies in, so even if he did vectorize it by use of a loop, the find would be quite inefficient.

Judah S

This code doesn't need the function for operation. If you don't have the function then define a vector x prior to run it.
Makes sense?

Sean de

This function has a hardwired function that you provide x-data and a point you care about. Then it only the two adjacent points to the point you care about using the data recovered from running the function on the original data. The function is right there!! Why wouldn't you just evaluate it at the point you care about?

Needless to say this function is useless and it not a good example of mathematics or how to program in MATLAB. Take a look at INTERP1 and friends.

MATLAB Release Compatibility
Created with R2010a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!