-1

So I have this task, where im supposed to interpolate a function with polynomials. The entire interval is divided into N subintervals, and the polynomial interpolating in each subinterval is of order k. I generet all my interpolating points, but I am running into two problems.

I) For k=1, i.e first order polynomials, I've tried solving the task by having a loop generate a first order polynomial in each subinterval using the scipy interp1d, but I'd like to get all the different polynomials in a single plot.

This is my code, tried only including the nessescary bits, sorry if something is missing. intpoint here are the interpolation points, and funky(x) is just the arbitrary function im approximating.

import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as sc

intpoint=np.array([-3,-2,-1,0,1,2,3])

for i in range(len(intpoint)):
    intleng=[intpoint[i],intpoint[i+1]]
    myinterval=np.linspace(intpoint[i],intpoint[i+1],1000)
    mypol=sc.interp1d(intleng,np.sin(intleng),1)
    plt.plot(intleng, mypol(intleng))
    plt.plot(myinterval,np.sin(myinterval))
    plt.show()

Apologies in advance if anything is unclear, or my code is hard to follow/untidy.

Tettidesa
  • 1
  • 1
  • Please provide a [mre]. – Banana Sep 19 '20 at 15:05
  • Sorry, tried streamlining a bit, hopefully this is better. I was wondering how i could combine the plots, into a single plot of the whole interval, rather than multiple plots of each subinterval. – Tettidesa Sep 19 '20 at 17:25

1 Answers1

0
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as sc

intpoint=np.array([-3,-2,-1,0,1,2,3])

for i in range(len(intpoint)-1):
    intleng=[intpoint[i],intpoint[i+1]]
    myinterval=np.linspace(intpoint[i],intpoint[i+1],1000)
    mypol=sc.interp1d(intleng,np.sin(intleng),1)
    plt.plot(myinterval,mypol(myinterval))
    plt.plot(myinterval,np.sin(myinterval))

plt.show()

I think this is what you want. There was a mistake in the plotting and you should do plt.show() only once to get one plot.