# find_abcx_ver1.py # # 2016-01-26 #
# wrt f(x) = a*x*x + b*x + c
# (1) Since when x = 0, f(0) = 4 implies c = 4
# AND THUS f(x) = a*x*x + b*x + 4
# (2) Since when x = 24, f(24) = 0 implies 0 = 24*24*a + 24*b + 4
# then divide all by 24 ... 0 = 24*a + b + 1/6
# i.e. b = -(24*a + 1/6)
# i.e. f(x) = a*x*x - (24*a + 1/6)*x + 4
# thus b = -(24*a + 1/6)
# and c = 4
# wrt the form: f(x) = a*x*x + b*x + c
# (3) Now to find a = ? in f(x) = a*x*x - (24*a + 1/6)*x + 4
# SINCE the max value of 14 occurs when ...
# x = (24*a + 1/6)/(2*a)
# thus: 14 = (24*a + 1/6)^2/(4*a) - (24*a + 1/6)^2/(2*a) + 4
# i.e. 10 = (24*a + 1/6)^2/(4*a) - (24*a + 1/6)^2/(2*a)
# 40*a = - (24*a + 1/6)^2
# 40*a = - (24*24*a*a + 8*a + 1/36)
# 0 = (24*24*a*a + 48*a + 1/36)
# THUS a*a + (1/12)*a + 1/((6*24)*(6*24)) = 0
# i.e. a*a + (1/12)*a + 1/(12^4) = 0);
from math import sqrt
# roots = ( -b +- sqrt(b*b - 4*a*c) ) / (2*a)
# now define Python function to calculate and return both roots
def roots( a, b, c ):
return (-b + sqrt(b*b - 4*a*c))/(2*a), (-b - sqrt(b*b - 4*a*c))/(2*a)
a1, a2 = roots( 1, 1/12, 1/12**4 )
print( "In f(x) = a*x*x + b*x + c" )
print( "a takes TWO value, b = -24*a -1/6 and c = 4" )
print( "a1 =", a1, "and a2 =", a2 )
# a Python function to calculate 'b' from the passed in 'a' value
def b( a ):
return -24*a - 1/6
# a Python function to calculate 'f(x)' from the passed values for a, b, c and x
def f( a, b, c, x ):
return (a*x + b)*x + c
print( "\nCHECK: For a1 =", a1 )
print( "f(0) =", f(a1, b(a1), 4, 0), "and f(24) =", f(a1, b(a1), 4, 24) )
print( "\nCHECK: For a2 =", a2 )
print( "f(0) =", f(a2, b(a2), 4, 0), "and f(24) =", f(a2, b(a2), 4, 24) )
# if 10 = a*x**2 + b*x + 4
# i.e. 0 = a*x**2 + b*x - 6
print( "\nFor a1 =", a1, "and b1 =", b(a1), "and c = 4" )
x1, x2 = roots(a1, b(a1), -6)
print( "when x1 =", x1, "or x2 =", x2 )
print( "CHECK: f(x1) =", f(a1, b(a1), 4, x1), "and f(x2) =", f(a1, b(a1), 4, x2) )
print( "\nFor a2 =", a2, "and b2 =", b(a2), "and c = 4" )
x1, x2 = roots(a2, b(a2), -6)
print( "when x1 =", x1, "or x2 =", x2 )
print( "CHECK: f(x1) =", f(a2, b(a2), 4, x1), "and f(x2) =", f(a2, b(a2), 4, x2) )
input( "\nPress 'Enter' to continue/exit ... " )
"""
OUTPUT:
In f(x) = a*x*x + b*x + c
a takes TWO value, b = -24*a -1/6 and c = 4
a1 = -0.0005827792840304435 and a2 = -0.08275055404930289
CHECK: For a1 = -0.0005827792840304435
f(0) = 4.0 and f(24) = 0.0
CHECK: For a2 = -0.08275055404930289
f(0) = 4.0 and f(24) = -1.7763568394002505e-15
For a1 = -0.0005827792840304435 and b1 = -0.152679963849936 and c = 4
when x1 = -213.84017796050352 or x2 = -48.14573683388753
CHECK: f(x1) = 10.000000000000002 and f(x2) = 10.0
For a2 = -0.08275055404930289 and b2 = 1.8193466305166026 and c = 4
when x1 = 4.040400677928648 or x2 = 17.945514116462135
CHECK: f(x1) = 10.0 and f(x2) = 10.000000000000002
Press 'Enter' to continue/exit ... ...
"""
# find_abcx_ver2.py # # 2016-01-26 #
# (1) Since we are given 24 is a root of the quadratic, we can use the form:
# f(x) = a*(x-r)*(x-24)
# = a*( x*x -(r+24)*x + 24*r )
# (2) But when x = 0, f(0) = 4 implies 4 = a*24*r and that implies r = 1/(6*a)
# AND THUS f(x) = a*x*x - a*(1/(6*a) + 24)*x + a*24*(1/(6*a))
# OR f(x) = a*x*x - (1/6 + 24*a)*x + 4
# thus b = -(1/6 + 24*a) = -24*a - 1/6
# and c = 4
# re. form f(x) = a*x*x + b*x + c
# (3) Now to find a = ? in f(x) = a*x*x - (24*a+ 1/6)*x + 4
# SINCE the max value of 14 occurs when x = (24*a + 1/6)/(2*a)
# thus: 14 = (24*a + 1/6)^2 /(4*a) - (24*a + 1/6)^2 /(2*a) + 4
# i.e. 10 = (24*a + 1/6)^2 /(4*a) - (24*a + 1/6)^2 /(2*a)
# 10 = - (24*a + 1/6)^2 /(4*a)
# 40*a = - (24*24*a*a + 8*a + 1/36)
# 0 = 24*24*a*a + 48*a + 1/(6*6)
# THUS a*a + (1/12)*a + 1/((6*24)*(6*24)) = 0
# i.e. a*a + (1/12)*a + 1/(12^4) = 0
# define a Python function to find and return both of the roots ...
def roots( a, b, c ):
return (-b + (b*b - 4*a*c)**.5)/(2*a), (-b - (b*b - 4*a*c)**.5)/(2*a)
a1, a2 = roots( 1, 1/12, 1/12**4 )
print( "In f(x) = a*x*x + b*x + c" )
print( "a takes TWO value, b = -24*a -1/6 and c = 4" )
print( "a1 =", a1, "and a2 =", a2 )
# define a Python function to find and return the (other) 'r' for this passed in 'a'
# from (2) above ...
def r( a ):
return 1/(6*a)
print( "\nIn f(x) = a*(x-r)*(x-24), since f(0) = 4, THUS r = 1/(6*a)" )
print( "r(a1) =", r(a1), "and r(a2) =",r(a2) )
# define a Python function to find and return 'b' for the passed in value of 'a' ...
def b( a ):
return -24*a - 1/6
# define a Python function to evaluate f(x) for passed in values for a,b,c,x ...
def f( a, b, c, x ):
return (a*x + b)*x + c
print( "\nCHECK: For a1 =", a1 )
print( "f(0) =", f(a1, b(a1), 4, 0), "and f(24) =", f(a1, b(a1), 4, 24) )
print( "f(0) =", a1*(0-r(a1))*(0-24), "and f(24) =", a1*(24-r(a1))*(24-24) )
print( "\nCHECK: For a2 =", a2 )
print( "f(0) =", f(a2, b(a2), 4, 0), "and f(24) =", f(a2, b(a2), 4, 24) )
print( "f(0) =", a2*(0-r(a2))*(0-24), "and f(24) =", a2*(24-r(a2))*(24-24) )
# Now re...
# 10 = a*x*x + b*x + 4
# 0 = a*x*x + b*x - 6
print( "\nFor a1 =", a1, "and b1 =", b(a1), "and c = 4" )
x1, x2 = roots(a1, b(a1), -6)
print( "when x1 =", x1, "or x2 =", x2 )
print( "CHECK: f(x1) =", f(a1, b(a1), 4, x1), "and f(x2) =", f(a1, b(a1), 4, x2) )
print( "\nFor a2 =", a2, "and b2 =", b(a2), "and c = 4" )
x1, x2 = roots(a2, b(a2), -6)
print( "when x1 =", x1, "or x2 =", x2 )
print( "CHECK: f(x1) =", f(a2, b(a2), 4, x1), "and f(x2) =", f(a2, b(a2), 4, x2) )
input( "\nPress 'Enter' to continue/exit ... " )
"""
OUTPUT:
In f(x) = a*x*x + b*x + c
a takes TWO value, b = -24*a -1/6 and c = 4
a1 = -0.0005827792840304435 and a2 = -0.08275055404930289
In f(x) = a*(x-r)*(x-24), since f(0) = 4, THUS r = 1/(6*a)
r(a1) = -285.9859147943911 and r(a2) = -2.014085205609215
CHECK: For a1 = -0.0005827792840304435
f(0) = 4.0 and f(24) = 0.0
f(0) = 4.0 and f(24) = -0.0
CHECK: For a2 = -0.08275055404930289
f(0) = 4.0 and f(24) = -1.7763568394002505e-15
f(0) = 4.0 and f(24) = -0.0
For a1 = -0.0005827792840304435 and b1 = -0.152679963849936 and c = 4
when x1 = -213.84017796050352 or x2 = -48.14573683388753
CHECK: f(x1) = 10.000000000000002 and f(x2) = 10.0
For a2 = -0.08275055404930289 and b2 = 1.8193466305166026 and c = 4
when x1 = 4.040400677928648 or x2 = 17.945514116462135
CHECK: f(x1) = 10.0 and f(x2) = 10.000000000000002
Press 'Enter' to continue/exit ...
"""
# find_max_and_time.py # # 2016-01-31 #
"""
Solve: –4.9t^2 + 50t + 1.5 = 0
by using algebra ... and by graphing h = –4.9t^2 + 50t + 1.5 using technology.
Note alternate (VERTEX) form:
h = -4.9( t - 50/(4.9*2) )^2 + (50^2/(4*4.9) + 1.5)
Given the graph or the equation of the above quadratic relation,
representing the height of a ball over elapsed time,
answer questions such as the following:
1) What is the maximum height of the ball? ==> k = (50^2/(4*4.9) + 1.5) = 129.1
2) After what length of time will the ball hit the ground? (10.23)
3) Over what time interval is the height of the ball greater than 3 m
t1 = 0.0301, t2 = 10.17
"""
# if h = –4.9t2 + 50t + 1.5
def h( a, b, c, t ):
return (a*t + b)*t + c
def roots( a, b, c ):
return (-b + (b*b - 4*a*c)**.5)/(2*a), (-b - (b*b - 4*a*c)**.5)/(2*a)
t1, t2 = roots( -4.9, 50, 1.5 )
print( "t1 =", t1, "t2 =", t2 )
print( "Flight ends after", max(t1,t2), "units of time from 'time 0'." )
# max occurs at t = -b/(2a)
tmax = -50/(2*(-4.9))
print( "Max height was", h( -4.9, 50, 1.5, tmax), "units. CHECK =", 50**2/(4*4.9) + 1.5 )
#if h = 3,
t3, t4 = roots( -4.9, 50, -1.5 )
print( "t3 =", t3, "t4 =", t4 )
print( "Time interval flight was above '3' was", abs(t4-t3), "units of time." )
input( "\nPress 'Enter' to continue/exit ... " )
"""
OUTPUT:
t1 = -0.029912314835277086 t2 = 10.233993947488337
Flight ends after 10.233993947488337 units of time from 'time 0'.
Max height was 129.05102040816325 units. CHECK = 129.05102040816325
t3 = 0.030088722459485746 t4 = 10.173992910193574
Time interval flight was above '3' was 10.143904187734089 units of time.
Press 'Enter' to continue/exit ...
"""
# lines_1.py # # 2016-03-19 #
'''
For 'slope, y-intercept form' (m is slope, b is y-intercept)
y = m*x + b
or (inverse) ...
x = (y-b)/m
y(at x=0) = b
x(at y=0) = -b/m
For 2 point form, (x1,y1) and (x2,y2) ...
determine the line ?
Firstly ...
m = (y2-y1)/(x2-x1)
y2 = m*x2 + b implies that ...
b = y2 - m*x2
= y2 - (y2-y1)/(x2-x1) * x2
THUS ...
y = m*x + b implies ...
y = (y2-y1)/(x2-x1) * x + y2 - (y2-y1)/(x2-x1) * x2
y - y2 = (y2-y1)/(x2-x1) * x - (y2-y1)/(x2-x1) * x2
y - y2 = m * (x - x2 )
and/or we also can find the equation of the line using (x1, y1) ...
(if given 2 distinct points (x1, y1) and (x2, y2) that determine the line)
y - y1 = m * (x - x1 )
(Recall that the slope 'm' here is given by: (y2-y1) / ((x2-x1)
Or if start with ... A*x + B*y + C = 0
(If B != 0) same as: (A/B)*x + y + C/B = 0
and same as: y = -(A/B)*x - C/B
thus ... m = -A/B and b = -C/B
'''
def slopeIntercept( A, B, C ):
return -A/B, -C/B
A, B, C = 1, 2, 3;
m, b = slopeIntercept( A, B, C )
print( "For equation of line with form: A*x + B*y + C = 0" )
print( "For A = {}, B = {}, C = {}, slope m = {:.2e}, intercept b = {:.2e}".
format( A, B, C, m, b ) )
more = True
while more :
line = input( "Enter A B C: " ) # prompt and take in a line #
# split the line elements separated by spaces,
# into a list of strings
lst = line.split()
goodData = True
newLst = []
try:
newLst = [eval(stg) for stg in lst]
except:
goodData = False
print( "\nNumbers only please ... Try again.\n" )
if not goodData:
continue # start at top of 'while loop' #
if len(newLst) != 3:
print( "\nMust enter 3 and ONLY 3 numbers ... Try again.\n" )
continue # start at top of 'while loop' #
A, B, C = tuple(newLst)
if B == 0:
print( "\nB can NOT be zero ... Try again.\n" )
continue # start at top of 'while loop' #
try:
m, b = slopeIntercept( A, B, C )
except:
goodData = False
if not goodData:
print( "\nData entry error ... Try again, 3 numbers, spaces between.\n" )
continue # start at top of 'while loop' #
print( "For A = {}, B = {}, C = {}, slope m = {:.2e}, intercept b = {:.2e}".
format( A, B, C, m, b ) )
if input( "More (y/n) ? " ).lower() == "n":
more = False
# lines_2.py # # 2016-03-19 #
'''
For 'slope, y-intercept form' (m is slope, b is y-intercept)
y = m*x + b
or (inverse) ...
x = (y-b)/m
y(at x=0) = b
x(at y=0) = -b/m
For 2 point form, (x1,y1) and (x2,y2) ...
determine the line ?
Firstly ...
m = (y2-y1)/(x2-x1)
y2 = m*x2 + b implies that ...
b = y2 - m*x2
= y2 - (y2-y1)/(x2-x1) * x2
THUS ...
y = m*x + b implies ...
y = (y2-y1)/(x2-x1) * x + y2 - (y2-y1)/(x2-x1) * x2
y - y2 = (y2-y1)/(x2-x1) * x - (y2-y1)/(x2-x1) * x2
y - y2 = m * (x - x2 )
Or if start with ... A*x + B*y + C = 0
(If B != 0) same as: (A/B)*x + y + C/B = 0
and same as: y = -(A/B)*x - C/B
thus ... m = -A/B and b = -C/B
'''
def slopeIntercept( A, B, C ):
return -A/B, -C/B
A, B, C = 1, 2, 3;
m, b = slopeIntercept( A, B, C )
print( "For equation of line with form: A*x + B*y + C = 0" )
print( "For A = {}, B = {}, C = {}, slope m = {:.2e}, intercept b = {:.2e}".
format( A, B, C, m, b ) )
A2, B2, C2 = 2, 3, 4;
m2, b2 = slopeIntercept( A2, B2, C2 )
print( "For A = {}, B = {}, C = {}, slope m = {:.2e}, intercept b = {:.2e}".
format( A2, B2, C2, m2, b2 ) )
'''
At intersection y1 is same y2 and this implies ...
set m*x + b equal to m2*x + b2 ... and this implies ...
m*x - m2*x = b2 - b
x = (b2 - b) / (m - m2 )
y = m*x + b
'''
def intersection( m, b, m2, b2 ):
x = (b2 - b) / (m - m2 )
return x, m*x + b
x, y = intersection( m, b, m2, b2 )
print( "The lines intersect at ({:.2e},{:.2e})".format( x, y ) )
def getABC( msg ):
while( True ):
line = input( msg ) # prompt and take in a line #
# split the line elements separated by spaces,
# into a list of strings
lst = line.split()
numsOk = True
newLst = []
try:
newLst = [eval(stg) for stg in lst]
except:
numsOk = False
print( "\nNumbers only please ... Try again.\n" )
if not numsOk:
continue # start at top of 'while loop' #
if len(newLst) != 3:
print( "\nMust enter 3 and ONLY 3 numbers ... Try again.\n" )
continue # start at top of 'while loop' #
A, B, C = tuple(newLst)
if B == 0:
print( "\nB can NOT be zero ... Try again.\n" )
else:
break
return A, B, C
more = True
while more:
A, B, C = getABC( "Enter A B C: " )
try:
m, b = slopeIntercept( A, B, C )
except:
print( "\nBad data entry ... Try again, 3 numbers, spaces between.\n" )
continue
print( "For A = {}, B = {}, C = {}, slope m = {:.2e}, intercept b = {:.2e}".
format( A, B, C, m, b ) )
A2, B2, C2 = getABC( "Enter A2 B2 C2: " )
try:
m2, b2 = slopeIntercept( A2, B2, C2 )
except:
print( "\nBad data entry ... Try again, 3 numbers, spaces between.\n" )
continue
print( "For A2 = {}, B2 = {}, C2 = {}, slope m2 = {:.2e}, intercept b2 = {:.2e}".
format( A2, B2, C2, m2, b2 ) )
if m != m2:
x, y = intersection( m, b, m2, b2 )
print( "Lines intersect at Point ({:.2e},{:.2e})".format(x, y) )
else:
print( "\nParallel lines will NEVER intersect ...\n" )
if input( "More (y/n) ? " ).lower() == "n" :
more = False
# scientificNotation_transitTime.py # # 2016-03-21 #
'''
Question:
Find transit time of light from sun to earth?
https://en.wikipedia.org/wiki/Astronomical_unit
Distance (d) earth to sun is 93,000,000 miles = 9.3 * 10**7 miles
Speed (v) of light is 300,000 km/sec = 3.00 * 10**5 km/sec
'''
d = 1.50 * 10**8 # km
print( "distance (km) : {:.2e}".format( d ) )
v = 3.00 * 10**5 # km/sec
print( "speed (km/sec) : {:.2e}".format( v ) )
t = d/v # seconds
print( "time (second) : {:.2e}".format( t ) )
t = t/60 # minutes
print( "time (minute) : {:.2e}".format( t ) )
print()
'''
distance (km) : 1.50e+08
speed (km/sec) : 3.00e+05
time (second) : 5.00e+02
time (minute) : 8.33e+00
'''
'''
Distance (d) earth to sun is 93,000,000 miles = 9.3 * 10**7 miles
Speed (v) of light is 186,000 miles/sec = 1.86 * 10**5 miles/sec
'''
d = 9.3 * 10**7 # miles
print( "distance (mile) : {:.2e}".format( d ) )
v = 1.86 * 10**5 # mph
print( "speed (mps) : {:.2e}".format( v ) )
t = d/v # seconds
print( "time (second) : {:.2e}".format( t ) )
t = t/60 # minutes
print( "time (minute) : {:.2e}".format( t ) )
input( "\nPress 'Enter' to continue/exit ... " )
'''
distance (mile) : 9.30e+07
speed (mps) : 1.86e+05
time (second) : 5.00e+02
time (minute) : 8.33e+00
Press 'Enter' to continue/exit ...
'''
# scientificNotation_numberRBCs.py # # 2016-03-22 #
'''
Quetion was ... find estimate of the total rbc count for an adult male?
Here we assume, the total blood volume is 5 liters ...
and that 1/2 of this is taken up by the red cells, (i.e., the Hct = 50%)
and the measured (average or mean) cell volume was 90 fL
Note that 1 fL = 10**(-15) L
Volume blood (liter) Vblood = 5
Volume rbcs (liter) Vcells = 5/2
Volume 1 rbc (liter) Vrbc = 90 * 10**(-15)
'''
Vcells = 5/2
print( "Volume of packed cells (liter) : {:.2e}".format( Vcells ) )
Vrbc = 90 * 10**(-15)
print( "Volume of single rbc (liter) : {:.2e}".format( Vrbc ) )
n = Vcells / Vrbc
print( "number of rbcs : {:.2e}".format( n ) )
input( "\nPress 'Enter' to continue/exit ... " )
'''
Volume of packed cells (liter) : 2.50e+00
Volume of single rbc (liter) : 9.00e-14
number of rbcs : 2.78e+13
Press 'Enter' to continue/exit ...
'''
# scientificNotation_numberSeeds.py # # 2016-03-21 #
from math import pi
'''
Question?
Using data at ...
http://agron-www.agron.iastate.edu/Courses/Agron212/Calculations/Seed_acre.htm
find the average volume occupied in an 100 bag, by a single grain of wheat ?
(Use very ROUGH estimates for bag radius and height - bag treated as a cylinder)
Volume (Vb) 100 lb bag r = 8 cm, h = 30 cm ... Vb = pi * r**2 * h
12500 seeds / lb (given at above link)
'''
n = 100 * 12500 # 1,250,000 seeds in 100 lb bag #
print( "number of seeds : {:.2e}".format( n ) )
r = 8 # in cm #
print( "bag radius (cm) : {:.2e}".format( r ) )
h = 30 # in cm #
print( "bag height (cm) : {:.2e}".format( h ) )
Vb = pi * r**2 * h
print( "bag Vb (cm^3) : {:.2e}".format( Vb ) )
Vb = Vb * 1000
print( "bag Vb (mm^3) : {:.2e}".format( Vb ) )
Vs = Vb/n # mm^3
print( "seed Vs (mm^3) : {:.2e}".format( Vs ) )
wps = 100/n # lbs per seed
print( "Weight/seed (lb) : {:.2e}".format( wps ) )
print( "X-check seeds/lb : {:.2e}".format( 1/wps ) )
wps = wps * 1000 # lbs per 1000 seeds
print( "Per k seeds (lb) : {:.2e}".format( wps ) )
wps = ( wps / 2.2 ) * 1000
print( "Per k seeds (gm) : {:.2e}".format( wps ) )
print( "\nRecall 2.2 lb is 1.0 kg" ) # 0.453592 kg = 1 lb #
print( "Better is 1 lb : {:.5e} gm".format(453.592) )
print( "and 1 kg : {:.5e} lb".format(2.20462) )
input( "\nPress 'Enter' to continue/exit ... " )
'''
number of seeds : 1.25e+06
bag radius (cm) : 8.00e+00
bag height (cm) : 3.00e+01
bag Vb (cm^3) : 6.03e+03
bag Vb (mm^3) : 6.03e+06
seed Vs (mm^3) : 4.83e+00
Weight/seed (lb) : 8.00e-05
X-check seeds/lb : 1.25e+04
Per k seeds (lb) : 8.00e-02
Per k seeds (gm) : 3.64e+01
Recall 2.2 lb is 1.0 kg
Better is 1 lb : 4.53592e+02 gm
and 1 kg : 2.20462e+00 lb
Press 'Enter' to continue/exit ....
'''
# scientificNotation_numberSeeds2.py # # 2016-03-21 #
'''
Questions ?
1) Find number of seeds harvested from 10 acres.
2) Find weight of all this new wheat in lbs
Using data at ...
http://agron-www.agron.iastate.edu/Courses/Agron212/Calculations/Seed_acre.htm
From the link above, it was shown that 653.4 lbs will seed 10 acres
i.e. 6.53 * 10**2 (rounded to 3 significant figures, and in scientific notation)
12500 seeds / lb (given at above link)
'''
print( "For 10 acres seeded with wheat ..." )
w = 6.53 * 10**2 # num lbs seed to seed 10 acres
print( "seed weight (lb) : {:.2e}".format( w ) )
n = 6.53 * 10**2 * 12500 # told that 12500 seeds / lb #
print( "number of seeds : {:.2e}".format( n ) )
f = 80 # 100 * .80 = 80 fold gain over seeds planted #
print( "net fold gain (multiplier) : {:.2e}".format( f ) )
h = n * f # harvested number of seed #
print( "number seeds harvested : {:.2e}".format( h ) )
wh_old = h / 12500 # lbs if same weight per seed as the weight per seed planted
print( "weight harvested (old-lbs) : {:.2e}".format( wh_old ) )
wh_new = wh_old * 1.2 # (lbs) Note: the new seeds weigh 20% more on the average than the old seeds planted #
print( "weight harvested (new-lbs) : {:.2e}".format( wh_new ) )
wh_check = w * 80 * 1.2
print( "weight gain check (new-lbs) : {:.2e}".format( wh_check ) )
input( "\nPress 'Enter' to continue/exit ... " )
'''
For 10 acres seeded with wheat ...
seed weight (lb) : 6.53e+02
number of seeds : 8.16e+06
net fold gain (multiplier) : 8.00e+01
number seeds harvested : 6.53e+08
weight harvested (old-lbs) : 5.22e+04
weight harvested (new-lbs) : 6.27e+04
weight gain check (new-lbs) : 6.27e+04
Press 'Enter' to continue/exit ..
'''
w.r.t. form ...
y = a*x*x + b*x + c
we are told that y = 2 if x = 0, thus we find that c = 2
Also the point (12, 0) being on the parabola, implies ...
0 = a*144 + b* 12 + 2
thus ... a = (-12*b - 2) / 144
a = -(6*b + 1)/72
And we know that max value of 10 occurs at x = -b/(2*a)
so we have: 10 = a * (-b/(2*a)) * (-b/(2*a)) + b * (-b/(2*a)) + 2
thus 8 = a* (b*b/(4*a*a) - b*b/(2*a)
= b*b/(4*a) - b*b/(2*a)
= -b*b/(4*a)
and 32*a = -b*b
so a = -b*b/32
and now, substituting this back into, a = -(6*b + 1)/72 , this means that:
-b*b/32 = -(6*b + 1)/72
multiply through by -8 ...
b*b/4 = (6*b + 1) / 9
9*b*b / 36 = 4*(6*b + 1) / 36
9*b*b - 24*b - 4 = 0
# problem_iv.py # # 2016-04-21 #
INTRO = '''SOLUTION to (iv - ADVANCED question);
w.r.t. quadratic equation with form:
y = a*x*x + b*x + c
we are told that y = 2 if x = 0, thus we can easily find that c = 2
Also the point (12, 0) being on the parabola, implies ...
0 = a*144 + b*12 + 2
thus: -144*a = 12*b + 2
a = (12*b + 2) / (-144)
a = -(6*b + 1)/72
And we know that the max value of 10 occurs at x = -b/(2*a)
so we have: 10 = a*(-b/(2*a))**2 + b*(-b/(2*a)) + 2
thus: 8 = a*(b*b/(4*a*a) - b*b/(2*a)
= b*b/(4*a) - b*b/(2*a)
= -b*b/(4*a)
And thus: 32*a = -b*b
so a = -b*b/32 (a is this ...
whatever b is,
so now we find b,
then sub back to find a)
Subbing this back into:
a = -(6*b + 1)/72
means that:
-b*b/32 = -(6*b + 1)/72
multiply both sides by -8 yields:
b*b/4 = (6*b + 1)/9
multiply both sides by 36 yields:
9*b*b = 4*(6*b + 1)
And thus:
9*b*b - 24*b - 4 = 0
So we get this above relation to solve for 'b' to find
suitable values for b that work there.
'''
print( INTRO )
c = 2
# 9*b*b - 24*b - 4 = 0 #
def roots( a, b, c ): # here, we are using the general formula to find roots of any quadratic, given a,b,c #
return (-b - (b*b-4*a*c)**0.5) / (2*a), (-b + (b*b-4*a*c)**0.5) / (2*a)
def get_a( b ):
return -b*b / 32
def f( a, b, c, x ):
return (a*x + b)*x + c
b1, b2 = roots( 9, -24, -4 ) # find both values for b #
print( "b1 =", b1, "b2 =", b2 )
a1 = get_a( b1 )
a2 = get_a( b2 )
print( "a1 =", a1, "a2 =", a2 )
#X-checking ...
y1 = f( a1, b1, c, 0 ) # 2 #
y2 = f( a1, b1, c, 12 ) # 0 #
if abs(y2) < 1e-12: y2 = 0.0 # if VERY NEAR ZERO, show as ZERO #
print( "y1 =", y1, "y2 =", y2 )
y1 = f( a2, b2, c, 0 ) # 2 #
y2 = f( a2, b2, c, 12 ) # 0 #
if abs(y2) < 1e-12: y2 = 0.0
print( "y1 =", y1, "y2 =", y2 )
print( "\nX-checking extreme values ..." )
print( "{:.5f}*({:.2f})**2 + ({:.2f})*({:.4f}) + {} = {:.2f}"
.format(a1, -b1/(2*a1), b1, -b1/(2*a1), c, f(a1,b1,c,-b1/(2*a1)) ) )
print( "{:.5f}*({:.2f})**2 + ({:.2f})*({:.4f}) + {} = {:.2f}"
.format(a2, -b2/(2*a2), b1, -b2/(2*a2), c, f(a2,b2,c,-b2/(2*a2)) ) )
input( "\nPress 'Enter' to continue/exit ... " )
# OUTPUT from running program ... #
'''
SOLUTION to (iv - ADVANCED question);
w.r.t. quadratic equation with form:
y = a*x*x + b*x + c
we are told that y = 2 if x = 0, thus we can easily find that c = 2
Also the point (12, 0) being on the parabola, implies ...
0 = a*144 + b*12 + 2
thus: -144*a = 12*b + 2
a = (12*b + 2) / (-144)
a = -(6*b + 1)/72
And we know that the max value of 10 occurs at x = -b/(2*a)
so we have: 10 = a*(-b/(2*a))**2 + b*(-b/(2*a)) + 2
thus: 8 = a*(b*b/(4*a*a) - b*b/(2*a)
= b*b/(4*a) - b*b/(2*a)
= -b*b/(4*a)
And thus: 32*a = -b*b
so a = -b*b/32 (a is this ...
whatever b is,
so now we find b,
then sub back to find a)
Subbing this back into:
a = -(6*b + 1)/72
means that:
-b*b/32 = -(6*b + 1)/72
multiply both sides by -8 yields:
b*b/4 = (6*b + 1)/9
multiply both sides by 36 yields:
9*b*b = 4*(6*b + 1)
And thus:
9*b*b - 24*b - 4 = 0
So we get this above relation to solve for 'b' to find
suitable values for b that work there.
b1 = -0.15737865166652654 b2 = 2.824045318333193
a1 = -0.0007740012500116843 a2 = -0.2492259987499883
y1 = 2.0 y2 = 0.0
y1 = 2.0 y2 = 0.0
X-checking extreme values ...
-0.00077*(-101.67)**2 + (-0.16)*(-101.6656) + 2 = 10.00
-0.24923*(5.67)**2 + (-0.16)*(5.6656) + 2 = 10.00
Press 'Enter' to continue/exit ...
'''
If y = 0
then 0 = a*x**2 + b*x + c
and 0 = x**2 + (b/a)*x + (c/a) ... <divide all by a != 0>
and = (x + (b/(2*a)))**2 - b**2/(4*a**2) + (c/a) ... <complete the square>
Thus (x + (b/(2*a)))**2 = b**2/(4*a**2) - (c/a)
= (b**2 - 4*a*c)/(4*a**2)
Then x + (b/(2*a)) = +- sqrt(b**2 - 4*a*c)/(2*a) ... <sqrt both sides>
So x = (-b +- sqrt(b**2 - 4*a*c)) / (2*a)
i.e. r1 = (-b - sqrt(b**2 - 4*a*c)) / (2*a)
and r2 = (-b + sqrt(b**2 - 4*a*c)) / (2*a)
if b**2 - 4*a*c = 0, (i.e. if b**2 = 4*a*c),
then r = -b/(2*a)
Note: b**2 - 4*a*c is called the 'discriminant' (D)
If D < 0 i.e. D is a negative value ...
then there are NO REAL roots. (Look up 'imaginary' numbers'.)
y = a*x**2 + b*x + c
= a*(x**2 + (b/a)*x) + c
= a*(x + (b/(2*a)))**2 - b**2/(4*a) + c ... Note: a/(4*a**2) = 1/(4*a)
So re. 'Vertex Form' : y = a*(x - h)**2 + k
h = -b/(2*a)
k = c - b**2/(4*a) = (4*a*c - b**2)/(4*a)
thus h = (r1+r2)/2
and k = a*((r1+r2)/2 - r1)*((r1+r2)/2 - r2)
= a*(-r1+r2)/2)*((r1-r2)/2)
= a*(-r1**2 + 2*r1*r2 -r2**2) / 4
= -a*(r1 - r2)**2 / 4
we have :
y = a*(x - r1)*(x - r2)
= a*x**2 - a*(r1+r2)*x + a*r1*r2
and thus :
b = -a*(r1+r2) ... i.e. r1+r2 = -b/a
c = a*r1*r2 ... i.e. r1*r2 = c/a
# will_first_program_revised.py # # 2016-03-24 and updated 2016-04-01 #
# edited/fixed version, value of 'a' here fixed to be negative #
'''
You are given a TABLE of VALUES (and asked to find dif's ...)
( x, : f(x)) : 1st dif, : 2nd dif. : 3rd dif.
=============================================================
( 0, : 1 )
: 5
( 1, : 6 ) : 2
: 3 : 0
( 2, : 9 ) : 2
: 1 : 0
( 3, : 10 ) : 2
: -1 : 0
( 4, : 9 ) : 2
: -3 : 0
( 5, : 6 ) : 2
: -5
( 6, : 1 )
Now, since the 2nd dif's are 'constant', this is a 'quadratic relationship'.
(Recall, if 1st dif's were 'constant', it would be a 'linear relationship').
Thus ...
with respect to 'VERTEX' form for quadratic: y = f(x) = a*(x - h)*(x - h) + k
it looks like h = 3, k = 10 and value of a is negative.
So using/(subbing in) the 1st given point, (0, 1), that is on the curve,
into: y = a*(x - 3)*(x - 3) + 10,
we have ...
1 = a*9 + 10,
thus: 9*a = -9
and: a = -1
THUS y = f(x) = -(x - 3)*(x - 3) + 10
and that means we also have ...
y = f(x) = -x*x + 6*x + 1
THUS for the form: y = f(x) = a*x*x + b*x + c
we have ...
a = -1, b = 6, c = 1
We will confirm these values for a, b and c in the (calculated) TABLE below:
'''
a = -1 # fixed, the *value* of 'a' is *negative one* #
b = 6
c = 1
h = 3
k = 10
def f( a, b, c, x ):
return a*x*x + b*x + c # <--fixed version, (you had: -a*x*x + b*x + c) #
def fVertex( a, h, k, x ):
return a*(x - h)*(x - h) + k
def fRoots( a, r1, r2, x ):
return a*(x - r1)*(x - r2)
# using formula for roots of a quadratic #
def roots( a, b, c ):
r1 = (-b + (b*b - 4*a*c)**0.5) / (2*a)
r2 = (-b - (b*b - 4*a*c)**0.5) / (2*a)
return r1, r2
r1, r2 = roots( a, b, c )
print( 'Table of x and f(x) and fVertex(x) and fRoots(x) ...' )
print( '( x,' )
print( ' '*10, 'f(x) = {}*x*x + {}*x + {}),'.format( a, b, c ) )
print( ' '*20, 'fVertex(x) = {}*(x - {})*(x - {}) + {},'.format( a, h, h, k ) )
print( ' '*35, 'fRoots(x) = {}*(x - {:.3f})*(x - {:.3f}) )'
.format( a, r1, r2 ) )
for x in range( -1, 8 ): # expanded range from x = -1 to and including x = 7 #
print( '({:2}, {:14}, {:14}, {:14.3f} )'
.format(x, f(a, b, c, x), fVertex(a, h, k, x), fRoots(a, r1, r2, x)) )
print( "r1, r2 = {:.5f}, {:.5f}".format( r1, r2 ), end = ' and ' )
#print( "X-checking ...", end = ' ' )
y1 = f(a, b, c, r1)
if abs(y1) < 1e-12: # 1e-12 means 1 * 10**(-12) #
y1 = 0 # handle 'zero' values to be really set to zero #
y2 = f(a, b, c, r2)
if abs(y2) < 1e-12:
y2 = 0 # handle 'zero' values to be really set to zero #
print( "f(r1), f(r2) = {:.11f}, {:.11f}". format(y1, y2) )
print( "Table of powers of 2 ..." )
for x in range( 2, 24, 3 ):
print( "2**{:2} = {:11}".format(x-2, 2**(x-2)), ' '*7,
"2**{:2} = {:11}".format(x-1, 2**(x-1)), ' '*7,
"2**{:2} = {:11}".format(x, 2**x) )
# print()
name = input( 'Enter your name: ' )
name = name[0:1].upper() + name[1:] # set first letter to *upper* case #
age = input( 'Enter your age (years): ' )
def ending( age ):
if age not in [11, 12, 13, 111, 112, 113]:
if age % 10 == 1: return 'st' # Note that % is for modular division #
if age % 10 == 2: return 'nd'
if age % 10 == 3: return 'rd'
# if reach here ...
return 'th'
# See IF the characters entered for age represent a valid integer #
try:
age = int(age)
if age >= 0:
print( '{}, you really are past {} years old and '
'are now in your {}{} year.'.
format( name, age, age+1, ending(age+1) ) )
else:
print( '{}, {} really?'.format( name, age ) )
except:
print( "(You did not enter a valid number for your years.)" )
print( '{}, are you really {} years old?'.format( name, age) )
input ( "Press 'Enter' to continue/exit ... " )
# Below is copy/paste of output for a run of this program: #
'''
Table of x and f(x) and fVertex(x) and fRoots(x) ...
( x,
f(x) = -1*x*x + 6*x + 1),
fVertex(x) = -1*(x - 3)*(x - 3) + 10,
fRoots(x) = -1*(x - -0.162)*(x - 6.162) )
(-1, -6, -6, -6.000 )
( 0, 1, 1, 1.000 )
( 1, 6, 6, 6.000 )
( 2, 9, 9, 9.000 )
( 3, 10, 10, 10.000 )
( 4, 9, 9, 9.000 )
( 5, 6, 6, 6.000 )
( 6, 1, 1, 1.000 )
( 7, -6, -6, -6.000 )
r1, r2 = -0.16228, 6.16228 and f(r1), f(r2) = 0.00000000000, 0.00000000000
Table of powers of 2 ...
2** 0 = 1 2** 1 = 2 2** 2 = 4
2** 3 = 8 2** 4 = 16 2** 5 = 32
2** 6 = 64 2** 7 = 128 2** 8 = 256
2** 9 = 512 2**10 = 1024 2**11 = 2048
2**12 = 4096 2**13 = 8192 2**14 = 16384
2**15 = 32768 2**16 = 65536 2**17 = 131072
2**18 = 262144 2**19 = 524288 2**20 = 1048576
2**21 = 2097152 2**22 = 4194304 2**23 = 8388608
Enter your name: will
Enter your age (years): 15
Will, you really are past 15 years old and are now in your 16th year.
Press 'Enter' to continue/exit ...
'''
# ballTrajectory.py # # 2016-04-21 #
INTRO = '''INTRODUCTION to solving QUADRATIC EQUATIONS and PARABOLAS ...
What does it actually mean ...
if we have *constant* acceleration ...
for constant acceleration, a,
in units of feet per cecond per second,
starting with initial velocity, vi = 0,
in units of feet per second,
if the constant acceleration, a = 32 feet per second per second?
This means that ...
at 1 second point, the velocity reached is 32 feet per second
st 2 second point, the velocity reached is 64 feet per second
st 3 second point, the velocity reached is 96 feet per second
st 4 second point, the velocity reached is 128 feet per second
etc...
So what is the distance traveled in each second?
We can easily find the average velocity over each second,
to see the total change in distance *for that second* ...
Then we can add up all the changes in distance over all the seconds.
Over the 1st second, the average velocity was (0+32)/2 feet per second.
So distance travelled then was: 16 feet
Over the 2nd second, the average velocity was (32+64)/2 feet per second.
So distance travelled then was: 16+32 feet = 48 feet
Over the 3rd second, the average velocity was (64+96)/2 feet per second.
So distance travelled then was: 32+48 feet = 80 feet
Over the 4th second, the average velocity was (96+128)/2 feet per second.
So distance travelled then was:, 48+64 feet = 112 feet
Thus at time zero, at (0, 0)
and at time 1, at (1, 16)
and at time 2, at (2, 64)
and at time 3, at (3, 144)
and at time 4, at (4, 256)
etc...
Can you see a pattern ?
Thus at time n, at (n, 16*n**2)
But notice that 16 is exactly 1/2 of 32,
(the constanct acceleration here) ...
Thus, we can see, here, that for constant acceleration, a, the distance, d, is:
d = (1/2)*a*t**2
but if we had an initial velocity, vi,
in the same direction as the constant acceleration, a,
the distance, d, then becomes:
d = vi*t + (1/2)*a*t**2
and if we started at initial distance, di,
then the distance. d = f(t), becomes:
d = di + vi*t + (1/2)*a*t*t
NOTE ALSO:
d(1) = (a*0 + a*1)/2
= a/2
= a/2 * (1)**2
d(2) = (a*0 + a*1)/2 + (a*1 + a*2)/2
= a/2 + a/2 + a
= 2*a
= a/2 * (2)**2
d(3) = (a*0 + a*1)/2 + (a*1 + a*2)/2 + (a*2 + a*3)/2
= a/2 + a/2 + a + a + 3*a/2
= 4.5*a
= a/2 * (3)**2
d(4) = (a*0 + a*1)/2 + (a*1 + a*2)/2 + (a*2 + a*3)/2 + (a*3 + a*4)/2
= a/2 + a/2 + a + a + 3*a/2 + 3*a/2 + 2*a
= 8*a
= a/2 * (4)**2
d(5) = (a*0 + a*1)/2 + (a*1 + a*2)/2 + (a*2 + a*3)/2 + (a*3 + a*4)/2 + (a*4 + a*5)/2
= 8*a + 4.5*a
= 12.5*a
= a/2 * (5)**2
Demo of 'proof by induction' ...
We assume: d(n) = a/2 * (n)**2
Then: d(n+1) = a/2 * (n+1)**2
= a/2 * (n**2 + 2*n + 1)
= a/2 * (n)**2 + a/2 *(2*n + 1)
= d(n) + a*(n + n+1)/2
And that fulfills the pattern ...
So ... d(n) = a/2 * n**2
AND d(t) = a/2 * t**2 = (1/2) * a * t**2
'''
print( INTRO, '\n' )
def getDiffs( lst ):
diffs = []
for i in range(1, len(lst)):
diffs.append(lst[i] - lst[i-1])
return diffs
# NEW example(s) ... ball is propelled up, at 8 feet above the ground
# with initial up velocity at 40 feet per second,
# and initial side-ways velocity component of 100 feet per second
# let distance up, be a function of time, d(t), then d(t) = ?
def d(t):
return 8 + 16*t*(2.5 - t) # i.e. d(t) = 8 + 40*t - 16*t*t #
# compare with NO initial velocity at all ...
# i.e. ball is dropped from 8 feet #
def d2( t ):
return 8 - 16*t*t # i.e. d2(t) = 8 - 16*t*t #
# compare with (positive) initial velocity at 40 feet per second DOWN,
# and ball is descending from 0 (as 8) feet
# but t here is starting from t = 0 (2.5) seconds
# so solving 8 = 16*t*(2.5 + t) ... and plotting ... (to find 'zeros')
def f( t ):
return -8 + 16*t*(2.5 + t) # i.e. f(t) = -8 + 40*t - 16*t*t #
print( "So we have 3 quadratic relations demo'd here ..." )
print( "d(t) = 8 + 16*t*(2.5 - t) " )
print( "d2(t) = 8 - 16*t*t " )
print( "f(t) = -8 + 16*t*(2.5 + t) " )
# get a Lst of pairs (t, d(t), d2( t), f(t)(t)) #
print( "\n\nNow let's get and print Python lists of (x, f(x)) for x in range -3..5" )
print( "Note that at t = 0, ball is at 8 feet up,\n"
"\n1st distance column in has vi(up component) = 40 feet per second,\n"
"i.e. (up) d(t) = 8 + 16*t*(2.5 - t) \n"
"\n2nd distance column in has vi = 0 feet per second,\n"
"i.e. (up) d2(t) = 8 - 16*t*t \n"
"\n3rd distance column in has vi(down component) = 40 feet per second,\n"
"and now find time it takes to go down 8 more feet to the ground,\n"
"i.e. for (down) d3(t) = 16*t*(2.5 + t), solve for d3(t) = 8 \n"
"i.e. solve f(t) = 0 for f(t) = -8 + 16*t*(2.5 + t) \n" )
print( "-"*24 )
print( "{:>3s} {:>6s} {:>6s} {:>6s}".format('t', 'd', 'd2', 'f(t)'),
"\n" + "-"*24 )
Lst3 = []
Lst2 = []
Lst = []
for t in range(-3, 6):
Lst3.append( f(t) )
Lst2.append( d2(t) )
Lst.append( d(t) )
print( '{:3} {:6} {:6} {:6}'.format(t, d(t), d2(t), f(t)) )
print( "\nFirst differences ... with ..." )
firstDiffsLst3 = getDiffs(Lst3)
firstDiffsLst2 = getDiffs(Lst2)
firstDiffsLst = getDiffs(Lst)
print("vi = 40, differences are:", firstDiffsLst)
print("vi = 0, differences are:", firstDiffsLst2)
print("vi = -40, differences are:", firstDiffsLst3)
print( "\nSecond differences ... with ..." )
secondDiffsLst3 = getDiffs(firstDiffsLst3)
secondDiffsLst2 = getDiffs(firstDiffsLst2)
secondDiffsLst = getDiffs(firstDiffsLst)
print("vi = 40, differences are:", secondDiffsLst)
print("vi = 0, differences are:", secondDiffsLst2)
print("vi = -40, differences are:", secondDiffsLst3)
print( "\nRecall extreme value for d(t) is reached at 't = -b/(2*a) " )
t_at_max = (-40)/(-2*16)
print( "For d(t), t_at_max =", t_at_max, "seconds, and d(t_at_max) =",
d(t_at_max), "feet.\n" )
def roots( a, b, c ):
return (-b - (b**2 - 4*a*c)**0.5)/(2*a), (-b + (b**2 - 4*a*c)**0.5)/(2*a)
# d(t) = 8 + 16*t*(2.5 - t) #
r1_1, r1_2 = roots( -16, 40, 8 )
print( "\nNow finding roots r1 and r2 for each ..." )
print( "for d(t), r1_1 = {}, r1_2 = {}".format(r1_1, r1_2) )
# d2(t) = 8 - 16*t*t #
r2_1, r2_2 = roots( -16, 0, 8 )
print( "for d2(t), r2_1 = {}, r2_2 = {}".format(r2_1, r2_2) )
# 8 = 16*t*(2.5 + t) implies f(t) = -8 + 16*t*(2.5 + t) #
r3_1, r3_2 = roots( 16, 40, -8 )
print( "for f(t) , r3_1 = {}, r3_2 = {}".format(r3_1, r3_2) )
print( '\nNote that {} + {} = {}'.format(2.5, r3_2, r1_1) )
print( "\nSo, if the ball also had a sideways constant velocity "
"of {} feet/second, \n"
"it would hit the ground at {} ft/sec * {:.4f} sec = {:.2f} feet away."
.format(100, 100, r1_1, 100*r1_1 ) )
input("\nPress 'Enter' to continue/exit ... " )
# output of running program #
'''
INTRODUCTION to solving QUADRATIC EQUATIONS and PARABOLAS ...
What does it actually mean ...
if we have *constant* acceleration ...
for constant acceleration, a,
in units of feet per cecond per second,
starting with initial velocity, vi = 0,
in units of feet per second,
if the constant acceleration, a = 32 feet per second per second?
This means that ...
at 1 second point, the velocity reached is 32 feet per second
st 2 second point, the velocity reached is 64 feet per second
st 3 second point, the velocity reached is 96 feet per second
st 4 second point, the velocity reached is 128 feet per second
etc...
So what is the distance traveled in each second?
We can easily find the average velocity over each second,
to see the total change in distance *for that second* ...
Then we can add up all the changes in distance over all the seconds.
Over the 1st second, the average velocity was (0+32)/2 feet per second.
So distance travelled then was: 16 feet
Over the 2nd second, the average velocity was (32+64)/2 feet per second.
So distance travelled then was: 16+32 feet = 48 feet
Over the 3rd second, the average velocity was (64+96)/2 feet per second.
So distance travelled then was: 32+48 feet = 80 feet
Over the 4th second, the average velocity was (96+128)/2 feet per second.
So distance travelled then was:, 48+64 feet = 112 feet
Thus at time zero, at (0, 0)
and at time 1, at (1, 16)
and at time 2, at (2, 64)
and at time 3, at (3, 144)
and at time 4, at (4, 256)
etc...
Can you see a pattern ?
Thus at time n, at (n, 16*n**2)
But notice that 16 is exactly 1/2 of 32,
(the constanct acceleration here) ...
Thus, we can see, here, that for constant acceleration, a, the distance, d, is:
d = (1/2)*a*t**2
but if we had an initial velocity, vi,
in the same direction as the constant acceleration, a,
the distance, d, then becomes:
d = vi*t + (1/2)*a*t**2
and if we started at initial distance, di,
then the distance. d = f(t), becomes:
d = di + vi*t + (1/2)*a*t*t
NOTE ALSO:
d(1) = (a*0 + a*1)/2
= a/2
= a/2 * (1)**2
d(2) = (a*0 + a*1)/2 + (a*1 + a*2)/2
= a/2 + a/2 + a
= 2*a
= a/2 * (2)**2
d(3) = (a*0 + a*1)/2 + (a*1 + a*2)/2 + (a*2 + a*3)/2
= a/2 + a/2 + a + a + 3*a/2
= 4.5*a
= a/2 * (3)**2
d(4) = (a*0 + a*1)/2 + (a*1 + a*2)/2 + (a*2 + a*3)/2 + (a*3 + a*4)/2
= a/2 + a/2 + a + a + 3*a/2 + 3*a/2 + 2*a
= 8*a
= a/2 * (4)**2
d(5) = (a*0 + a*1)/2 + (a*1 + a*2)/2 + (a*2 + a*3)/2 + (a*3 + a*4)/2 + (a*4 + a*5)/2
= 8*a + 4.5*a
= 12.5*a
= a/2 * (5)**2
Demo of 'proof by induction' ...
We assume: d(n) = a/2 * (n)**2
Then: d(n+1) = a/2 * (n+1)**2
= a/2 * (n**2 + 2*n + 1)
= a/2 * (n)**2 + a/2 *(2*n + 1)
= d(n) + a*(n + n+1)/2
And that fulfills the pattern ...
So ... d(n) = a/2 * n**2
AND d(t) = a/2 * t**2 = (1/2) * a * t**2
So we have 3 quadratic relations demo'd here ...
d(t) = 8 + 16*t*(2.5 - t)
d2(t) = 8 - 16*t*t
f(t) = -8 + 16*t*(2.5 + t)
Now let's get and print Python lists of (x, f(x)) for x in range -3..5
Note that at t = 0, ball is at 8 feet up,
1st distance column in has vi(up component) = 40 feet per second,
i.e. (up) d(t) = 8 + 16*t*(2.5 - t)
2nd distance column in has vi = 0 feet per second,
i.e. (up) d2(t) = 8 - 16*t*t
3rd distance column in has vi(down component) = 40 feet per second,
and now find time it takes to go down 8 more feet to the ground,
i.e. for (down) d3(t) = 16*t*(2.5 + t), solve for d3(t) = 8
i.e. solve f(t) = 0 for f(t) = -8 + 16*t*(2.5 + t)
------------------------
t d d2 f(t)
------------------------
-3 -256.0 -136 16.0
-2 -136.0 -56 -24.0
-1 -48.0 -8 -32.0
0 8.0 8 -8.0
1 32.0 -8 48.0
2 24.0 -56 136.0
3 -16.0 -136 256.0
4 -88.0 -248 408.0
5 -192.0 -392 592.0
First differences ... with ...
vi = 40, differences are: [120.0, 88.0, 56.0, 24.0, -8.0, -40.0, -72.0, -104.0]
vi = 0, differences are: [80, 48, 16, -16, -48, -80, -112, -144]
vi = -40, differences are: [-40.0, -8.0, 24.0, 56.0, 88.0, 120.0, 152.0, 184.0]
Second differences ... with ...
vi = 40, differences are: [-32.0, -32.0, -32.0, -32.0, -32.0, -32.0, -32.0]
vi = 0, differences are: [-32, -32, -32, -32, -32, -32, -32]
vi = -40, differences are: [32.0, 32.0, 32.0, 32.0, 32.0, 32.0, 32.0]
Recall extreme value for d(t) is reached at 't = -b/(2*a)
For d(t), t_at_max = 1.25 seconds, and d(t_at_max) = 33.0 feet.
Now finding roots r1 and r2 for each ...
for d(t), r1_1 = 2.686140661634507, r1_2 = -0.18614066163450715
for d2(t), r2_1 = 0.7071067811865476, r2_2 = -0.7071067811865476
for f(t) , r3_1 = -2.686140661634507, r3_2 = 0.18614066163450715
Note that 2.5 + 0.18614066163450715 = 2.686140661634507
So, if the ball also had a sideways constant velocity of 100 feet/second,
it would hit the ground at 100 ft/sec * 2.6861 sec = 268.61 feet away.
Press 'Enter' to continue/exit ...
'''
# solution_2.py #
INFO_1 = '''Points (0,2) and (12,0) are on the parabola with a max value of 10
find the equation(s) for the parabola(s).
'''
INFO_2 = '''Since 2 = a(0)**2 +b(0) + c,
c = 2
'''
c = 2
def f( a, b, x ):
return (a*x + b)*x + 2
INFO_3 = '''But also (12,0) is on parabola ... so ...
0 = 144*a + 12*b + 2
thus ...
-12*b = 144*a + 2
b = -(12*a + 1/6)
or ...
144*a = -12*b - 2
thus...
a = -(12*b + 2)/144 = -(6*b + 1)/72
'''
def b_from_a( a ):
return -(12*a + 1/6)
def extreme( a, b ): # recall extreme at x = -b/(2*a)
#return a*(-b/(2*a))**2 + b*(-b/(2*a)) + 2
return 2 - b*b/(4*a)
INFO_4 = '''But also max of 10 occurs for ...
10 = 2 - b*b/(4*a)
8 = - b*b/(4*a)
thus ...
b*b = -32*a
and ...
a = -b*b/32
but also, recall: a = -(6*b + 1)/72
so we get ...
-b*b/32 = -(6*b + 1)/72
so ... multiply both sides by -8 gives ...
b*b/4 = (6*b +1)/9
now ... multiply both sides by 36 gives ...
9*b*b - 24*b - 4 = 0
'''
print( INFO_1 )
print( INFO_2 )
print( INFO_3 )
print( INFO_4 )
def a_from_b( b ):
return -b*b/32
def roots( a, b, c ):
return (-b -(b*b-4*a*c)**0.5)/(2*a), (-b +(b*b-4*a*c)**0.5)/(2*a)
b1, b2 = roots(9, -24, -4)
print( "b1 = {:.6f}, b2 = {:.6f}".format(b1, b2) )
a1 = a_from_b(b1)
a2 = a_from_b(b2)
print( "a1 = {:.6f}, a2 = {:.6f}".format(a1, a2) )
print( "Checking ..." )
print( 'y = {:.6f}*x**2 + {:.6f}*x + 2'.format(a1, b1) )
print( 'y = {:.6f}*x**2 + {:.6f}*x + 2'.format(a2, b2) )
print( 0, 2, "=? {:.2f}".format(f(a1,b1,0)) )
print( 12, 0, "=? {:.2f}".format(f(a1,b1,12)) )
print( 0, 2, "=? {:.2f}".format(f(a2,b2,0)) )
print( 12, 0, "=? {:.2f}".format(f(a2,b2,12)) )
print( "extreme is:", extreme(a1,b1), "at x =", "{:.6}".format(-b1/2/a1) )
print( "extreme is:", extreme(a2,b2), "at x =", "{:.6}".format(-b2/2/a2) )
input( "\nPress 'Enter' to continue/exit ... " )
# OUTPUT Oor running program ... #
'''
Points (0,2) and (12,0) are on the parabola with a max value of 10
find the equation(s) for the parabola(s).
Since 2 = a(0)**2 +b(0) + c,
c = 2
But also (12,0) is on parabola ... so ...
0 = 144*a + 12*b + 2
thus ...
-12*b = 144*a + 2
b = -(12*a + 1/6)
or ...
144*a = -12*b - 2
thus...
a = -(12*b + 2)/144 = -(6*b + 1)/72
But also max of 10 occurs for ...
10 = 2 - b*b/(4*a)
8 = - b*b/(4*a)
thus ...
b*b = -32*a
and ...
a = -b*b/32
but also, recall: a = -(6*b + 1)/72
so we get ...
-b*b/32 = -(6*b + 1)/72
so ... multiply both sides by -8 gives ...
b*b/4 = (6*b +1)/9
now ... multiply both sides by 36 gives ...
9*b*b - 24*b - 4 = 0
b1 = -0.157379, b2 = 2.824045
a1 = -0.000774, a2 = -0.249226
Checking ...
y = -0.000774*x**2 + -0.157379*x + 2
y = -0.249226*x**2 + 2.824045*x + 2
0 2 =? 2.00
12 0 =? -0.00
0 2 =? 2.00
12 0 =? 0.00
extreme is: 10.0 at x = -101.666
extreme is: 10.0 at x = 5.66563
Press 'Enter' to continue/exit ...
'''
# differences_demo.py #
for loop in range(1, 5):
print( "For: ", end = '' )
if loop == 1:
def f( t ):
return 8 + 40*t
print( "y = 8 + 40*t" )
elif loop == 2:
def f( t ):
return 8 + 40*t - 32/2*t**2
print( "y = 8 + 40*t - 32/2*t**2" )
elif loop == 3:
def f( t ):
return 8 + 40*t - 32/2*t**2 + 10/2/3*t**3
print( "y = 8 + 40*t - 32/2*t**2 + 10/2/3*t**3" )
elif loop == 4:
def f( t ):
return 8 + 40*t - 32/2*t**2 + 10/2/3*t**3 - 5/2/3/4*t**4
print( "y = 8 + 40*t - 32/2*t**2 + 10/2/3*t**3 - 5/2/3/4*t**4" )
def getDiffs( lst ):
diffs = []
for i in range(1, len(lst)):
diffs.append(lst[i] - lst[i-1])
return diffs
Lst = []
for t in range(-3, 6):
Lst.append( f(t) )
roundedLst = [ eval('{:.1f}'.format(i)) for i in Lst]
print( '\nValues are: ', end = '' )
print( roundedLst )
def show( diffs ):
nLst = [ '{:.1f}'.format( i ) for i in diffs ]
print( '[' + ', '.join(nLst) + ']' )
print( "\n1st differences are: ", end = '' )
firstDiffs = getDiffs(Lst)
show( firstDiffs )
print( "\n2nd differences are: ", end = '' )
secondDiffs = getDiffs(firstDiffs)
show( secondDiffs )
print( "\n3rd differences are: ", end = '' )
thirdDiffs = getDiffs(secondDiffs)
show( thirdDiffs )
print( "\n4th differences are: ", end = '' )
fourthDiffs = getDiffs(thirdDiffs)
show( fourthDiffs )
if loop < 4:
msg = ' on to do loop ' + str(loop+1)
else:
msg = '/exit'
input( "\nPress 'Enter' to continue{} ... ".format( msg ) )
print( '-'*45, '\n' )
print( '-'*45 )
# output from running program ... #
'''
For: y = 8 + 40*t
Values are: [-112.0, -72.0, -32.0, 8.0, 48.0, 88.0, 128.0, 168.0, 208.0]
1st differences are: [40.0, 40.0, 40.0, 40.0, 40.0, 40.0, 40.0, 40.0]
2nd differences are: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
3rd differences are: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
4th differences are: [0.0, 0.0, 0.0, 0.0, 0.0]
Press 'Enter' to continue on to do loop 2 ...
---------------------------------------------
---------------------------------------------
For: y = 8 + 40*t - 32/2*t**2
Values are: [-256.0, -136.0, -48.0, 8.0, 32.0, 24.0, -16.0, -88.0, -192.0]
1st differences are: [120.0, 88.0, 56.0, 24.0, -8.0, -40.0, -72.0, -104.0]
2nd differences are: [-32.0, -32.0, -32.0, -32.0, -32.0, -32.0, -32.0]
3rd differences are: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
4th differences are: [0.0, 0.0, 0.0, 0.0, 0.0]
Press 'Enter' to continue on to do loop 3 ...
---------------------------------------------
---------------------------------------------
For: y = 8 + 40*t - 32/2*t**2 + 10/2/3*t**3
Values are: [-301.0, -149.3, -49.7, 8.0, 33.7, 37.3, 29.0, 18.7, 16.3]
1st differences are: [151.7, 99.7, 57.7, 25.7, 3.7, -8.3, -10.3, -2.3]
2nd differences are: [-52.0, -42.0, -32.0, -22.0, -12.0, -2.0, 8.0]
3rd differences are: [10.0, 10.0, 10.0, 10.0, 10.0, 10.0]
4th differences are: [0.0, -0.0, -0.0, 0.0, 0.0]
Press 'Enter' to continue on to do loop 4 ...
---------------------------------------------
---------------------------------------------
For: y = 8 + 40*t - 32/2*t**2 + 10/2/3*t**3 + 5/2/3/4*t**4
Values are: [-284.1, -146.0, -49.5, 8.0, 33.9, 40.7, 45.9, 72.0, 146.5]
1st differences are: [138.1, 96.5, 57.5, 25.9, 6.8, 5.2, 26.1, 74.5]
2nd differences are: [-41.6, -39.1, -31.6, -19.1, -1.6, 20.9, 48.4]
3rd differences are: [2.5, 7.5, 12.5, 17.5, 22.5, 27.5]
4th differences are: [5.0, 5.0, 5.0, 5.0, 5.0]
Press 'Enter' to continue/exit ...
'''