Skip to main content

Posts

Showing posts from August, 2019

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)

Program to evaluate integration using Monte carlo method:

#Program to evaluate integration using Monte carlo method: from math import* def f(x):  y=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) h=h1*1.2 ns=0.0 for i in range (nt):  x=rn.random()  y=rn.random()  x1=a+(b-a)*x  y1=h*y  y2=f(x1)  if y1<=y2:   ns=ns+1 A=ns/nt*h*(b-a) print("The value of the intergration is:",A)

Program to determine the value of slope by fitting the supplied data with a theoritical

# Program to determine the value of slope by fitting the supplied data with a theoritical # function y=mx+c f1=open("lin1.d","r") f2=open("least.d","w") b=0 a=0 q1=0 q2=0 y1=[] x=[] y=[] for k in f1:  z=k.split()  x.append(eval(z[0]))  y.append(eval(z[1])) print("The entered values of the x's are:\n",x,"\nThe entered values of the y's are:\n",y) n=len(x) for i in range (0,n):  b=b+x[i]  a=a+x[i]*x[i]  q1=q1+x[i]*y[i]  q2=q2+y[i] m=(q1*n-q2*b)/(a*n-b*b) c=(a*q2-b*q1)/(a*n-b*b) print("The value of the slope is:",m,"\nThe value of the c is:",c) for i in range(0,n):  k=m*x[i]+c  y1.append(k)  print(x[i],y[i],y1[i],file=f2) f2.close()

Program to define a function which calculates the trace of a matrix:

#Program to define a function which calculates the trace of a matrix: def imatrix(m):  A=[]  for i in range(m):   a=input("Enter the elements of row "+str(i+1)+" :").split(",")   b=list(map(float,a))   A.append(b)  return A def showm(A):  for i in range (m):   print (A[i]) def tracem(A):  k=0  for i in range (m):   k=k+A[i][i]  return k m=input("Enter the dimension of the matrix:") m=int(m) X=imatrix(m) print("The entered matrix is:") showm(X) Y=tracem(X) print ("The trace of the matrix is:",Y)

Program to find root using secant method for f(x)=x-3(1-exp(-x))

#Program to find root using secant method for f(x)=x-3(1-exp(-x)) from math import* def f(x):  y=x-tan(x)  return y a=input("Enter the 1st value:") b=input("Enter the 2nd value:") a=float(a) b=float(b) c=(a*f(b)-b*f(a))/(f(b)-f(a))*1.0 s=0 while abs((c-b)/c)>0.00000000001 and s<500:  a=b  b=c  c=(a*f(b)-b*f(a))/(f(b)-f(a))*1.0  s=s+1 print("The root for the given function is =",c,"\nThe No. of steps required:",s) print("Function's value at point of root=",f(c))

Program to find root by Newton-Raphson method for y=x-tan(x):

#Program to find root by Newton-Raphson method for y=x-tan(x): from math import* def f(x):  y=x-tan(x)  return y def f1(x):  y1=1-1/(cos(x)*cos(x))  return y1 x=input("Enter the initial guess,x0=") x=float(x) s=0 while abs(f(x))>0.000001 and s<500:  h=float(f(x))/float(f1(x))  x=x-h  s=s+1 print ("The solution for the given function is:",x,"\nNo. of iterations required:",s)

Program to find root using Regula Falsi method for f(x)=x-tan(x)

#Program to find root using Regula Falsi method for f(x)=x-tan(x) from math import* def f(x):  y=x-tan(x)  return y a=input("Enter the upper limit:") b=input("Enter the lower limit:") a=float(a) b=float(b) while f(a)*f(b)>0:  print("The root of the function does not lie between the limits.")  a=input("Enter the new upper limit:")  b=input("Enter the new lower limit:")  a=float(a)  b=float(b) s=0 while (f(a)*f(b))<0:  c=(a*f(b)-b*f(a))/(f(b)-f(a))  if abs(f(c))<0.00000000001 or (a-b)<.0000001 :   break  if f(a)*f(c)<0:   b=c  else:   a=c  s=s+1 print("The solution of the function is=",c,"\nRequired No. of steps:",s) print("The value of the function at the point of solution is=",f(c))

Program to find root using Bisection method for f(x)=x-tan(x):

#Program to find root using Bisection method for f(x)=x-tan(x): from math import* def f(x):  y=x-tan(x)  return y a=input("Enter the lower limit:") b=input("Enter the upper limit:") a=float(a) b=float(b) while f(a)*f(b)>0:  print("The root for the given function does not lie between the limits.")  a=input("Enter new lower limit:")  b=input("Enter new upper limit:")  a=float(a)  b=float(b) s=0 while (f(a)*f(b))<0:  c=(a+b)/2.0  if abs(f(c))<0.0000001:   break  if f(a)*f(c)<0:   b=c  else:   a=c  s=s+1 print("the root of the function is=",c,"\nThe required No. of steps:",s) print("The value of the function at point of root is=",f(c))

Program to find roots by using Newton Raphson for f(x)=x-3(1-exp(-x))

#Program to find roots by using Newton Raphson for f(x)=x-3(1-exp(-x)) from math import* def f(x):  y=x-3*(1-exp(-x))  return y def f1(x):  y=1-3*exp(-x)  return y x=input("Enter the initial guess value,x0=") x=float(x) s=0 while abs(f(x))>.000001 and s<50:  h= f(x)/float(f1(x))  x=x-h  s=s+1 print("The value of the root for the function is:",x,"\nRequired No. of iteration:",s)

Program to find root using secant method for f(x)=x-3(1-exp(-x))

#Program to find root using secant method for f(x)=x-3(1-exp(-x)) from math import* def f(x):  y=x-3*(1-exp(-x))  return y a=input("Enter the 1st value:") b=input("Enter the 2nd value:") a=float(a) b=float(b) c=(a*f(b)-b*f(a))/(f(b)-f(a))*1.0 s=0 while abs(f(c))>0.00000000001 and s<50:  a=b  b=c  c=(a*f(b)-b*f(a))/(f(b)-f(a))  s=s+1 print("The root for the given function is =",c,"\nThe No. of steps required:",s) print("Function's value at point of root=",f(c))

Program to find root using Regula Falsi method for f(x)=x-3(1-exp(-x))

#Program to find root using Regula Falsi method for f(x)=x-3(1-exp(-x)) from math import* def f(x):  y=x-3*(1-exp(-x))  return y a=input("Enter the upper limit:") b=input("Enter the lower limit:") a=float(a) b=float(b) while f(a)*f(b)>0:  print("The root for the given function does not exist between the entered limits.")  a=input("Enter the new upper limit:")  b=input("Enter the new lower limit:")  a=float(a)  b=float(b) s=0 while (f(a)*f(b))<0:  c=(a*f(b)-b*f(a))/(f(b)-f(a))  if abs(f(c))<0.00000000001 or (a-b)<.0000001 :   break  if f(a)*f(c)<0:   b=c  else:   a=c  s=s+1 print("The solution of the entered function is =",c,"\nRequired No. of steps:",s) print("The value of the function at the point of solution is=",f(c))

Program to find root using Bisection method for y=x-3(1-exp(-x)):

#Program to find root using Bisection method for y=x-3(1-exp(-x)): from math import* def f(x):  y=x-3*(1-exp(-x))  return y a=input("Enter the lower limit:") b=input("Enter the upper limit:") a=float(a) b=float(b) while f(a)*f(b)>0:  print ("Root of this given function does not exist between the entered limits.")  a=input("Enter the new lower limit:")  b=input("Enter the new upper limit:")  a=float(a)  b=float(b) s=0 while (f(a)*f(b))<0:  c=(a+b)/2.0  if abs(f(c))<0.00000000001:   break  if f(a)*f(c)<0:   b=c  else:   a=c  s=s+1 print("The root of the given function is=",c,"\nRequired No. of steps:",s) print("The value of the function at the point of root is=",f(c))

Program to find root by Newton-Raphson method for y=x-tanh(2x):

#Program to find root by Newton-Raphson method for y=x-tanh(2x): from math import* def f(x):  y=x-tanh(2*x)  return y def f1(x):  y1=1-(2/(cosh(2*x)*cosh(2*x)))  return y1 x=input("Enter the initial guess,x0=") x=float(x) s=0 while abs(f(x))>0.000001 and s<500:  h=float(f(x))/float(f1(x))  x=x-h  s=s+1 print ("The solution for the given function is:",x,"\nNo. of iterations required:",s)

Program to find root by secant method for y=x-tanh(2x):

#Program to find root by secant method for y=x-tanh(2x): from math import* def f(x):  y=x-tanh(2*x)  return y a=input("Enter the upper limit:") b=input("Enter the lower limit:") a=float(a) b=float(b) c=(a*f(b)-b*f(a))/(f(b)-f(a))* 1.0 s=0 while abs(f(c))>0 & s<50:  a=b  b=c  c=(a*f(b)-b*f(a))/(f(b)-f(a))  s=s+1 print("The root of the function is=",c,"\nThe No. of required steps:",s) print("Function's value at the point of root=",f(c))

Program to find root by regula falsi method for function y=x-tanh(2x):

#Program to find root by regula falsi method for function y=x-tanh(2x): from math import* def f(x):  y=x-tanh(2*x)  return y a=input("Enter the value of upper limit:") b=input("Enter the value of lower limit:") a=float(a) b=float(b) while f(a)*f(b)>0:  print("The roots of the given function does not lie between the entered limits.")  a=input("Enter the new upper limit:")  b=input("Enter the new lower limit:")  a=float(a)  b=float(b) s=0 while (f(a)*f(b))<0:  c=(a*f(b)-b*f(a))/(f(b)-f(a))  if abs(f(c))<0.00000000001 or (a-b)<.0000001 :   break  if f(a)*f(c)<0:   b=c  else:   a=c  s=s+1 print ("The solution of the function is=",c, "\nThe required No. of steps:",s) print ("The value of the function at the point of solution is=",f(c))

Program to find root by Bisection method for the function y=x-tanh(2x)

#Program to find root by Bisection method for the function y=x-tanh(2x): from math import* def f(x):  y=x-tanh(2*x)  return y a=input("Enter the lower limit:") b=input("Enter the upper limit:") a=float(a) b=float(b) while f(a)*f(b)>0:  print("The root of the given function does not lie between the entered limits.")  a=input("Enter the new lower limit:")  b=input("Enter the new upper limit:")  a=float(a)  b=float(b) s=0 while(f(a)*f(b))<0:  c=(a+b)/2.0  if abs(f(c))<0.00000000001:   break  if f(a)*f(c)<0:   b=c  else:   a=c  s=s+1 print ("The root of the given function is=",c,"\nRequired No. of steps:",s) print ("The value of the function at the point of root is=",f(c))

Program to find root of a function by Bisection method (y=x^2-x-2)

#Program to find root of a function by Bisection method (y=x^2-x-2): def f(x):  y=x*x-x-2  return y a=input("Enter the lower limit:") b=input("Enter the upper limit:") a=float(a) b=float(b) while f(a)*f(b)>0:  print("The root of the given function does not lie in the entered limits.")  a=input("Enter the new lower limit:")  b=input("Enter the new upper limit:")  a=float(a)  b=float(b) while(f(a)*f(b))<0:  c=(a+b)/2.0  if abs(f(c))<0.0000001:   break  if f(a)*f(c)<0:   b=c  else:   a=c print("The root of the given function is:",c)

Program to evaluate integration by Trapezoidal Rule and simpson's (1/3) rule and to compute the exact value and find the relative error:

#Program to evaluate integration by Trapezoidal Rule and simpson's (1/3) rule and to compute the exact value and find the relative error: def f(x):  y=1-exp(-x)  return y a=0 b=1 n=input("Enter the no. of divisions:") n=int(n) h=(b-a)/(n*1.) s=0 from math import* for i in range(1,n):  s=s+f(a+i*h) y=(h/2)*(f(a)+f(b)+2*s) print("The value of integration using trapezoidal rule is:",y) r=abs(exp(-1)-y) print("Error obtained in trapezoidal method is:",r) z=0 c=0 for i in range(1,n,2):  z=z+f(a+i*h) for i in range(2,n-1,2):  c=c+f(a+i*h) g=(h/3)*(f(a)+f(b)+4*z+2*c) print("The value of integration using Simpson's 1/3 rule is:",g) v=abs(exp(-1)-g) print("Error obtained in Simpson's 1/3 method is:",v)

Program to evaluate the integration using Simpson's (1/3) rule for f(x)=1-exp(-x):

# Program to evaluate the integration using Simpson's (1/3) rule for f(x)=1-exp(-x): f1=open("le.d","w") from math import* def f(x):  y=1-exp(-x)  return y a=0 Y=[] B=[] V=[] h=0.001 for n in range(0,5001,50):  b=a+n*h  v=b+exp(-b)-1  p=0  q=0  for j in range(1,n,2):   p=p+f(a+j*h)  for k in range(2,n-1,2):   q=q+f(a+k*h)  y=h/3*(f(a)+f(b)+4*p+2*q)  print(b,y,file=f1)  Y.append(y)  B.append(b)  V.append(v) f1.close() import matplotlib.pyplot as plt plt.plot(B,Y,'r',B,V,'g') #plt.show()

Program to evaluate integration using Trapezoidal rule

#Program to evaluate integration using Trapezoidal rule from math import* def f(x):  y=exp(-x*x)  return y a=input("Enter the lower limit:") b=input("Enter the upper limit:") n=input("Enter the no. of divisions:") a=float(a) b=float(b) n=int(n) h=(b-a)/(n*1.) s=0 for i in range(1,n):  s=s+f(a+i*h) y=(h/2)*(f(a)+f(b)+2*s) print("The value of the integration is:",y)

Program to define a function which calculates the transpose of a square matrix

#Program to define a function which calculates the transpose of a square matrix and test it with main. def imatrix(m):  A=[]  for i in range(m):   a=input("Enter the data of row "+str(i+1)+" of the matrix:").split(",")   b=list(a)   A.append(b)  return A def showm(A):  m=len(A)  for i in range (m):   print(A[i]) def transm(A):  m=len(A)  n=len(A[0])  B=[[0 for i in range (m)] for j in range (n)]  for i in range (m):   for j in range (n):    B[j][i]=A[i][j]  return B m=input("Enter the dimension of the square matrix:") m=int(m) X=imatrix(m) print("The matrix is:") showm(X) print("Transpose of the matrix is:") Y=transm(X) showm(Y)

Program to read number from file containing some repetition.Find the frequency distribution

# Program to read number from file containing some repetition.Find the frequency distribution and # hence the mode of the distribution.write the frequency distribution in a different file. f1=open("fd.d","w") n=input("Enter the no. of dimention:") n=int(n) x=[] for i in range (n):  s=input("Enter the value:")  s=float(s)  x.append(s) for i in range (n):  for j in range (i+1,n):   if x[i]>x[j]:    a=x.pop(j)    x.insert(i,a) print("The list of numbers in ascending series=",x,file=f1) f1.close() f1=open("fd.d","r") V=[x[0]] F=[] f=1 for i in range (1,n):  if x[i]==V[-1]:   f=f+1  else:   F.append(f)   f=1   V.append(x[i]) F.append(f) s=len(V) e=[] for i in range(1,s-1):  if F[i]>F[i-1] and F[i]>F[i+1]:   e.append(V[i]) f2=open("fd2.d","w") for k in range (s):  print("For entered value",V[k],"the corresponding frequency is=",F[k],file=f2) print("The mode of the dist

Program to read names, surnames from a file and arrange them in alphabetical order of surnames

# Program to read names, surnames from a file and arrange them in alphabetical order of surnames # and write them in an another file. n=input("Enter the no of peoples:") n=int(n) f1=open("name.d","w") for i in range (n):  s=input("Enter the name of people "+str(i+1)+":")  print(s,file=f1) f1.close() f1=open("name.d","r") name=[] sname=[] for k in f1:  x=k.split()  name.append(x[0])  sname.append(x[1]) for j in range (n):  for i in range(n-j-1):   if sname[i]>sname[i+1]:    sname[i],sname[i+1]=sname[i+1],sname[i]    name[i],name[i+1]=name[i+1],name[i] f2=open("sname_arranged.d","w") for k in range (n):  print(name[k],sname[k],file=f2) f2.close()

Program to determine the median of n given numbers:

#Program to determine the median of n given numbers: n=input("Enter the no. of terms:") n=int(n) x=[] for i in range (0,n):  m=input("enter the no."+str(i+1)+":")  m=float(m)  x.append(m) for i in range (0,n):  for j in range(i+1,n):   if x[i]>x[j]:    a=x.pop(j)    x.insert(i,a) print("Sorted elements in ascending order are:\n",x) if n%2==0:  c=(x[int(n/2)]+x[int((n/2)-1)])/2.0 else:  c=x[int((n-1)/2)] print("Median of the given numbers is:",c)

Program to implement Insertion sort algorithm:

#Program to implement Insertion sort algorithm: n=input("Enter the no. of terms:") n=int(n) x=[] m=0 for i in range (0,n):  m1=input("Enter the no."+str(i+1)+":")  m1=float(m1)  x.append(m1) for i in range (0,n):  for j in range(i+1,n):   if x[i]>x[j]:    a=x.pop(j)    x.insert(i,a)    m=m+1 print("Sorted elements in ascending order are\n",x,"\nNo. of swapping required:",m)

Program to implement the Selection sort algorithm:

# Program to implement the Selection sort algorithm: n=input("Enter the no. of terms:") n=int(n) x=[] m=0 for i in range (0,n):  m1=input("Enter the no."+str(i+1)+":")  m1=float(m1)  x.append(m1) for i in range (0,n):  a=i  s=x[i]  for j in range (i+1,n):   if s>x[j]:    a=j    s=x[j]  t=x[i]  x[i]=x[a]  x[a]=t  m=m+1 print("Selection sort of entered no. in ascending order is:\n",x,"\nNo of steps required:",m)

Program to arrange n numbers in ascending order using Bubble sort algorithm:

#Program to arrange n numbers in ascending order using Bubble sort algorithm: n=input("Enter the no. of data:") n=int(n) x=[] s=0 for i in range(n):  m=input("Enter the number "+str(i+1)+":")  m=float(m)  x.append(m) for i in range(0,n,1):   for j in range (0,n-i-1,1):     if x[j]>x[j+1]:       x[j],x[j+1]=x[j+1],x[j]       s=s+1 print("The entered numbers in ascending order is:\n",x,"\nNo. of swapping required:",s)

Program to read two vectors of dimension n and determine the sum and dot product of them:

# Program to read two vectors of dimension n and determine the sum and dot product of them: n=input("Enter the number of dimension:") n=int(n) x=[] y=[] z=[] t=0 for i in range(1,n+1):  s=input("Enter the component "+str(i)+" of the first vector:")  s=float(s)  x.append(s) for i in range(1,n+1):  d=input("Enter the component "+str(i)+" of the second vector:")  d=float(d)  y.append(d) for i in range(0,n):  c=x[i]+y[i]  z.append(c) for i in range(0,n):  p=x[i]*y[i]  t=t+p print("The sum of the entered vectors are:",z) print("The dot product of the entered vectors are:",t)

Program to perform tasks similar to the string method: count() and find():

#Program to perform tasks similar to the string method: count() and find(): x=input("Enter the string:") y=input("Enter the character you want to find:") z=len(x) k=0 for i in range(0,z):  if x[i]==y:   k=k+1   if k==1:    z=i print("The total no. of occurance is:",k) print("The first occureuce position is in:",z+1)

Program to determine the value of golden ratio (phi)

#                                                                                                    t_n+1 # Program to determine the value of golden ratio (phi) = lim   --------- #                                                                                          n->inf  t_n n=input("Enter the decimal place upto which correction is required:") a=1.0 b=1.0 c=a+b R1=c/b R2=b/a e=R1-R2 n=float(n) while abs(e)>=n:  a=b  b=c  c=a+b  R1=c/b  R2=b/a  e=R1-R2 print("The value of the Golden ratio is",R1)

Program to verify if a given number belongs to Fibonacci sequence or not:

# Program to verify if a given number belongs to Fibonacci sequence or not: n=input("Enter a number to check if it belongs to Fibonacci series or not:") a=1 b=1 c=0 c=a+b n=int(n) while c<=n:  a=b  b=c  c=a+b if b==n:  print("Entered number belongs to Fibonacci series.") else:  print("Entered number does not belong to Fibonacci series.")

Program to find sum of the series i) 1/r and ii) 1/r^2 for n = 10,100,1000,10000,100000,1000000

# Program to find sum of the series i) 1/r and ii) 1/r^2 for n = 10,100,1000,10000,100000,1000000 n=input("Enter the no. of terms:") n=int(n) s=0 s1=0 for i in range (1,n):   s=s+1/(float(i))   s1=s1+(1/float(i))**2 print("The sum of 1/r series is=",s) print("The sum of 1/r^2 series is=",s1)

Program to input from the user, the market price per unit of purchased items and unit purchased

# Program to input from the user, the market price per unit of purchased items and unit purchased # until the user asks to stop. Calculate the total price. Give discount of 5% if the total price # is between 100 to 500, 10% if that between 500 to 1500,15% for that between 1500 to 3000 and 25% # if the total price is above 3000.Output the total price to be paid. z=input("Enter the no. of items:") z=int(z) c=0.0 if z>0:  for n in range (1,z+1):   y=input("Market price per unit of the item no."+str(n)+":")   y=float(y)   c=y+c  print("Total amount=",c)  if c>=100 and c<=500:   d=c*(100-5)/100  if c>=500 and c<=1500:   d=c*(100-10)/100  if c>=1500 and c<=3000:   d=c*(100-15)/100  if c>=3000:   d=c*(100-25)/100  print("The amount to be paid=",(round(d,2)))  print("Paid.")  print("Thank you, visit again.") else :  print("You have not purchased any item. Thank you, visit again.")

Program to input six components of two vectors and to determine

# Program to input six components of two vectors and to determine # i) the angle between them in degrees # ii) the norm of the difference vector # iii) if these vectors are OA and OB, find the area of the triangle AOB. # Co-ordinate of O is (0,0,0) # iv) find the another co-ordinate of the vertex of parallelogram whose three vertices are O, A, B. x1,y1,z1=input("Enter the components of vector A:").split(",") x2,y2,z2=input("Enter the components of vector B:").split(",") x1=float(x1) y1=float(y1) z1=float(z1) x2=float(x2) y2=float(y2) z2=float(z2) from math import* norm1=sqrt(x1**2+y1**2+z1**2) norm2=sqrt(x2**2+y2**2+z2**2) angle=degrees(acos((x1*x2+y1*y2+z1*z2)/(norm1*norm2))) print("The angle between these two vectors are:",angle) a=x1-x2 b=y1-y2 c=z1-z2 n=sqrt(a**2+b**2+c**2) print("The norm of the difference vector is:",n) s=sin(radians(angle)) area=(norm1*norm2)*s/2 print("The area of triangle AOB is:",area) x=x

Program to input a vector and determine i) the norm and ii)angle with each of the axes

# Program to input a vector and determine i) the norm and ii)angle with each of the axes x,y,z =input("Enter the values of x, y and z:").split(",") x=float(x) y=float(y) z=float(z) from math import * n=sqrt(x**2+y**2+z**2) x1=degrees(acos(x/n)) y1=degrees(acos(y/n)) z1=degrees(acos(z/n)) print("Norm of the entered vector=",n) print("Angle made with x axis in degrees:",x1) print("Angle made with y axis in degrees:",y1) print("Angle made with z axis in degrees:",z1)

Program to find solution of a quadratic equation

# Program to find solution of a quadratic equation a,b,c=input("Enter the values of a,b,c:").split(",") a=float(a) b=float(b) c=float(c) dis=b*b-4*a*c from math import * if dis>=0:  x1=(-b+sqrt(dis))/(2*a)  x2=(-b-sqrt(dis))/(2*a)  print("The solutions for the given equation are ",x1,"and",x2) else:  from cmath import *  xr=-b/(2*a)  xi=sqrt(dis)/(2*a)  print("The 1st solution for the given equation is ",xr,"+",xi)  print("The 2nd solution for the given equation is ",xr,"-",xi)