Interpolation calculator: linear, spline, pchip and makima
Paste x, y data and the points you want, and get the values MATLAB's interp1 would give: linear, pchip, makima, spline, nearest, previous or next. It shows what happens outside the data, writes the text file and the MATLAB and Python code, and checks a pasted script for the mistakes found in 822 student reports. Free, runs in your browser.
MATLAB interp1 / spline / makima / fprintf docs · NumPy + SciPy docs · Moler, Numerical Computing with MATLAB · Updated 20 Nov 2026
It opens on the course file VI.xlsx (24 voltages and currents) and the challenge range. Try the 15 voltages the challenge lists: -17.2 -17.1 -13.8 -13.5 -13 -12.5 -11 -9 -5.76 -6 -1 0.11 0.25 0.5 0.61.
It opens on a typical report script. The checker reads the code as text and uses the data in the Interpolate tab for its numbers; it does not run the code. Nothing you paste leaves your browser.
- Interpolating…
Values
| V | I | note |
|---|
Text file
V I -17.300 -28.000000 -17.200 -26.000000 -17.100 -24.000000 -17.000 -19.000000 -16.900 -16.500000 -16.800 -14.000000 -16.700 -12.333333 -16.600 -10.666667 -16.500 -9.000000 -16.400 -8.400000 -16.300 -7.800000 -16.200 -7.200000 -16.100 -6.600000 -16.000 -6.000000 -15.900 -5.800000 -15.800 -5.600000 -15.700 -5.400000 -15.600 -5.200000 -15.500 -5.000000 -15.400 -4.800000 -15.300 -4.600000 -15.200 -4.400000 -15.100 -4.200000 -15.000 -4.000000 -14.900 -3.900000 -14.800 -3.800000 -14.700 -3.700000 -14.600 -3.600000 -14.500 -3.500000 -14.400 -3.400000 -14.300 -3.300000 -14.200 -3.200000 -14.100 -3.100000 -14.000 -3.000000 -13.900 -2.950000 -13.800 -2.900000 -13.700 -2.850000 -13.600 -2.800000 -13.500 -2.750000 -13.400 -2.700000 -13.300 -2.650000 -13.200 -2.600000 -13.100 -2.550000 -13.000 -2.500000 -12.900 -2.450000 -12.800 -2.400000 -12.700 -2.350000 -12.600 -2.300000 -12.500 -2.250000 -12.400 -2.200000 -12.300 -2.150000 -12.200 -2.100000 -12.100 -2.050000 -12.000 -2.000000 -11.900 -1.965000 -11.800 -1.930000 -11.700 -1.895000 -11.600 -1.860000 -11.500 -1.825000 -11.400 -1.790000 -11.300 -1.755000 -11.200 -1.720000 -11.100 -1.685000 -11.000 -1.650000 -10.900 -1.615000 -10.800 -1.580000 -10.700 -1.545000 -10.600 -1.510000 -10.500 -1.475000 -10.400 -1.440000 -10.300 -1.405000 -10.200 -1.370000 -10.100 -1.335000 -10.000 -1.300000 -9.900 -1.285000 -9.800 -1.270000 -9.700 -1.255000 -9.600 -1.240000 -9.500 -1.225000 -9.400 -1.210000 -9.300 -1.195000 -9.200 -1.180000 -9.100 -1.165000 -9.000 -1.150000 -8.900 -1.135000 -8.800 -1.120000 -8.700 -1.105000 -8.600 -1.090000 -8.500 -1.075000 -8.400 -1.060000 -8.300 -1.045000 -8.200 -1.030000 -8.100 -1.015000 -8.000 -1.000000 -7.900 -0.986667 -7.800 -0.973333 -7.700 -0.960000 -7.600 -0.946667 -7.500 -0.933333 -7.400 -0.920000 -7.300 -0.906667 -7.200 -0.893333 -7.100 -0.880000 -7.000 -0.866667 -6.900 -0.853333 -6.800 -0.840000 -6.700 -0.826667 -6.600 -0.813333 -6.500 -0.800000 -6.400 -0.786667 -6.300 -0.773333 -6.200 -0.760000 -6.100 -0.746667 -6.000 -0.733333 -5.900 -0.720000 -5.800 -0.706667 -5.700 -0.693333 -5.600 -0.680000 -5.500 -0.666667 -5.400 -0.653333 -5.300 -0.640000 -5.200 -0.626667 -5.100 -0.613333 -5.000 -0.600000 -4.900 -0.590000 -4.800 -0.580000 -4.700 -0.570000 -4.600 -0.560000 -4.500 -0.550000 -4.400 -0.540000 -4.300 -0.530000 -4.200 -0.520000 -4.100 -0.510000 -4.000 -0.500000 -3.900 -0.490000 -3.800 -0.480000 -3.700 -0.470000 -3.600 -0.460000 -3.500 -0.450000 -3.400 -0.440000 -3.300 -0.430000 -3.200 -0.420000 -3.100 -0.410000 -3.000 -0.400000 -2.900 -0.390000 -2.800 -0.380000 -2.700 -0.370000 -2.600 -0.360000 -2.500 -0.350000 -2.400 -0.340000 -2.300 -0.330000 -2.200 -0.320000 -2.100 -0.310000 -2.000 -0.300000 -1.900 -0.288889 -1.800 -0.277778 -1.700 -0.266667 -1.600 -0.255556 -1.500 -0.244444 -1.400 -0.233333 -1.300 -0.222222 -1.200 -0.211111 -1.100 -0.200000 -1.000 -0.188889 -0.900 -0.177778 -0.800 -0.166667 -0.700 -0.155556 -0.600 -0.144444 -0.500 -0.133333 -0.400 -0.122222 -0.300 -0.111111 -0.200 -0.100000 -0.100 -0.065517 0.000 -0.031034 0.100 0.000000 0.200 0.000000 0.300 0.002500 0.400 0.015000 0.500 0.170000 0.600 1.230000 0.700 NaN 0.800 NaN 0.900 NaN
MATLAB code
data = readmatrix('VI.xlsx'); % skips the header row; xlsread is 'not recommended' since R2019a
V = data(:,1); I = data(:,2); % column vectors
Vq = -17.3:0.1:0.9; % a row vector, 183 points
Iq = interp1(V, I, Vq, 'linear'); % outside the data: NaN (MATLAB default for this method)
figure; plot(V, I, 'o', Vq, Iq, '.-'); grid on;
xlabel('V'); ylabel('I'); legend('data', 'linear', 'Location', 'northwest');
% data tips, zoom and pan work in the figure window (datacursormode on)
fid = fopen('interpolated.txt', 'w');
fprintf(fid, '%8s %12s\n', 'V', 'I');
fprintf(fid, '%8.3f %12.6f\n', [Vq(:)'; Iq(:)']); % 2 rows -> one line per point
fclose(fid);Python code
import numpy as np
import pandas as pd
d = pd.read_excel("VI.xlsx") # header row becomes the column names
V, I = d.iloc[:, 0].to_numpy(float), d.iloc[:, 1].to_numpy(float)
Vq = np.linspace(-17.3, 0.9, 183) # = -17.3:0.1:0.9; np.arange(-17.3, 0.9, step) would stop one point short
Iq = np.interp(Vq, V, I, left=np.nan, right=np.nan) # without left/right numpy repeats the end values
np.savetxt("interpolated.txt", np.column_stack([Vq, Iq]), fmt="%8.3f %12.6f", header="V I", comments="")What is the Data Handling and Interpolation project?
It is the closing MATLAB project of Skill-Lync's MATLAB, Python and CFD for Mechanical Engineers course: "In a laboratory experiment, data of voltage and current has been recorded ... Read the excel file in the Matlab program and perform the interpolation in the range of -17.3:0.1:0.9. Generate the interactive plot for the interpolated data and store them in a text file." Most versions also ask for the current at 15 given voltages. Skill-Lync read all 822 public reports on 20 Nov 2026; 454 include their MATLAB code as text, the rest as screenshots.
The file VI.xlsx has 24 rows, voltage from −17.3 to 0.65 V and current from −28 to 3.91 mA. It has the shape of a diode's I-V curve: almost no current from about −14 V to 0.4 V, a sharp forward rise past 0.5 V, and a breakdown in reverse below about −16 V. That shape is what makes the choice of method matter: long flat stretches next to steep ones. The range −17.3:0.1:0.9 is 183 points, 0.1 V apart, and its last three (0.7, 0.8 and 0.9 V) lie past the last measurement.
Linear, pchip, makima or spline: which interp1 method?
Linear interpolation, MATLAB's default, joins neighbouring points with a straight line:
The three cubic methods all build a cubic on each interval from the two end values and a slope d at each data point; they differ only in how they choose the slopes:
- spline picks the slopes so the second derivative is continuous too, with 'not-a-knot' ends (the first two and last two intervals are one cubic). Smoothest, but every point affects every interval, so it overshoots near sudden changes.
- pchip (piecewise cubic Hermite) uses a weighted harmonic mean of the neighbouring slopes, and a zero slope wherever the data turn or go flat. It never overshoots on monotonic stretches:
- makima (modified Akima) weights the two neighbouring slopes by how much the slope changes on each side. MathWorks' description: the modification "gives priority to the side that is closer to horizontal, which is more intuitive and avoids overshoot."
- nearest, previous, next return a data value as it is: the closest point, the one to the left or the one to the right. Useful for lookup tables of steps, not for smooth data. At an exact midpoint this tool takes the left point, as SciPy's kind='nearest' does; MATLAB's documentation does not state its rule.
Here are the 15 voltages the challenge asks for, by method. The spline column matches the values MATLAB printed in the reports to the 6 decimals shown:
| V (Volt) | linear | pchip | makima | spline | nearest |
|---|---|---|---|---|---|
| -17.2 | -26.0000 | -26.7500 | -26.6755 | -28.1734 | -24 |
| -17.1 | -24.0000 | -24.0000 | -24.0000 | -24.0000 | -24 |
| -13.8 | -2.9000 | -2.8673 | -2.8829 | -2.8682 | -3 |
| -13.5 | -2.7500 | -2.6876 | -2.7134 | -2.6889 | -3 |
| -13 | -2.5000 | -2.4299 | -2.4491 | -2.4298 | -3 |
| -12.5 | -2.2500 | -2.2072 | -2.2102 | -2.2058 | -2 |
| -11 | -1.6500 | -1.5996 | -1.6009 | -1.6103 | -2 |
| -9 | -1.1500 | -1.1329 | -1.1361 | -1.1164 | -1.3 |
| -5.76 | -0.7013 | -0.6921 | -0.6926 | -0.7105 | -0.6 |
| -6 | -0.7333 | -0.7230 | -0.7239 | -0.7445 | -0.6 |
| -1 | -0.1889 | -0.2099 | -0.2012 | -0.2906 | -0.1 |
| 0.11 | 0.0000 | 0.0000 | 0.0000 | 0.0012 | 0 |
| 0.25 | 0.0000 | 0.0000 | 0.0000 | -0.0001 | 0 |
| 0.5 | 0.1700 | 0.1700 | 0.1700 | 0.1700 | 0.17 |
| 0.61 | 1.6100 | 1.5530 | 1.5466 | 1.5665 | 1.23 |
Away from the steep parts linear, pchip, makima and spline agree to within 0.07 mA. They disagree at −17.2 V (between the two most negative points: linear -26.0000, spline -28.1734, below both neighbours), at −1 V (linear -0.1889, spline -0.2906) and at 0.61 V on the forward rise. In the flat region, where the data are exactly 0 from 0.09 to 0.27 V, linear, pchip and makima return 0 at 0.11 and 0.25 V; spline returns 0.001150 and -0.000137 mA, a small negative current where none was measured. For measured data like this, pchip or makima is the safer smooth choice; linear is fine when you only need values between points and the step is small.
Why spline overshoots in the breakdown region
Between −17.3 V (−28 mA) and −17.1 V (−24 mA) a straight line stays between −28 and −24. The spline dips to -28.8501 mA at −17.25 V, 0.8501 mA below the lowest neighbour, because its not-a-knot end makes the first two intervals one cubic, and the steep drop from −17.1 V towards −16.8 V bends it. pchip and makima stay inside every interval on this data (the verifier checks 39 points per interval):
What happens past 0.65 V: NaN or extrapolation
The default depends on the method. MathWorks' interp1 page: 'pchip', 'spline' and 'makima' extrapolate by default, and "All other interpolation methods return NaN by default for query points outside the domain." So on −17.3:0.1:0.9:
| V (Volt) | linear | linear 'extrap' | pchip | makima | spline | np.interp |
|---|---|---|---|---|---|---|
| 0.7 | NaN | 7.11 | 8.854 | 6.648 | 10.04 | 3.91 |
| 0.8 | NaN | 13.51 | 20.75 | -13.35 | 39.07 | 3.91 |
| 0.9 | NaN | 19.91 | 26.94 | -111.2 | 101.1 | 3.91 |
Three methods, three answers at 0.9 V: 26.94, -111.2 and 101.1 mA, and a negative current for a forward-biased diode from makima. None was measured. Seven reports print the spline's 101.1 mA at 0.9 V in their output. Extrapolated values are a guess about how the curve continues, so stop the range at the last measurement, or keep the asked range and write NaN past it (interp1(V, I, Vq, 'pchip', NaN)), and say which you did.
The shaded band is past the last data point; dashed lines are extrapolated. The spline leaves the top of the chart before 0.9 V.
Writing the text file: the fprintf trap
MathWorks: fprintf "applies the formatSpec to all elements of arrays A1,...An in column order." So fprintf(fid, '%f %f\n', xq, yq) does not print xq(1) yq(1) on line 1. It walks through all of xq first, two numbers per line, then all of yq. One report's file shows it. Its lines run "−17.300000 −17.200000" (two voltages) down to the middle, then:
0.900000 -28.000000 -26.000000 -24.000000 -19.000000 -16.500000
0.9 V paired with −28 mA, the current at −17.3 V. Putting the two side by side first (z = [xq yq]) gives the same file. What works is a matrix whose columns are the lines of the file:
fprintf(fid, '%8.3f %12.6f\n', [xq(:)'; yq(:)']); % 2 rows: one xq, yq pair per line % or writematrix([xq(:) yq(:)], 'interpolated.txt', 'Delimiter', 'tab');
x(:)' makes a row whatever x was. That matters because readmatrix and xlsread give columns (V = data(:,1)), while a colon range like −17.3:0.1:0.9 is a row, and interp1 returns the shape of its query points.
What goes wrong in the reports
Of the 454 reports with code as text:
- NaN at the end (244 reports). Linear interp1 over −17.3:0.1:0.9 leaves 0.7, 0.8 and 0.9 V as NaN in the plot (a gap) and the text file. 14 of these reports mention NaN.
- Silent extrapolation (197). 'spline' or 'pchip' over the same range returns numbers past 0.65 V. One of these reports uses the word extrapolation.
- A scrambled text file (49). fprintf with two separate vectors (26) or a side-by-side matrix (23), as above. Only 30 build the 2-row matrix.
- fopen without fclose (47). The file stays open; MATLAB may not have written the last lines to disk until it closes it.
- %d for currents (60). %d is an integer conversion; for −0.7333 MATLAB "may override the specified conversion". Use %f or %g.
- xlsread (339) and full paths (24). xlsread still works, but MathWorks has marked it "not recommended" since R2019a in favour of readmatrix, readtable or readcell. A path like C:\Users\...\VI.xlsx runs on one computer only.
- 2-D functions for 1-D data (12). meshgrid, interp2 and surf copied from MATLAB's interp2 example. V-I data has one current per voltage, so interp1 is the function.
The "interactive plot" part needs no extra code: a MATLAB figure window already has data tips, zoom and pan (datacursormode on switches data tips on from a script). 5 reports use datacursormode, ginput or UI controls. The Check tab flags all of the above in a pasted script.
A script that avoids all of these
readmatrix, pchip with NaN past the data, the 2-row fprintf and fclose. The Interpolate tab writes this for any method and range you pick.
data = readmatrix('VI.xlsx'); % skips the header row; xlsread is 'not recommended' since R2019a
V = data(:,1); I = data(:,2); % column vectors
Vq = -17.3:0.1:0.9; % a row vector, 183 points
Iq = interp1(V, I, Vq, 'pchip', NaN);
figure; plot(V, I, 'o', Vq, Iq, '.-'); grid on;
xlabel('V'); ylabel('I'); legend('data', 'pchip', 'Location', 'northwest');
% data tips, zoom and pan work in the figure window (datacursormode on)
fid = fopen('interpolated.txt', 'w');
fprintf(fid, '%8s %12s\n', 'V', 'I');
fprintf(fid, '%8.3f %12.6f\n', [Vq(:)'; Iq(:)']); % 2 rows -> one line per point
fclose(fid);The same in Python. numpy.interp is linear only, and it repeats the end values outside the data (it returns 3.91 at 0.9 V) unless you pass left and right; NumPy's docs also advise np.linspace over np.arange for a step like 0.1, and np.arange(−17.3, 0.9, 0.1) gives 182 points ending at 0.8:
import numpy as np
import pandas as pd
from scipy.interpolate import PchipInterpolator
d = pd.read_excel("VI.xlsx") # header row becomes the column names
V, I = d.iloc[:, 0].to_numpy(float), d.iloc[:, 1].to_numpy(float)
Vq = np.linspace(-17.3, 0.9, 183) # = -17.3:0.1:0.9; np.arange(-17.3, 0.9, step) would stop one point short
f = PchipInterpolator(V, I)
Iq = f(Vq)
Iq[(Vq < V[0]) | (Vq > V[-1])] = np.nan
np.savetxt("interpolated.txt", np.column_stack([Vq, Iq]), fmt="%8.3f %12.6f", header="V I", comments="")What this calculator does not cover
- Curve fitting. Interpolation passes through every point, noise included. To fit a smooth law through noisy data (least squares), use the curve fitting calculator.
- 2-D and scattered data. interp2, interp3, griddata and scatteredInterpolant are not modelled.
- MATLAB's 'cubic' and 'v5cubic'. Since R2020b they are cubic convolution, not pchip as in older releases and many tutorials; they are not in this tool.
- Errors in the data. Every method trusts every point. A typing slip in VI.xlsx (one report's copy has +9 instead of −9 mA at −16.5 V) goes straight into every interpolated value near it.
- The code check is pattern matching. It reads the text, so unusual variable names or code split across files can hide a mistake or flag a false one.
Reading test data, interpolating it and writing it out is the first step of every simulation and test-data job. It is a project in Skill-Lync's MATLAB and Python courses.
Frequently asked questions
How do you do linear interpolation?
Find the two data points either side of x, then go the same fraction of the way between their y values: y = y1 + (y2 − y1)(x − x1)/(x2 − x1). In the course data, −13.8 V lies between −14 V (−3 mA) and −12 V (−2 mA), a tenth of the way along, so I = −3 + 0.1 × 1 = -2.9 mA. MATLAB's interp1(V, I, -13.8) gives the same.
What is the difference between linear, spline and pchip interpolation?
Linear joins the points with straight lines. Spline fits one smooth cubic through all of them (continuous second derivative), so it can swing past the data: on the course V-I data it dips 0.8501 mA below the lowest neighbouring point near -17.25 V. pchip is a cubic too, but it chooses the slopes so it never overshoots and stays flat where the data are flat. makima sits in between: smooth and with fewer wiggles than spline.
What does interp1 return outside the data range?
For 'linear', 'nearest', 'next' and 'previous', NaN. For 'pchip', 'spline' and 'makima', MATLAB extends the end cubic and returns a number by default. The course data stop at 0.65 V, so at 0.9 V linear gives NaN, while pchip gives 26.94 mA, makima -111.2 mA and spline 101.1 mA. None of them was measured.
Why does my interp1 output have NaN values?
Because some query points are outside the sample points and the method is linear (or nearest, next, previous). Here, -17.3:0.1:0.9 runs past the last voltage in VI.xlsx (0.65 V), so 0.7, 0.8 and 0.9 come back NaN. 244 of 454 report scripts we checked have these 3 NaN; 14 of those reports mention them.
Why is my text file from fprintf in the wrong order?
fprintf uses up its arrays in column order. fprintf(fid, '%f %f\n', V, I) prints all of V first, two to a line, then all of I. To get one V, I pair per line, give it a 2-row matrix: fprintf(fid, '%f %f\n', [V(:)'; I(:)']). 49 reports write their file the scrambled way.
What is the Python equivalent of MATLAB interp1?
numpy.interp for linear, but it repeats the end values outside the data instead of returning NaN (at 0.9 V it returns 3.91), so pass left=np.nan, right=np.nan. For the cubics: scipy.interpolate.CubicSpline (not-a-knot, like MATLAB's spline), PchipInterpolator and Akima1DInterpolator(method='makima', extrapolate=True). Use np.linspace(-17.3, 0.9, 183) for the range: np.arange(-17.3, 0.9, 0.1) stops at 0.8.
Related on Skill-Lync
- Curve fitting calculator (least-squares polynomial fits, R² and RMSE)
- Finite difference coefficients calculator
- Student project: Data Handling and Interpolation
- MATLAB courses compared and Python courses compared
- MATLAB and Simulink project ideas
- All free engineering tools
Sources
- MathWorks, interp1 documentation: methods, "The values in x must be distinct", 'pchip', 'spline' and 'makima' extrapolate by default and the other methods return NaN; 'cubic' is cubic convolution since R2020b.
- MathWorks, spline ("not-a-knot end conditions"), makima ("Modified Akima piecewise cubic Hermite interpolation"; "avoids overshoot"), fprintf ("in column order"; "may override the specified conversion") and xlsread ("not recommended", R2019a) documentation.
- C. Moler, Numerical Computing with MATLAB, chapter 3 (pchiptx and splinetx); C. Moler and C. Ionita, "Makima Piecewise Cubic Interpolation", Cleve's Corner (MathWorks blog), 2019. F. N. Fritsch and R. E. Carlson, "Monotone piecewise cubic interpolation", SIAM J. Numer. Anal. 17(2), 1980.
- NumPy, numpy.interp (left / right default to fp[0] / fp[−1]; xp must be increasing, "not explicitly enforced") and numpy.arange (half-open interval; use numpy.linspace for a non-integer step). SciPy 1.18: CubicSpline (not-a-knot default), PchipInterpolator, Akima1DInterpolator (method="makima" since 1.13, extrapolate defaults to False), interp1d (legacy; raises outside the range by default).
- Skill-Lync, all 822 public "Data Handling and Interpolation" reports, read 20 Nov 2026: the VI.xlsx table as printed by xlsread, MATLAB's printed linear, spline and pchip values, methods, file writing.
- Skill-Lync, scripts/verify/interpolation.py: 54 checks. SciPy reproduces the MATLAB output printed in the reports (linear and spline at the 15 voltages to 6 decimals, pchip on the range, spline 101.1102 and pchip 26.9384 at 0.9 V, the 3 NaN), then writes the values this page checks its own TypeScript against at build time.