3
$\begingroup$

I am new to Mathematica, and I am trying to extrapolate some data points. In Python, I can easily do that using interp1d. See below:

from scipy.interpolate import interp1d
import numpy as np
import matplotlib.pyplot as plt

#Random_data
xdata = [0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.1]
ydata = [0.11, 0.234, 0.324, 0.3986, 0.512, 0.677, 0.765, 0.85433, 1.0533]

xdata_new = np.linspace(0, 0.3, 100)
f_interp = interp1d(xdata, ydata, kind = 'quadratic', fill_value = 'extrapolate')

plt.plot(xdata, ydata, label = "original/old data")
plt.plot(xdata_new, f_interp(xdata_new), '--', label = "extrapolated data")
plt.xlabel("x")
plt.ylabel("y")
plt.legend()

The result:

enter image description here

My attempt in Mathematica:

xdata = {0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.1}
ydata = {0.11, 0.234, 0.324, 0.3986, 0.512, 0.677, 0.765, 0.85433, 
  1.0533}

ListLinePlot[Transpose[{Interpolation[xdata, 0], Interpolation[ydata, 0]}],AxesOrigin -> {0, 0}]

The result in Mathematica:

Input value {0} lies outside the range of data in the interpolating function. 
Extrapolation will be used

I was using Interpolation in hopes that it will extend the data points outside the range as demonstrated in Python.

What would be the equivalent of interp1d(xdata, ydata, kind = 'quadratic', fill_value = 'extrapolate') in Mathematica?

$\endgroup$
1
  • 1
    $\begingroup$ You're using Interpolation blindly, please read its document (which is very detailed and suitable for self-learning) carefully by pressing F1. Getting used to reading document of Mathematica is the first thing to learn for beginners. $\endgroup$
    – xzczd
    Aug 23, 2022 at 2:31

1 Answer 1

8
$\begingroup$
int = Interpolation[Transpose[{xdata, ydata}]]

Show[
  Plot[
    int[x], {x, 0.3, 1.1},
    PlotLegends -> {"original"}
  ],
  Plot[
    int[x], {x, 0, 0.3}, PlotStyle -> Directive[Orange, Dashed],
    PlotLegends -> {"extrapolated"}
  ],
  Frame -> True, Axes -> False, PlotRange -> All
]

similar plot to one in OP

This will still output the extrapolation warning. I find that to be a positive feature: I would always like to be aware of the use of extrapolation whenever that happens.

$\endgroup$
2
  • $\begingroup$ If you need to get rid of the warning, you can wrap the code in Off[InterpolatingFunction::dmval]; .... On[InterpolatingFunction::dmval]; $\endgroup$
    – mikado
    Aug 22, 2022 at 18:12
  • 4
    $\begingroup$ Interpolation[Transpose[{xdata, ydata}] "ExtrapolationHandler" -> {Automatic, "WarningMessage" -> False}] will transfer the redness from the output to the input, but it turns off the warning message if desired. $\endgroup$
    – Michael E2
    Aug 22, 2022 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.