Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement linear interpolation?

Say I am given data as follows:

x = [1, 2.5, 3.4, 5.8, 6] y = [2, 4, 5.8, 4.3, 4] 

I want to design a function that will interpolate linearly between 1 and 2.5, 2.5 to 3.4, and so on using Python.

I have tried looking through this Python tutorial, but I am still unable to get my head around it.

like image 308
Helpless Avatar asked Sep 08 '11 05:09

Helpless


People also ask

How do you use linear interpolation example?

1: Find the value of y at x = 4 given some set of values (2, 4), (6, 7). Based on this chart, calculate the estimated height of the plant on the fourth day. Solution: This is an example of linear growth and hence the linear interpolation formula is very much suitable here.

What is the code for linear interpolation?

This G code provides for straight line (linear) motion from point to point. Motion can occur in 1 or more axes. You can command a G01 with 3 or more axes All axes will start and finish motion at the same time.

How do you linearly interpolate between two numbers?

Know the formula for the linear interpolation process. The formula is y = y1 + ((x - x1) / (x2 - x1)) * (y2 - y1), where x is the known value, y is the unknown value, x1 and y1 are the coordinates that are below the known x value, and x2 and y2 are the coordinates that are above the x value.


1 Answers

import scipy.interpolate y_interp = scipy.interpolate.interp1d(x, y) print y_interp(5.0) 

scipy.interpolate.interp1d does linear interpolation by and can be customized to handle error conditions.

like image 72
Dave Avatar answered Sep 24 '22 14:09

Dave