You must use lists in this question. Do not use arrays in this question. The pur
ID: 3916044 • Letter: Y
Question
You must use lists in this question. Do not use arrays in this question.
The purpose of this question is to write a complete Python program that computes the approximate area under a parabola between X = 0 and X = 2 × h and compares the approximate area to the actual area under the parabola.
The parabola shown above is created using equation (1).
y(x) = a(x - h)2 + k, where a = -k/h2 (1)
k is the largest Y coordinate of the parabola and h is the corresponding X coordinate. The X coordinates range from 0 to 2×h.
The actual area under the parabola is 4 × h × k / 3.
Write a function that begins with the following header:
def getPositiveNumber(prompt, EOF):
Call this function to get input from the user. Valid input is either an integer greater than zero or a float greater than zero. The function displays the prompt to tell the user what to enter. If the user does not enter valid input display the appropriate error message as described below and display the prompt again asking for more input from the user. Repeat this until the user enters valid input. When the user enters valid input return that input. If the user enters zero return the value of EOF.
Error situations:
• If the user presses enter/return without entering anything display the message 'Missing input!'
• If the input causes an exception when passed to eval display the value of the input and the message 'is not valid!'
• If the input is not an integer and is not a float display the value of the input and the message 'is not a number!'
• If the input is less than 0 display the value of the input and the message 'is less than 0!'
Write a function that begins with the following header:
def parabola(h, k, xCoordinates):
Given the values of h, k and a list of X coordinates this function creates a list of Y coordinates using equation (1). There will be one Y coordinate for each X coordinate. Return the list of Y coordinates.
Hint: You may find it useful to use list comprehension to create the list of Y coordinates.
Write a function that begins with the following header:
def computeXcoordinates(h, intervals):
Given the value of h and the number of intervals create a list of X coordinates. The first element in the list is 0 and the last element in the lists is 2 × h. The number of X coordinates in the list is intervals + 1. The X coordinates are equally spaced between 0 and 2 × h. Return the list of X coordinates.
Hint: You may find it useful to use list comprehension to create the list of X coordinates.
Write a function that begins with the following header:
def computeArea(h, k, intervals):
Call computeXcoordinates to get the list of X coordinates and then call parabola to
get the list of corresponding Y coordinates. Use these two lists to compute the
approximate area under the parabola using the trapezoid method. Calculate the
actual area under the parabola. Calculate the error in the approximation, which is
the absolute value of the difference between the actual and approximate areas.
Display the approximate area and the actual area to 14 decimal places using
exponential format. Display the error to 6 decimal places using exponential format.
See the sample run of the program for the exact format of the output. This function
does not return a value.
Note: There is NO function named main in this program!
The main program defines EOF to be -1.
In a loop:
• input the value of h from the user by calling getPositiveNumber, if the value
returned by getPositiveNumber is equal to EOF exit from the loop
• input the value of k from the user by calling getPositiveNumber, if the value
returned by getPositiveNumber is equal to EOF exit from the loop
• input the number of intervals from the user by calling getPositiveNumber, if
the value returned by getPositiveNumber is equal to EOF exit from the loop
• use the Timer and timeit functions from the timeit module to calculate the
time it takes to compute the area under the parabola.
• display the time it took to compute the area under the parabola to 3 decimal
places, see the sample output for the exact format of the output
Call displayTerminationMessage, as defined in assignment 2, to display the
termination message.
To use the Timer and timeit functions from the timeit module to calculate the time it
takes to calculate the area the curve use the following statements:
t = timeit.Timer('computeArea(h,k,intervals)',
'from __main__ import computeArea, h, k , intervals')
computeTime = t.timeit(1)
print "Compute time using lists is %.3f seconds." % computeTime
Do NOT type in the statements given above, cut and paste them from this
document!
There are two underscores before and after the word main in the statements given
above.
To use the Timer and timeit functions import the timeit module.
Explanation / Answer
from time import ctime
from math import sqrt
import numpy as np
import timeit
print '---------------------------------------------------------------------'
def displayTerminationMessage(): #Terminate the program.
print'''
programmed by Michael Oghenekome Victor.
Date: %s
End of processing.'''% ctime()
def getPositiveNumber(prompt,EOF):
'''return a positve number which is also an integer given a prompt and EOF'''
#number - The value for prompt to be assigned to.
#prompt - A string for getting input.
#EOF - End of file to control the loop
while True:
number = raw_input(prompt).strip()
if number != '':
try:
number = eval(number, {}, {})
except: # handle the error
print '%r is not valid!'%number
else: # no error, do this
if type(number) is int or type(number) is float:
if number<0:
print '%g is less than zero!'%number
else:
if number==0:
number= EOF
break
else:
break
else:
print '%r is not a number!'%number
else:
print 'missing input!'
return number
def computeXcoordinates(a, intervals):
'''A function to create and return an array of x values given the
value of the semi major values, a ,and number of intervals'''
#xValues - An array displaying x values
#a - length of the semi major axis
#intervals - the number of elements in a list or array
xValues= np.linspace(0,a,intervals+1)
return xValues
def computeYcoordinates (a, b, xValues):
'''A function to create and return a list of y values given the
value of the semi major values, a , the semi minor axis ,b ,and x values'''
#xValues - An array displaying x values
#yValues - An array displaying y values
#a - length of the semi major axis
#b - length of the semi minor axis
#x - the new element to be appended to the list of xValues
yValues=[]
for x in xValues:
y= b* sqrt(1-(x**2/a**2))
yValues.append(y)
return np.array(yValues)
def calcCircumference(a, b, intervals):
'''A function to create the circumference of an ellipse given the
value of the semi major values, a , the semi minor axis ,b ,and intervals'''
#xValues - An array displaying x values
#yValues - An array displaying y values
#yP - the value to which y gets assigned to after going through the loop
# successively
#y - the value for yValues to get assigned to
#a - length of the semi major axis
#b - length of the semi minor axis
#intervals - the number of elements in a list or array
#circumference - the circumference of a quater of the ellipse
#Circumference - the circumference of the ellipse
xValues= computeXcoordinates(a, intervals)
yValues= computeYcoordinates (a, b, xValues)
circumference=0
yP= b #first value for yP
deltaX= a/intervals
for y in yValues[1:]:
circumference+= sqrt(((yP-y)**2)+(deltaX**2))
yP=y
Circumference= circumference*4
print ' The approximate circumference of the ellipse is %.14e cm,'%Circumference
#a - length of the semi major axis
#b - length of the semi minor axis
#intervals - the number of elements in a list or array
#EOF - End of file to control the loop
#computeTime - A function that computes the time python took to run the program
#t - the actual time taken to run the program.
EOF = -1
print 'To terminate the program enter 0 after any prompt.'
while True:
a= float(getPositiveNumber('Enter the length of the semi-major axis in cm (> 0): ',EOF))
if a== EOF:
break #break the loop
b= float(getPositiveNumber('Enter the length of the semi-minor axis in cm (> 0): ',EOF))
if b== EOF:
break #break the loop
intervals= int(getPositiveNumber('Enter the number of intervals (> 0): ',EOF))
if intervals== EOF:
break #break the loop
#TIme computation
t = timeit.Timer('calcCircumference(a, b, intervals)', #displays the time
'from __main__ import calcCircumference, a, b, intervals') #to calculate the
computeTime = t.timeit(1) #circumference
print "The compute time using array is %.3f seconds." % computeTime
displayTerminationMessage()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.