# Tech Blog

## Function Interpolation with known

piecewise averages

### Function interpolation with known piecewise averages

## Overview

In a typical interpolation problem, values are estimated within a discrete set of known data points. Recently, we had to “interpolate” a function from which we only knew the averages at certain time intervals. A set of known data points was not available and regular interpolation techniques could not be applied. This post explains the solution that we implemented by way of an example.

## Solution outline for function interpolation with known piecewise averages

The example used to illustrate the interpolation methodology is based on a small photovoltaic power plant. The hourly average generation for a given day is shown in Table 1.

Hour of the day | Average generation (kW) |
---|---|

8:00-9:00 | 0.5 |

9:00-10:00 | 2.0 |

10:00-11:00 | 5.0 |

11:00-12:00 | 8.0 |

12:00-13:00 | 10.0 |

13:00-14:00 | 10.0 |

14:00-15:00 | 9.5 |

15:00-16:00 | 8.5 |

16:00-17:00 | 7.0 |

17:00-18:00 | 5.0 |

18:00-19:00 | 2.0 |

The target function is the power plant’s production curve on a minute-by-minute resolution. Note that the measured power curve is only available on an hourly basis.

The subsequent steps are followed to interpolate the power generation data:

- Calculate the cumulative function. This is equivalent to cumulative generation in our example (see Table 2). Note that 1 kWh is the energy generated by a power plant producing 1 kW during 1 hour. For example, the total generation until 10am was 2.5kWh (0.5kWh generated between 8:00 and 9:00 and 2.0kWh generated between 9:00 and 10:00am).
- Interpolate the cumulative function. Standard interpolation techniques can be applied to the cumulative function as a set of data points are known. For example, we know that the cumulative generation equals 0.5kWh at 9:00.
- Differentiate the interpolated cumulative function to obtain the target function. As the cumulative function is the integral (or sum) of the target function, the target function is the derivative of the cumulative function.

Time of the day | Cumulative generation (kWh) |
---|---|

8:00 | 0 |

9:00 | 0.5 |

10:00 | 2.5 |

11:00 | 7.5 |

12:00 | 15.5 |

13:00 | 25.5 |

14:00 | 35.5 |

15:00 | 45.0 |

16:00 | 53.5 |

17:00 | 60.5 |

18:00 | 65.5 |

19:00 | 67.5 |

A third-order spline interpolation was applied to the solar PV cumulative values in Table 2. Given that a third order Spline interpolation is twice differentiable, its derivative, the target function of our example, is continuous and differentiable. The result for the target function compared to the original hour-by-hour data can be seen in the figure below.

## Example code implementation in Python

An example code implementation in Python is as follows.

```
# 0. Start by importing Numpy and interpolate from Scipy
import numpy as np
from scipy import interpolate
# 1. Get cumulative function
cumulative_generation = np.array([0, 0.5, 2.5, 7.5, 15.5, 25.5, 35.5, 45, 53.5, 60.5, 65.5, 67.5])
hours = np.arange(8, 20)
# 2. Interpolate the cumulative function (Spline interpolation)
interpolator = interpolate.CubicSpline(hours, cumulative_generation)
# 3. Differentiate and substitute.
# For example, differentiate and interpolate the power at 10am:
print(interpolator(10, 1))
# 3.37 is the output of the interpolation at 10am.
```

## What next

Check out some of our other blogs. At Evergreen Innovations , we have extensive experience in data analytics and numerical analysis. If you have any questions or there any data analytics topic that you would like us to cover, just contact us.