Posts

Newtons backward Difference formula -sastry 3.6

 #Newtons backwardDiffernce formula -sastry 3.6 xpoints=[15,20,25,30,35,40] ypoints=[.2588190,0.3420201,0.4226183,0.5,0.573576,0.6427876] n=len(xpoints) z=[0] d1=z*(n-1) d2=z*(n-2) d3=z*(n-3) d4=z*(n-4) d5=z*(n-5) x=float(input('enter data point for interpolation')) h=xpoints[1]-xpoints[0] for i in range(0,n-1):     d1[i]=ypoints[i+1]-ypoints[i] for i in range(0,n-2):     d2[i]=d1[i+1]-d1[i] for i in range(0,n-3):     d3[i]=d2[i+1]-d2[i] for i in range(0,n-4):     d4[i]=d3[i+1]-d3[i] for i in range(0,n-5):     d5[i]=d4[i+1]-d4[i] print('Table of first order differences \n', d1) print('\n Table of second order differences \n',d2) print('\n Table of third order differences \n',d3) print('\n Table of fourth order differences \n',d4) print('\n Table of fifth order differences \n',d5) p=(x-xpoints[0])/h print ('p=',p) print ('h=',h) yvalue=ypoints[0]+p*d1[0]+(p*(p-1)*d2[0])/2+(p*(p-1)*(p-2)*d3[0])/6 yvalue1=yvalue+(p*(p-1)*(p-2

Newtons forward Difference formula

 #Newtons forward Difference formula xpoints=[30.1,30.2,30.3,30.4,30.5,30.6] ypoints=[.5015107,.5030199,.5045276,0.5060388,0.5075384,0.5090414] n=len(xpoints) z=[0] d1=z*n d2=z*n d3=z*n d4=z*n d5=z*n x=float(input('enter data point for interpolation')) h=xpoints[1]-xpoints[0] for i in range(0,n-1):     d1[i]=ypoints[i+1]-ypoints[i] for i in range(0,n-2):     d2[i]=d1[i+1]-d1[i] for i in range(0,n-3):     d3[i]=d2[i+1]-d2[i] for i in range(0,n-4):     d4[i]=d3[i+1]-d3[i] for i in range(0,n-5):     d5[i]=d4[i+1]-d4[i] print('Table of first order differences \n', d1) print('\n Table of second order differences \n',d2) print('\n Table of third order differences \n',d3) print('\n Table of fourth order differences \n',d4) print('\n Table of fifth order differences \n',d5) p=(x-xpoints[0])/h print ('p=',p) print ('h=',h) yvalue=ypoints[0]+p*d1[0]+(p*(p-1)*d2[0])/2+(p*(p-1)*(p-2)*d3[0])/6 print ('value of y corresponding to

Simpsons 3 by 8

  #Numerical integration by Simpson's 3/8 rule from math import * def f(x):     return 1/(1+x) a,b,n =eval(input('Input lower limit (a), upper limit (b) & no. of steps(multiple of 3)(n): ')) h = (b - a)/float(n) x = [a+h*i for i in range(n+1)] y=[f(x[i]) for i in range(n+1)]  intgrl=y[0] + y[-1] for i in range(1,n):     if i%3==0:         intgrl+=2*y[i]     else:         intgrl+=3*y[i] intgrl=(3*h/8)*intgrl      print('Integral using Simpson\'s 3/8 rule = ',intgrl)          Output Screen: Input lower limit (a), upper limit (b) & no. of steps(multiple of 3)(n): 0,1,3 Integral using Simpson's 3/8 rule =  0.6937500000000001 Another Code #Numerical integration by Simpsons 3/8 rule from math import * def f(x):     return sin(x) a=0 b=pi/2 n=int(input ("enter number of intervals,n  ")) h=(b-a)/n integral=f(a)+f(b) for i in range(1,n):     x=a+i*h     if (i%3==0):         integral=integral+2*f(x)     else:         integral=integral+3*f(x) integral

Montecarlo: Integrate x from 0 to 1

 #this example integrates x from 0 to 1 #approximation of an integral by average sum=0 def f(x):     return x from random import * n=int(input('How many random numbers ?')) for i in range(1,n+1):       x=random()       sum=sum+f(x)   print('Value of integral =',1/n*sum)         Output Screen:    How many random numbers ?10000 Value of integral = 0.4997902939934223         

Spline Interpolation- Scipy

Image
 #Spline interpolation using Scipy. from scipy.interpolate import CubicSpline import numpy as np import matplotlib.pyplot as plt x = [1, 2, 3] y = [-8, -1, 18] f = CubicSpline(x, y, bc_type='natural') x_new = np.linspace(1, 3, 100) y_new = f(x_new) print(f(2.5)) plt.plot(x_new, y_new, 'b') plt.plot(x, y, 'ro') plt.title('Cubic Spline Interpolation') plt.xlabel('x') plt.ylabel('y') plt.show() Output Screen: 7.375 Plotting the solution of example 5.3  using pylab #Example 5.1,5.3 SS Satry from pylab import*  x = [1,2,3] y = [-8,-1,18] x1=linspace(1,2,100) y1=3*(x1-1)**3-8*(2-x1)-4*(x1-1) #cubic spline y2=7*x1-15   #linear y5=x1**3-9  #actual function x2=linspace(2,3,100) y4=3*(3-x2)**3+22*x2-48  #cubic spline y3=19*x2-39   #linear y6=x2**3-9  #actual function plot(x, y, 'o','data') plot(x1,y1,'b-') plot(x2,y4,'b-') #cubic spline -blue plot(x1,y2,'r--') plot(x2,y3,'r--') #linear -red plot(x1,y5

Bisection Method

 #Root of x-cos(x) by bisection method from math import* def f(x): return (x-cos(x)) a=0 b=1 x=(a+b)/2 print ("The successive approximation root is ") while abs(f(x))>0.0000001: x=(a+b)/2 print (x) if f(x)<0: a=x elif f(x)>0: b=x #Correct root is 0.7390851 Ouput Screen: The successive approximation root is  0.5 0.75 0.625 0.6875 0.71875 0.734375 0.7421875 0.73828125 0.740234375 0.7392578125 0.73876953125 0.739013671875 0.7391357421875 0.73907470703125 0.739105224609375 0.7390899658203125 0.7390823364257812 0.7390861511230469 0.7390842437744141 0.7390851974487305 0.7390847206115723 0.7390849590301514 0.7390850782394409

Regula Falsi Method Or Method of false position

 #Root of x**3-2*x-5 by regula fasli method-problem 2.6 sastry from math import* def f(x): return (x**3-2*x-5) a=2 b=3 x=(a*f(b)-b*f(a))/(f(b)-f(a)) print ("The successive approximation root is ") while abs(f(x))>0.0000001: x=(a*f(b)-b*f(a))/(f(b)-f(a)) print (x) if f(x)<0: a=x elif f(x)>0: b=x #Correct root is 2.0945... Output Screen: The successive approximation root is  2.0588235294117645 2.0812636598450225 2.089639210090847 2.0927395743180055 2.0938837084618487 2.09430545112526 2.0944608457664873 2.0945180933857572 2.0945391822940174 2.094546950874257 2.094549812585823 2.0945508667513875 2.094551255072798 2.094551398118127 2.0945514508114953 2.0945514702220622 2.0945514773723