#Lagrange's interpolation Input
xdata = []
ydata = []
print("How many number of data sets")
n = int(input())
print("Enter the x and y data:")
for i in range(n):
x = float(input('xdata= '))
xdata.append(x)
y = float(input('ydata= '))
ydata.append(y)
print("Enter the x value for which y is to be determined")
x1 = float(input())
f = 0.0
for i in range(n):
t = 1.0
for j in range(n):
if i == j:
continue
else:
t = t * (x1 - xdata[j]) / (xdata[i] - xdata[j])
f = f + ydata[i] * t
print("The value of y at x=", x1, "is", f)
Output
Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit
(AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART
================================
>>>
How many number of data sets
4
Enter the x and y data:
xdata= 0
ydata= 18
xdata= 1
ydata= 10
xdata= 3
ydata= -18
xdata= 6
ydata= 90
Enter the x value for which y is to be determined
2
('The value of y at x=', 2.0, 'is', -6.0)
>>>
# monte carlo method#
import random
pi = []
m = [100, 1000, 10000, 100000, 1000000]
print('iteration\tvalue of pi')
for i in range(len(m)):
n=0
for j in range(m[i]):
x = random.random()
y = random.random()
r = x**2 + y**2
if r <= 1:
n =n+1
x = 4 * (float(n) / float(m[i]))
pi.append(x)
print('%8d\t%10.7f' % (m[i], pi[i]))
Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)]
on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART
================================
>>>
iteration value of pi
100 3.4400000
1000 3.1920000
10000 3.1320000
100000 3.1410400
1000000 3.1431480
>>>
print('Solving the differential equation using 4th order Runge-
Kutta method')
# Define the system of equations
def g(x, y, z):
return (x * z ** 2) - (y ** 2)
def f(x, y, z):
return z
# Initial conditions and step size
x=0
y=1
z=0
h = 0.2
xf = 0.2
# Print headers
print('%10s %10s' % ('x value', 'y value'))
# Initialize data lists
xdat = [x]
ydat = [y]
zdat = [z]
# Runge-Kutta iteration
while x <= xf:
print('%10.6f %10.6f' % (x, y))
k1 = h * f(x, y, z)
l1 = h * g(x, y, z)
k2 = h * f(x + h / 2, y + k1 / 2, z + l1 / 2)
l2 = h * g(x + h / 2, y + k1 / 2, z + l1 / 2)
k3 = h * f(x + h / 2, y + k2 / 2, z + l2 / 2)
l3 = h * g(x + h / 2, y + k2 / 2, z + l2 / 2)
k4 = h * f(x + h, y + k3, z + l3)
l4 = h * g(x + h, y + k3, z + l3)
y += (k1 + 2 * k2 + 2 * k3 + k4) / 6
z += (l1 + 2 * l2 + 2 * l3 + l4) / 6
x += h
xdat.append(x)
ydat.append(y)
zdat.append(z)
Output
Type "help", "copyright", "credits" or "license()" for more
information.
>>>
========== RESTART: C:/Users/user/Documents/PYTHON
EXPERIMENT/RK 1.py ==========
Solving the differential equation using 4th order Runge-Kutta method
x value y value
0.000000 1.000000
0.200000 0.980146
#Newton-Raphson method
from math import *
def f(x):
return cos(x)-x*exp(x)
def df(x):
return -sin(x)-exp(x)-x*exp(x)
x = float(input('Enter an approximate root of the equation: '))
print('x \t f(x)')
print('-- \t --')
while True:
print('%0.4f %0.4f '%(x, f(x)))
h = (f(x) / df(x))
if abs(h) < 0.000001:
break
x=x-h
print('Root of the given equation is %.3f' % (x))
Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit
(AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART
================================
>>>
Enter an approximate root of the equation: 1
x f(x)
-- --
1.0000 -2.1780
0.6531 -0.4606
0.5313 -0.0418
0.5179 -0.0005
0.5178 -0.0000
Root of the given equation is 0.518
>>>