Created
February 23, 2018 05:04
-
-
Save tommyct614/243dacfae8169cdb9de19bce7d2d72e0 to your computer and use it in GitHub Desktop.
python code for lagrange interpolation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
rawData = np.loadtxt("inputData.dat") | |
x = rawData.transpose()[0] | |
y = rawData.transpose()[1] | |
xnew = np.loadtxt("x.dat") | |
ynew = np.zeros(len(xnew)) | |
for k in range(len(xnew)): | |
suum1 = 0 | |
for i in range(len(x)): | |
y1 =1 | |
for j in range(len(x)): | |
if i != j: | |
y1 *= (xnew[k] - x[j]) / (x[i] - x[j]) | |
suum1 += y[i] * y1 | |
ynew[k] = suum1 | |
np.savetxt("lagint.dat",np.c_[xnew,ynew],fmt = "%10.3e") | |
plt.plot(x,y,"r") | |
plt.plot(xnew, ynew, "bo") | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment