Interpolation

Your Progress in this Chapter

0%

0 / 10 units completed

Chapter Overview

Linear Interpolation

Section 3.1 of 520 min8 code examples

Linear Interpolation

Linear Interpolation is the simplest form of interpolation used to estimate a value between two known data points. It assumes that the change between the points follows a straight line. While :math:Polyfit seeks to find a single curve for an entire dataset, linear interpolation works "locally" between adjacent coordinates.

1. The Mathematical Formula

Given two points (x_0, y_0) and (x_1, y_1), the value :math:y for any :math:x in the interval [x_0, x_1] is calculated as:

y = y_0 + (x - x_0) \cfrac{y_1 - y_0}{x_1 - x_0}

This formula represents the weighted average of the two endpoints based on how close :math:x is to either side.

2. Implementation in SepalSolver

In SepalSolver, the Interp1 method performs 1D linear interpolation. It first searches the input array to find the correct "bin" (the two closest :math:x values) and then applies the linear formula.

Example 1C#

1
2
3
4
5
6
7
8
9
Code is ready to run
OutputFrom the book
Interpolated value at 5 is 50

Examples

.. Admonition:: Example 1 : Reading Steam Tables

Engineers often use tables of data where values are only provided at fixed intervals (e.g., every 5 degrees). Linear interpolation allows for the estimation of properties at specific, non-tabulated temperatures.

Example 2C#

1
2
3
4
5
6
7
Code is ready to run
OutputFrom the book
Estimated pressure at 107.5C: :math:132.05 kPa

.. Admonition:: Example 2 : Lookup Tables in Control Systems

In real-time control, complex mathematical functions are often replaced by "lookup tables" to save processing power. Linear interpolation provides a fast way to get reasonably accurate intermediate values from these tables.

Example 3C#

1
2
3
4
5
6
7
Code is ready to run
OutputFrom the book
Torque at 2500 RPM: :math:`295` Nm

Exercise: Manual Implementation

Task: Implement the core logic of a linear interpolator between two specific points.

Example 4C#

1
2
3
4
5
6
7
8
9
10
11
12
Code is ready to run

2D Linear Interpolation (Interp2)

2D Interpolation, or Bilinear Interpolation, is used to estimate values of a function z = f(x, y) when the data is provided as a grid. This is essential in fields like meteorology (mapping temperatures across a geographic area) or structural engineering (calculating stress over a surface).

1. How it Works

Bilinear interpolation is essentially a two-step linear interpolation. To find the value at a point (x, y) inside a rectangular cell defined by four points:

  1. Interpolate along X: Perform linear interpolation between the points in the x-direction to find two intermediate values at the target x coordinate.
  1. Interpolate along Y: Perform a final linear interpolation between those two intermediate values in the y-direction to find the result z.

2. Data Structure

In SepalSolver, Interp2 requires: double[] X: A vector of coordinates for the columns. double[] Y: A vector of coordinates for the rows. * double[,] Z: A 2D matrix where Z[i, j] is the value at (X[j], Y[i]).

Example 5C#

1
2
3
4
5
6
7
8
9
10
11
12
13
Code is ready to run
OutputFrom the book
Value at (0.5, 0.5): 25

Examples

.. Admonition:: Example 1 : Topographic Height Estimation

Imagine you have a grid of elevation data from a survey. You want to know the height at a specific coordinate that wasn't directly measured.

Example 6C#

1
2
3
4
5
6
7
8
9
10
11
Code is ready to run
OutputFrom the book
Height at (100.5, 50.5): :math:537.5 m

.. Admonition:: Example 2 : Pressure Mapping

In aerodynamics, sensors on a wing provide pressure data at specific (x, y) points. Interp2 allows the solver to reconstruct the pressure distribution across the entire surface for lift calculations.

Example 7C#

1
2
3
4
5
6
7
8
9
10
11
Code is ready to run
OutputFrom the book
Interpolated Z: 50

.. Admonition:: Example 3 : Reading steam table

In thermodynamics, the properties of water and steam (such as Enthalpy h, Entropy s, or Specific Volume v) are determined by two independent state variables, typically Pressure (P) and Temperature (T).

these properties are governed by complex non-linear physics, they are often provided in massive, discrete grids known as Steam Tables.When an engineer needs the enthalpy at a specific state that isn't exactly in the table, they must use Bilinear Interpolation (Interp2).

Example 8C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Code is ready to run
OutputFrom the book
Specific Volume of superheated water at T = 1350, P = 0.0373 is 20.413475