Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

lagrange polynomial python code

# Lagrange Interpolation

# Importing NumPy Library
import numpy as np

# Reading number of unknowns
n = int(input('Enter number of data points: '))

# Making numpy array of n & n x n size and initializing 
# to zero for storing x and y value along with differences of y
x = np.zeros((n))
y = np.zeros((n))


# Reading data points
print('Enter data for x and y: ')
for i in range(n):
    x[i] = float(input( 'x['+str(i)+']='))
    y[i] = float(input( 'y['+str(i)+']='))


# Reading interpolation point
xp = float(input('Enter interpolation point: '))

# Set interpolated value initially to zero
yp = 0

# Implementing Lagrange Interpolation
for i in range(n):
    
    p = 1
    
    for j in range(n):
        if i != j:
            p = p * (xp - x[j])/(x[i] - x[j])
    
    yp = yp + p * y[i]    

# Displaying output
print('Interpolated value at %.3f is %.3f.' % (xp, yp))
Comment

PREVIOUS NEXT
Code Example
Python :: print with no newline 
Python :: how to download a project from pythonanywhere 
Python :: Python Tkinter Button Widget Syntax 
Python :: python window icon on task bar 
Python :: python autocorrelation plot 
Python :: django update request.post 
Python :: how to merge rows in pandas dataframe 
Python :: how to bold in colorama 
Python :: change index to dataframe pandas 
Python :: c++ call python function 
Python :: numpy declare arraylength 
Python :: how to convert binary to integer in python 
Python :: show distribution pandas coloumns 
Python :: python hasattribute 
Python :: list files in http directory python 
Python :: how to check if python is installed 
Python :: traversing a tree in python 
Python :: instagram python bot 
Python :: python visualize fft of an image 
Python :: taking array input in python 
Python :: python uuid 
Python :: how to create python environment 
Python :: save object pickle python 
Python :: pandas resample groupby 
Python :: python yeild 
Python :: hash table in python 
Python :: merge lists 
Python :: correlation for specific columns 
Python :: python session set cookies 
Python :: python string cut 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =