/
interpolate_plotting.py
79 lines (65 loc) · 2.6 KB
/
interpolate_plotting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import random
import os
from numpy import*
import numpy.polynomial.polynomial as P
import matplotlib.pyplot as plt
# class-Interpolation
class Interpolate:
def solve(self,U,V,method):
if(method=="newton"):
return (self.Newton(U,V))
else:
return (self.Lagrange(U,V))
def pattern(self,cofficient):
l=len(cofficient);
temp=[];
for i in range(l-1):
temp.append(str(cofficient[l-(i+1)])+'x^'+str(l-(i+1))+str('+'))
temp.append(str(cofficient[0])+'x^'+str(0));
str1=''.join(temp)
return(str1)
def Lagrange(self,U,V):
n=len(U)
w=(-1*U[0],1)
for i in range(1,n):
w=P.polymul(w,(-1*U[i],1))
result=array([0.0 for i in range(len(w)-1)])
derivative=P.polyder(w)
for i in range(n):
result+=(P.polydiv(w,(-1*U[i],1))[0]*V[i])/P.polyval(U[i],derivative)
temp=list(result);
cofficient=temp
l=len(cofficient);
temp=[];
for i in range(l-1):
temp.append(str(cofficient[l-(i+1)])+'x^'+str(l-(i+1))+str('+'))
temp.append(str(cofficient[0])+'x^'+str(0));
str1=''.join(temp)
return(str1)
def plot(self,U,V,ctr):
X1=arange(min(U)-2,max(U)+2,0.1)
x=[1]
for i in range(len(U)):
x=P.polymul(x,[-1*U[i],1])
plt.axis([-10,10,min(V)-1,max(V)+1])
b=[0]
for i in range(len(U)):
a=P.polydiv(x,[-1*U[i],1])
b=P.polyadd(P.polymul((P.polydiv(a[0],P.polyval(U[i],a[0])))[0],[V[i]]),b)
Y=P.polyval(X1,P.polymul((P.polydiv(a[0],P.polyval(U[i],a[0])))[0],[V[i]]))
plt.plot(X1,Y,'y')
plt.plot(U,V,'ro')
X1=arange(-5,5,0.1)
Y=P.polyval(X1,b)
plt.plot(X1,Y,'b',label='Required Polynomial')
plt.plot((-8,8),(0,0),'k')
plt.grid(b=True, which='both', color='0.65',linestyle='-')
Y=list(Y)
plt.plot((0,0),(-max(Y)-5,max(Y)+5),'k')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.legend(loc=1)
filename = "this_plot"+str(ctr)+".png"
path = "C:\\Users\HP\Anaconda3\static"
fullpath = os.path.join(path, filename)
plt.savefig(fullpath)