Skip to main content

Posts

Program to read numbers from a file and determine histogram of the data

#Program to read numbers from a file and determine histogram of the data: f1=open("hg3.d","r") n=input("Enter the no. of lines:") n=int(n) h=[0 for i in range (n)] x=[] for i in f1:  x.append(eval(i)) t=len(x) mn=min(x) mx=max(x) R=(mx-mn)*1.01 bs=R/n for i in range (t):  k=int(abs((h[i]-mn)/bs))  h[k]=h[k]+1.0  f=[] for i in range (n):  d=mn+(i*bs)  f.append(d) f1.close() f2=open("hg2.d","w") a=len(f) for i in range (a):  print(h[i],f[i],"-",f[i]+bs,file=f2) f2.close

Program to implement 2D Newton Raphson method to find intersecting points

#Program to implement 2D Newton Raphson method to find intersecting points for # i) x^2 + y^2 = 4 # ii) (x-1)^2 + y^2 = 4 from math import* def f(x,y):  y=x**2+y**2-4  return y def g(x,y):  y=(x-1)**2+y**2-4  return y def fx(x,y):  y=2*x  return y def gx(x,y):  y=2*(x-1)  return y def fy(x,y):  y=2*y  return y def gy(x,y):  y=2*y  return y x=float(input("Enter the initial guess,x0=")) y=float(input("Enter the initial guess,y0=")) s=0 det_J_inv=fx(x,y)*gy(x,y)-fy(x,y)*gx(x,y) if det_J_inv==0.0:  print("Enter new guess values.") else:  while abs(f(x,y))>.000000001 and abs(g(x,y))>.000000001 and s<50:   h=(f(x,y)*gy(x,y)-fy(x,y)*g(x,y))/float(det_J_inv)   k=(-gx(x,y)*f(x,y)+g(x,y)*fx(x,y))/float(det_J_inv)   x=x-h   y=y-k   s=s+1 print("The root is located at:\nx=",x,"y=",y,"\nRequired No. of iterations:",s)

Program to diagonalise a given matrix using Gauss Jordan method

#Program to diagonalise a given matrix using Gauss Jordan method a=[] n=input("Enter the dimension of the matrix:") n=int(n) for i in range(n):  r=input("Enter the elements for row "+str(i+1)+" :").split(",")  r=list(map(float,r))  a.append(r) print("The entered matrix is:") for i in range(n):  print(a[i]) for c in range(0,n):  for r in range(0,n):   if r!=c:    l=a[r][c]/a[c][c]    for k in range(n):     a[r][k]=a[r][k]-l*a[c][k] print("The diagonalised matrix is:") for i in range(n):  print(a[i])

Program to convert a matrix to a upper triangular form using Gauss elimination method and to find the determinant of the matrix.

# Program to convert a matrix to a upper triangular form using Gauss elimination method and to # find the determinant of the matrix. a=[] n=input("Enter the dimension of the matrix:") n=int(n) for i in range(n):  r=input("Enter the elements of row "+str(i+1)+" :").split(",")  r=list(map(float,r))  a.append(r) print("The entered matrix is:") for i in range(n):  print (a[i]) for c in range(n-1):  for r in range(c+1,n):   l=a[r][c]/a[c][c]   for k in range(n):    a[r][k]=a[r][k]-l*a[c][k] s=1.0 print("The entered matrix in upper triangular form is:") for i in range(n):  print(a[i])  s=a[i][i]*s print ("The determinant of the matrix is:",s)

Program to evaluate integration using Monte carlo method (four leaf rose):

#Program to evaluate integration using Monte carlo method (four leaf rose): from math import* def f(x):  y=sin(2*x)  return y import random as rn nt=input("Enter total no. darts:") nt=int(nt) ns=0.0 for i in range (nt):  x=rn.random()  y=rn.random()  x1=-1+2*x  y1=-1+2*y  r=sqrt(x1**2+y1**2)  o=atan2(y1,x1)  if r<=abs(f(o)):   ns=ns+1 A=ns/nt*4 print("The value of the integration is:",A)

Program to evaluate integration using monte carlo method:

#Program to evaluate integration using monte carlo method: from math import* def f(x):  y=(1-x*x)*exp(-x*x)  return y import random as rn a=input("Enter the lower limit:") b=input("Enter the upper limit:") nt=input("Enter the total no. darts:") n=input("Enter the no. of divisions:") a=float(a) b=float(b) n=int(n) nt=int(nt) dell=(b-a)/(n*1.0) k=[] for i in range (n):  j=a+i*dell  u=f(j)  k.append(u) h1=max(k)*1.2 s=min(k)*1.2 h2=abs(s) ns=0.0 for i in range(nt):  x=rn.random()  y=rn.random()  x1=a+(b-a)*x  y1=-h2+(h1+h2)*y  y2=f(x1)  if y1<=y2:   ns=ns+1 A=ns/nt*(h1+h2)*(b-a)-h2*(b-a) print("The value of the integration is:",A)