Christoph90
Christoph90

Reputation: 673

Python: SciPy.interpolate PiecewisePolynomial

import numpy as np
from scipy.interpolate import PiecewisePolynomial

xi = np.array([1,10])
yi = np.array([10,1])

p = PiecewisePolynomial(xi,yi)

Does not yield a linear interpolation of the two points but

ZeroDivisionError: integer division or modulo by zero

What's wrong there?

Upvotes: 1

Views: 430

Answers (1)

Sudeep Juvekar
Sudeep Juvekar

Reputation: 5108

Replace your yi with

yi = np.array([[10], [1]])

PiecewisePolynomial requires y array to be an array-like or a list-of-array structure. Each element of y can be a function value for x and its subsequent derivatives. Above change to y creates the correct linear interpolation

p = PiecewisePolynomial(xi,yi)
p.__call__([5.])
>> array([6.])
p.__call__([2.])
>> array([9.])

Upvotes: 1

Related Questions