Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import timeit
- from time import ctime
- import numpy as np
- - text - text - text - text - text - text - text - text - text - text - text - text
- that store the value of h.
- that store the value of k.
- that store value of intervals.
- that store the value from the prompt. that store the value of end of file. that store the value of a.
- that store the xcoordinates. that store the ycoordinates. that store the appproximate area. that store the actual area.
- that store the error between actual and approximate area. that store the amount of time taken by program.
- def getPositiveNumber(prompt, EOF):
- while True:
- number = input(prompt).strip() if number !='':
- try:
- number = eval(number, {}, {})
- except:
- print(number,'is not valid!')
- else:
- if number==0:
- return 0
- break
- elif type(number) is bool:
- print(number, 'is not number!')
- elif type(number) is int or float:
- if number > 0:
- break else:
- print(number, 'is less than zero!')
- else:
- print(number, 'is not number!')
- else:
- print('Missing Input!')
- if prompt==0:
- return EOF
- else:
- return number
- def parabola(h, k, xCoordinates): a = -k/(h**2)
- ycoordinates = a*((xCoordinates - h)**2) + k return ycoordinates
- def computeXcoordinates(h, intervals):
- xcoordinates = np.linspace( 0,2*h,(intervals)+1) return xcoordinates
- def computeArea(h, k, intervals):
- xCoordinates = computeXcoordinates(h, intervals) yCoordinates = parabola(h, k, xCoordinates) approxarea = 0.
- approxarea += sum(yCoordinates[1:]+yCoordinates[:-1])
- approxarea *= h/intervals
- actarea = 4 * h * (k / 3) error = abs(approxarea-actarea)
- print("""\nApproximate area under the parabola is %.14e cm^2 Actual area is %.14e cm^2
- The error in the approximate area is %.6e cm^2""" % (approxarea,actarea,error))
- def displayTerminationMessage():
- print("""
- Programmed by Manjinder Singh, Harsh Ahir. Date: %s
- End of processing.""" % ctime())
- EOF = -1 while True:
- h = getPositiveNumber('Enter the value of h in cm (0 to quit): ', EOF)
- if h>0:
- k = getPositiveNumber('Enter the value of k in cm (0 to quit): ', EOF)
- if k>0:
- intervals = getPositiveNumber('Enter the number of intervals in cm
- (0 to quit): ', EOF)
- intervals')
- if intervals>0:
- t = timeit.Timer('computeArea(h,k,intervals)',
- 'from __main__ import computeArea, h, k ,
- computeTime = t.timeit(1)
- print ("Compute time using vector arithmetic is %.3f seconds." %
- (computeTime) )
- else:
- break else:
- break
- else: break
- while EOF:
- break
- displayTerminationMessage()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement