To interpolate a real data set from an experiment using the Lagrange’s method, and Newton’s method of forward differences.

  To interpolate a real data set from an experiment using the Lagrange’s method, and Newton’s method of forward differences.





Python Program: (For Newton Forward Interpolation)

# ------------------------------------------------
# Newton method of forward difference
# ------------------------------------------------
def fact ( n):
  f = 1;
  for i in range (2 , n +1) :
    f = f *i # f *= i
  return f;
# ------------------------------------------------
def p_cal (p ,n ): # p(p -1) (p -2) ........(p-n)
  temp = p
  for i in range (1 , n):
    temp = temp * ( p - i);
  return temp ;

# ------------------------------------------------

n = 4
x = [1 , 357];

y = [[0 for i in range (n)]
for j in range (n ) ]
y [0][0]= 24
y [1][0]= 120
y [2][0]= 336
y [3][0]= 720

# ----------- calculating forward difference -- ----

for i in range (1 , n):
  for j in range (0 ,n -i ): # j = 4 -1 = 3
    y[j ][ i] = y[j +1][ i -1] - y [j ][ i -1]

# ------------------------------------------------

for i in range ( n):
  print (x[i] , end = "\t");
  for j in range (n -i):
    print (y[i ][ j], end = "\t")
  print (" ")
# ------------------------------------------------

v = 8

sum = y [0][0]
p = ( v - x [0]) /( x [1] - x [0]) # h = (x [1] - x [0])

for i in range (1 , n):
  sum = sum + ( p_cal (p ,i) * y [0][ i ]) / fact (i );

# ------------------------------------------------
print (" the value of y for v = ", v , "is :", sum )
# ------------------------------------------------




Python Program (Lagrange's Interpolation):


# ------------- lagrange ’s interpolation -------------------
# x | 300 | 304 | 305 | 309
# y | 2.4771 | 2.4829 | 2.4843 | 2.4871
# calculate on y on x = 301 (v)
# ---------------------------------------------------------
# l_n(x) = (x-x1)(x-x2) ...../( x0 -x1)(x0 -x2) ...........
#------ l_n (x) calculation -------------------------------
import numpy as np

def l_x (i ,x ,v ):
  num_lx = 1;
  denum_lx = 1;
  for j in range (0 , n):
    if(j != i):
      num_lx = num_lx * (v - x[j ])
      denum_lx = denum_lx * (x[i] - x[j ])

  l0 = num_lx / denum_lx
  return l0

# --------------------------------------------------------

x = [300 , 304 , 305 , 307];
y = [2.4771 , 2.4829 , 2.4843 , 2.4871];

n = np . size (x) # size of data
# --------------------------------------------------------
v = 301
sum = 0.0
for i in range (0 , n):
  sum = sum + l_x (i ,x ,v)* y[i]
  
  print ( l_x (i ,x , v))
print("\n"
print (" Lagrange interpolation of v = ",v ," is :", sum)
# --------------------------------------------------------









Comments

Popular posts from this blog

Solving Kepler’s equation by Newton-Raphson method

Velapanti Python Program

To find mean , standard deviation and frequency distribution of an actual data set (Python Program)