# -*- coding: utf-8 -*-
"""
Spyder Editor

Lab 5, Q3
"""

import numpy as np
import matplotlib.pyplot as plt

def polyx(a,x):
    # evaluates a polynomial f(x) of x,
    # given a nump array of coefficients a,
    # such that 
    # 
    # f(x) = a[0] + a[1]x + a[2]x**2 + ... + a[n-1]x**(n-1)
    
    f = 0
    for i in range(len(a)):
        f = f + a[i]*x**i
        
    return f

def vandint(x,xp,yp):
    # for a given set of nodes xp and values yp,
    # returns an interpolating polynomial in x
    
    V = xp**0.
    for i in range(1,len(xp)):
        V = np.hstack([V,xp**i])
        
    VI = np.linalg.inv(V)

    a = np.matmul(VI,yp)
    f = polyx(a,x)
    
    return f
    
#set up point pairs for interpolation
xp = np.array([[-1],[0],[1],[2]])
yp = np.array([[2],[-1],[1],[0]])

# set up x values:
x = np.linspace(-1,2,100)

f = vandint(x,xp,yp)
plt.plot(x,f)





