-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathlagrange.py
65 lines (59 loc) · 1.8 KB
/
lagrange.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
from typing import List
import numpy as np
import pandas as pd
import math
"""
lagrange is a mathematical interpolation methods that approximate
a polynomial of degree n given n-1 points
"""
def lagrange(X,Y) -> List:
"""
the lagrange method that takes n points as input and return a pandas dataframe
"""
my_columns = ['x','y']
dataframe = pd.DataFrame(columns=my_columns)
for pt in np.arange(-1,2,0.001):
condition = False
for i in range(0,len(X)):
check = False
for j in range(0,len(X)):
if (i is not j and check==False):
Lp = (pt-X[j])/(X[i]-X[j])
L = Lp
check = True
elif (i is not j):
L = L* (pt-X[j])/(X[i]-X[j])
if (condition ==False):
P = Y[i]* L
condition = True
else:
P = Y[i]* L + P
dataframe = dataframe.append(
pd.Series(
[pt,P],
index=my_columns
),
ignore_index=True)
return dataframe
if __name__=="__main__":
"""
creating the polynome that we want to approximate
using lagrange method
"""
my_columns = ['x','y']
dataframe = pd.DataFrame(columns=my_columns)
for i in np.arange(-1,2,0.001):
dataframe = dataframe.append(
pd.Series(
[i,pow(i,3)],
index=my_columns
),
ignore_index=True)
"""
calculating the mean square error from the given polynome and the result
"""
res = lagrange(np.array([-1,1,2]),np.array([-1,1,8]))
m = len(dataframe)
diff = pow(dataframe['y']-res['y'],2).sum()
RMSE = 1/m * (math.sqrt(diff))
print(RMSE)