Search This Blog

Wednesday, July 6, 2011

Lagrange Interpolation


#Lagrange interpolation
n=input(" No of data points = ")
xd=[]
yd=[]
print "Enter data points"
for i in range(n):
    xi,yi=input()
    xd.append(xi)
    yd.append(yi)

x=input(" Enter the x value for which y is to be found ")
l=0
for i in range(n):
    lg=1
    for j in range(n):
        if i!= j:
            lg=lg*(x-xd[j])/float(xd[i]-xd[j])
           
    l=l+yd[i]*lg
   

print " Y value = ",l

Newton forward interpolation


#Newton forward interpolation
from numpy import*

def fact(n):
    if n<=1:
        return(1)
    else:
        return(n*fact(n-1))
       
n=input("Enter no of elements ")
y=zeros([n,n])
x0,h=input("Enter the value of x[0] and finite difference ")
x=[x0+i*h for i in range(n)]

print "Enter y values"
for i in range(n):
    print "Enter y value for x[%d]"%(i)
    y[i,0]=input()
for i in range(1,n):
    k=n-i
    for j in range(k):
        y[j,i]=y[j+1,i-1]-y[j,i-1]
   
print y

xp=input(" Enter the x value for which y is to be found")
u=(xp-x[0])/float(h)
#print "u=",u
yp=y[0,0]
for i in range(1,n):
    t=1
    for j in range(i):
        t=t*(u-j)
    yp=yp+t*y[0,i]/fact(i)
   
print "The value of y is  %f "%(yp)

Saturday, July 2, 2011

Pie Chart


from pylab import *

figure(1, figsize=(8,8))
#axes([0.1, 0.1, 0.8, 0.8])

mycolors=['red', 'blue', 'green', 'brown']
mylabels=['Red', 'Blue', 'Green', 'Brown']
fracs=[40, 30, 20, 10]
pie(fracs,labels=mylabels,colors=mycolors)

show()

Parametric curves


from pylab import*
a=5
b=3
wx=4
wy=5
dx=0
dy=0
t=linspace(0,10,200)
x=a*cos(wx*t-dx)
y=b*sin(wy*t-dy)
figure(1,figsize=(4,4))
plot(x,y)
show()

Polar Graphs- Butterfly plot



from pylab import*
theta=linspace(0,8*pi,1000)
r=exp(sin(theta))-2*cos(4*theta)+sin((2*theta-pi)/24.0)
#r=cos(5*theta)*cos(5*theta)+sin(3*theta)+0.3
figure(1,figsize=(10,10))
polar(theta,r,'red')
show()

Bessel function plot


#Bessel function plot
from pylab import*
def fact(n):
    if n<=1:
        return(1)
    else:
        return(n*fact(n-1))
xdat=linspace(0,8*pi,200)

for n in range(5):
    jnx=[]
    for x in xdat:
        sum=0
        for k in range(0,50):
            sum+=(-1)**k*(x/2)**(n+2*k)/(fact(k)*fact(n+k))
        jnx.append(sum)
   
    plot(xdat,jnx)
   
show()

Power series- sine series


#sine series
from pylab import*
def fact(n):
    if n<=1:
        return(1)
    else:
        return(n*fact(n-1))
x=linspace(0.0,8*pi,100)
y=sin(x)
sinp=[]

for k in x:
    sum=0
    for i in range(0,31):
        sum+=(-1)**i*k**(2*i+1)/fact(2*i+1)
    sinp.append(sum)
   
plot(x,y,x,sinp)
show()

Fourier series- Square wave - Sawtooth wave


#Fourier Series-Square Wave
#f(t)=a/2+(2a)/pi[sin(wt)+sin(3wt)/3+sin(5wt)/5+........]

from pylab import*
t=linspace(0.0,2*pi,100)
a=2.0
w=2.0
y=a/2
for i in range(1,200,2):
    y=y+2*a*sin(i*w*t)/(pi*i)
plot(t,y)
show()


#Fourier Series-Saw-tooth wave
#f(t)=a/2+a/pi[sin(wt)+sin(2wt)/2+sin(3wt)/3+sin(4wt)/4+sin(5wt)/5+........]

from pylab import*
t=linspace(0.0,2*pi,100)
a=2.0
w=5.0
y=a/2
for i in range(1,100):
    y=y+a*sin(i*w*t)/(pi*i)
plot(t,y)
show()