Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.77 KB | None | 0 0
  1. import timeit
  2. from time import ctime
  3. import numpy as np
  4. - text - text - text - text - text - text - text - text - text - text - text - text
  5. that store the value of h.
  6. that store the value of k.
  7. that store value of intervals.
  8. that store the value from the prompt. that store the value of end of file. that store the value of a.
  9. that store the xcoordinates. that store the ycoordinates. that store the appproximate area. that store the actual area.
  10. that store the error between actual and approximate area. that store the amount of time taken by program.
  11. def getPositiveNumber(prompt, EOF):
  12.     while True:
  13. number = input(prompt).strip() if number !='':
  14.             try:
  15.                 number = eval(number, {}, {})
  16.             except:
  17.                 print(number,'is not valid!')
  18.             else:
  19.                 if number==0:
  20.                     return 0
  21.                     break
  22.                 elif type(number) is bool:
  23.                     print(number, 'is not number!')
  24.                 elif type(number) is int or float:
  25.                     if number > 0:
  26. break else:
  27.                         print(number, 'is less than zero!')
  28.                 else:
  29.                     print(number, 'is not number!')
  30.  
  31.         else:
  32.             print('Missing Input!')
  33.     if prompt==0:
  34.         return EOF
  35.     else:
  36.         return number
  37. def parabola(h, k, xCoordinates): a = -k/(h**2)
  38. ycoordinates = a*((xCoordinates - h)**2) + k return ycoordinates
  39. def computeXcoordinates(h, intervals):
  40. xcoordinates = np.linspace( 0,2*h,(intervals)+1) return xcoordinates
  41. def computeArea(h, k, intervals):
  42. xCoordinates = computeXcoordinates(h, intervals) yCoordinates = parabola(h, k, xCoordinates) approxarea = 0.
  43. approxarea += sum(yCoordinates[1:]+yCoordinates[:-1])
  44.     approxarea *= h/intervals
  45. actarea = 4 * h * (k / 3) error = abs(approxarea-actarea)
  46. print("""\nApproximate area under the parabola is %.14e cm^2 Actual area is %.14e cm^2
  47. The error in the approximate area is %.6e cm^2""" % (approxarea,actarea,error))
  48. def displayTerminationMessage():
  49.     print("""
  50. Programmed by Manjinder Singh, Harsh Ahir. Date: %s
  51. End of processing.""" % ctime())
  52. EOF = -1 while True:
  53.     h = getPositiveNumber('Enter the value of h in cm (0 to quit): ', EOF)
  54.     if h>0:
  55.         k = getPositiveNumber('Enter the value of k in cm (0 to quit): ', EOF)
  56.         if k>0:
  57.             intervals = getPositiveNumber('Enter the number of intervals in cm
  58. (0 to quit): ', EOF)
  59. intervals')
  60. if intervals>0:
  61. t = timeit.Timer('computeArea(h,k,intervals)',
  62. 'from __main__ import computeArea, h, k ,
  63. computeTime = t.timeit(1)
  64. print ("Compute time using vector arithmetic is %.3f seconds." %
  65. (computeTime) )
  66.             else:
  67.  
  68. break else:
  69. break
  70. else: break
  71. while EOF:
  72.     break
  73. displayTerminationMessage()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement