Author Topic: High School type Math questions solved using Python 3.x ...  (Read 36617 times)

Offline David

  • Hero Member
  • *****
  • Posts: 647
    • View Profile
High School type Math questions solved using Python 3.x ...
« on: January 27, 2016, 04:56:58 AM »


This first question was not solved completely by any? student ...

It was the last question on an Ontario grade 10 term test (in January 2016).

Here is the problem:

A curve is described by the quadratic relation:

f(x) = a*x*x + b*x + c

If f(0) = 4 and f(24) = 0 and the max value ever reached by f(x), for all x, is 14,
then find value(s) of x where f(x) = 10


Note!

The students were expected to be familiar with the following expressions (and concepts):

(1)  f(x) = a*x*x + b*x + c 
      and  that if f(x) = 0, then the two 'roots' are:  ( -b + sqrt(b*b - 4*a*c) ) / (2*a)  AND  ( -b - sqrt(b*b - 4*a*c) ) / (2*a)
      and that (both) the roots are real if (b*b - 4*a*c) > 0, i.e. the graph 'cuts' the x-axis,
      and that there is just one (real) root if (b*b - 4*a*c) = 0, i.e. the graph 'just touches' the x-axis,
      else, both (distinct) roots are 'imaginary', i.e. the graph does NOT touch or cut the x-axis.

(2)  f(x) = a*(x-r1)*(x-r2)

(3) f(x) = a*(x-h)*(x-h) + k

(4) An extreme value for f(x), i.e. a max value if 'a' is negative, a min if 'a' is positive, occurs when x = -b/(2*a) ... wrt to (1) above.


OK ... below is a solution coded in Python 3:


Code: [Select]
# 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 ... ...
"""


Version 2 below, also finds and shows both the other roots (beside the given root of 24) ...
for each of the possible 2 (pairs of) 'a' and 'b' values in the relation f(x) = a*x*x + b*x + 4

Code: [Select]
# 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 ...
"""


To see this graphed ... go to a Google search window ... then copy the following line and paste it there:

graph y = 10,   -0.000582x^2  -0.153x + 4,   -0.08275x^2 + 1.8193x + 4

« Last Edit: September 11, 2018, 02:01:11 AM by David »

Offline David

  • Hero Member
  • *****
  • Posts: 647
    • View Profile
Re: High School type Math questions solved using Python 3.x ...
« Reply #1 on: January 31, 2016, 11:29:31 AM »
Another typical quadratic problem is one like this ...

Given the graph  ...

or the equation:   h = –4.9t^2 + 50t + 1.5

of a quadratic relation representing the height of a ball,
(thrown straight up, beginning from 1.5 meters above the ground),
over elapsed time, (seconds),
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 (from time 0) 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, interval = 10.14


A Python 3 solution is coded below:
(You can copy/paste the following text into a text editor and save this file with file name as:  find_max_and_time.py
 To run the program, 'find_max_and_time.py', i.e. to see the OUTPUT produced,
 click on the file name, after you have installed a version of Python 3 - if you have not done that already.)


Code: [Select]
# 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 ...
"""


Now you can paste a copy of the text below into a Google search window, to see this graphed:

graph of y = –4.9t^2 + 50t + 1.5 ,  y = 50^2/(4*4.9) + 1.5 , y = 3
« Last Edit: February 01, 2016, 04:58:13 AM by David »

Offline David

  • Hero Member
  • *****
  • Posts: 647
    • View Profile
Re: High School type Math questions solved using Python 3.x ...
« Reply #2 on: March 21, 2016, 08:42:47 AM »
Review of equations of a line ...

Firstly the form A*x + B*y + C = 0 and how it converts to the form y = m*x + b

Code: [Select]
# 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



Now ... if you have two lines, (with finite slope), can you solve and find the (one) point of intersection, (if the lines are NOT parallel)?

Code: [Select]
# 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

« Last Edit: March 21, 2016, 09:03:44 AM by David »

Offline David

  • Hero Member
  • *****
  • Posts: 647
    • View Profile
Re: High School type Math questions solved using Python 3.x ...
« Reply #3 on: March 21, 2016, 08:45:23 AM »
Now for, a little more interesting, 3 questions ...

Firstly, here is some reference material to get some needed data to do these questions.

Questions for beginning grade 10 (advanced) review ...


(1) How long after a solar flare erupts, can it be seen from earth?

https://en.wikipedia.org/wiki/Astronomical_unit

The astronomical unit (symbol au), is a unit of length,
roughly the distance from Earth to the Sun.

However, that distance varies as Earth orbits the Sun,
from a maximum (aphelion) to a minimum (perihelion)
and back again once a year.

*Originally conceived as the average of Earth's aphelion and perihelion*,
it is now defined as exactly
149,597,870,700 metres
(about 150 million kilometres, or 93 million miles) ...


https://en.wikipedia.org/wiki/Speed_of_light

The speed of light in vacuum, commonly denoted c,
is a universal physical constant important in many areas of physics.
Its precise value is 299,792,458 metres per second
(approximately 3.00 * 10**8 m/s) ...



(2) Find an estimate of the number of red blood cells in an average adult male?

https://en.wikipedia.org/wiki/Mean_corpuscular_volume

Normally, MCV (Mean 'Cell' Volume) ... is expressed in femtoliters (fL),
(or 10**(-15) L),

and [RBC] is the quantity expressed in millions per microliter
(or 10**6 / µL) ... (or 10**12 / L)

The normal range for MCV is 80–100 fL.

( Note that a µL is 10**(-6) L. i.e. there are 10**6 µL in each L )

Now, take as an example ...
if the (measured) Hct = 42.5% (packed cells/to whole blood) and the ...
machine counted [RBC] = 4.58 million per microliter (4,580,000/µL),
and the (machine measured) MCV (was) = 92.8 * 10**(-15) L or 92.8 fL


If we assume 5.00 L of (whole) blood in this person ...
thus ... 5.00 * 0.425 L (packed cells) = 2.125 L (packed cells)


The total RBC count in all (assumed) 5 L is:

4.58 * 10**6 * 10**6  cells/L * (5 L)
=
4.58 * 10**12 cells/L * (5 L)
=
22.9 * 10**12 cells
=
2.29 * 10**13 cells

So our answer is: there are about 2.29 * 10**13 rbcs in this person,
(i.e. ....if the person had 5 L of (whole) blood.)


Also note ... X-checking ...
we can get a calculated average volume per (red) cell, (i.e a calculated MCV):

((assumed) packed cell_volume) / ((assumed) total number of cells)
(2.125) / (2.29 * 10**13)   (L/cell)
= 0.928 * 10**(-13) (L/cell)
= 92.8 *10**(-15) (L/cell)
= 92.8 fL / cell

This is the average (calculated) volume per each red blood cell (rbc),
(and note, it agrees here, with the machine measured value ... yeh :)


Thus ...
interesting here, to also note, we can get a calculated Hct ...
(from blood counting machine values for MCV and [RBC])

MCV * [RBC]  (L/cell * cells/L)
=
92.8 * 10**(-15)  *  (4.58 * 10**12)
=
425 * 10**(-3)
=
.425
= (same value as Hct measured here ... Yeh!!!)



(3)

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)
« Last Edit: May 06, 2016, 10:03:56 AM by David »

Offline David

  • Hero Member
  • *****
  • Posts: 647
    • View Profile
Re: High School type Math questions solved using Python 3.x ...
« Reply #4 on: March 21, 2016, 08:47:36 AM »
Here is an easy Python program solution for question (1) re. time of transit ...

Code: [Select]
# 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 ...
'''

Offline David

  • Hero Member
  • *****
  • Posts: 647
    • View Profile
Re: High School type Math questions solved using Python 3.x ...
« Reply #5 on: March 21, 2016, 08:50:09 AM »
Now ... and easy Python solution to the problem of ...

(2) Find an estimate of the number of red blood cells in an average adult male?

Code: [Select]
# 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 ...
'''
« Last Edit: March 22, 2016, 01:26:00 PM by David »

Offline David

  • Hero Member
  • *****
  • Posts: 647
    • View Profile
Re: High School type Math questions solved using Python 3.x ...
« Reply #6 on: March 21, 2016, 08:51:04 AM »
And thirdly ... a Python (version 3.x) solution to the problem ...

(3a) Find an estimate to the average volume occupied in an 100 bag, by a single grain of wheat ?

Code: [Select]
# 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 .... 
'''
« Last Edit: March 21, 2016, 02:10:31 PM by David »

Offline David

  • Hero Member
  • *****
  • Posts: 647
    • View Profile
Re: High School type Math questions solved using Python 3.x ...
« Reply #7 on: March 21, 2016, 01:58:30 PM »
And (3b)

(3b.1) Find number of seeds harvested from 10 acres.
(3b.2) Find weight of all this new wheat in lbs

Code: [Select]
# 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 ..
'''
« Last Edit: March 21, 2016, 03:05:18 PM by David »

Offline David

  • Hero Member
  • *****
  • Posts: 647
    • View Profile
Re: High School type Math questions solved using Python 3.x ...
« Reply #8 on: March 24, 2016, 01:10:05 PM »
Beginning quadratics ...

Recall that quadratics have several forms
and 3 independent parameters
to determine the parabola
(just as linear relations have several forms
(and 2 independent parameters to determine the line.)



Review for linear relations ...

Recall that the equation for a line has 3 forms, but really only 2 ... **** why? ***:


(1) slope and y-intercept form, with parameters m, b ... the slope & intercept
y = f(x) = m*x + b


(2) two point form given P1(x1, y1) and P2(x2, y2)
We know that the slope, m = (y2 -y1) / (x2 -x1)
Thus we now have, y = m*x + b = (y2 - y1) / (x2 - x1) * x + b
But both points P1 and P2 are on the line,
so each point MUST satisfy the equation.
Using p2, we have ...
y2 = (y2 - y1) / (x2 - x1) * x2 + b
and thus b = ...
b = y2 - (y2 - y1) / (x2 - x1) * x2
And substituting this in y = m*x + b gives us ...
y = (y2 - y1) / (x2 - x1) * x + y2 - (y2 - y1) / (x2 - x1) * x2
and that is same as ...
y - y2 =
     (y2 - y1) / (x2 - x1) * x  - (y2 - y1) / (x2 - x1) * x2
Note that if  we had used P1 above, we would ALSO have ...
y - y1 =
     (y2 - y1) / (x2 - x1) * x  - (y2 - y1) / (x2 - x1) * x1

... and this is the same as:
  (y - y1) / (x - x1)  = m = (y2 - y1) / (x2 - x1)


(3) A*x + B*y + C = 0 form that reduces to y = m*x + b
If we divide through by B, (if B != 0), we have ...
(A/B)*x + y + C/B = 0
and this gives us ...
y = -(A/B)*x - C/B
Thus slope m is -(A/B) and y-intercept b is - C/B

*** So if you are given 2 distinct points,
    or the slope and one point for the line,
    you can then DETERMINE the unique line so specified.***




Now for a parabola ...

we might suspect that 3 distinct points ...
or the vertex point and one other distinct point ...
(note that the other, not vertex point, also gives us the symetric other 3rd point) ...

or that ...
3 independant parameters ... like (a, b, c) in ...
y = f(x) = a*x*x + b*x +c

that this would fully determine a (vertically orientated) parabolic curve ...
Why?


(1) root form with roots r1, r2 (Note: roots often are called p, q)
y = f(x) = a*(x - r1)*(x - r2)
Note that ...
if x = r1, then factor (r1 - r1) = 0 and y becomes 0
or ...
if x = r2, then (r2 - r2) = 0 and y also becomes 0
So in either case, y, i.e. f(x), then becomes 0

What do changes in value of 'a' do here to the shape of the (parabolic) curve ?

if sign of 'a' is reversed ?

Answer: if sign of value 'a' is reversed, curve has same vertex but is flipped over,
if 'a' is positive, curve 'opens up', if 'a' is negative, curve 'opens down'

if |a| > 1 ?  ... recall that |a| means the absolute value of (value) 'a'
Answer: as |a| grows, the curve rises MORE quickly ...

if |a} < 1 ?
Answer:
curve is 'flattened out' as |a| gets smaller
curve with |a| < 1, compared to curve with |a| > 1, is 'MUCH flatter'.


If we multiply 'a' through ... we get ...

y  = a*x*x - a*(r1 + r2)*x + a*r1*r2
which suggests that we can factor a quadratic back into the form:
y = a*(x - r1)*(x - r2)
which suggests that if a quadratic has a root, say r1, (i.e. if x = r1 makes y = 0),
then it can be factored into the form:
y = a*(x - r1)*(x - r2)

This suggests a 'factor therom' ...
i.e. if f(x) = 0 has the root 'r', (i.e. f(r) evaluates to 0 for x = r),
      then (x - r) is a factor of f(x)


(2) Vertex form
y = f(x) = a*(x - h)*(x - h) + k
Note that here, if x = h, then (h - h) is 0, and then y = k,
and 'k' is the extreme (vertex) 'y' value (either a max or a min depending on ?)

So here, the vertex point is given by (h, k)

Now if we expand this form ... we get ...

y = a*x*x - 2*a*h*x + a*h*h + k
  = a*x*x + (-2*a*h)*x + (a*h*h + k)



(3) 'general' or 'standard' form for quadratic (with graph a parabola)
y = f(x) = a*x*x + b*x + c



Now note that for all 3 forms, each have 3 independant parameters,
and that values for those 3 parameters together will determine an unique parabola.


Comparing coefficients for the 'x' (power 1) terms ...

we can see ... from parabola forms (3) and (2) above ... that ...
-2*a*h = b

and thus ...
h = - b / (2*a)

and recall that this is the value of x where the 'vertex' (the extreme value) is reached.


Also ...
comparing coefficients for the 'x' (power 1) terms ...

we can see ... from parabola forms (1) and (2) above ... that ...
-2*a*h = - a*(r1 + r2)

and thus ...
h = (r1 + r2)/2  ... (which we already knew from symetry considerations) ...

and recall that this is the value of x where the 'vertex' (the extreme value) is reached.



Practice questions:


(i)  if y = 2*x*x + 4*x + 2, describe the graph.
Answer:
We can factor out 2 and thus we have:
y = 2*(x*x + 2*x + 1)
And this is same as y = 2*(x + 1)*(x + 1)
So graph 'opens up' and has root -1 (i.e. '2 equal roots' with both same, i.e. -1)
i.e. the graph just 'touches' the x-axis at (i.e y = 0 ONLY at) x = -1
i.e. the vertex is (-1, 0)


(ii) if y = f(x) = -2*(x - 2)*(x + 1), describe the graph.
Answer:
Graph 'opens down'
Roots are 2 and -1, i.e. f(x) = 0 for these values of x
Expanded (and re-arranged in vertex form) is:
y = -2*(x*x - x - 2)
  = -2*(x*x - x) + 4
  = -2*(x*x - x + 1/4 - 1/4) + 4   ... Note that here we 'complete the square'
  = -2*(x*x - x + 1/4) + 2/4 + 4
  = -2*(x - 1/2)*(x - 1/2) + 9/2

Thus, the vertex point is: (1/2, 9/2)

Another way to do this would be to expand to:
y = -2*(x*x - x - 2)
  = -2*x*x + 2*x + 4

Then recall that for form y = a*x*x + b*x + c,
that x = -b/(2*a) is the 'x' for the vertex ...

THUS, by inspecting the coefficients,
(i.e. using b = 2 and a = -2)
we get that the x at our vertex is: -(b) / (2*a) = -(2) / (2*(-2)) = 1/2
and f(1/2) = -2*(-3/2)*(3/2) = 9/2
So the vertex is: (1/2, 9/2)

BUT ... the SIMPLEST way to do this would be to note (from symetry considerations), 
that the axis of symmetry for this parabola, is a line drawn parallel to the y-axis,
that intersects the x-axis mid-way between the two roots,  x= -1 and x = 2

i.e. mid-point is at x = (2 + (-1))/2 = 1/2

and then to substitute that x = 1/2 value
back into f(x) = -2*(x - 2)*(x + 1) to get ...

f(1/2) = -2(-3/2)(3/2) = 9/2

And thus, the vertex is at point (1/2, 9/2)

q.e.d.



(iii) if y = -3*(x - 2)*(x - 2) + 10, describe the graph.
Answer:
Graph 'opens down' (since sign of x*x co-efficient is negative)
The max value is 10
The vertex point is (2, 10)
and expanding, we get ...
y = -3*(x*x - 4*x + 4) + 10
  = -3*x*x + 12*x - 2
If we solve for y = 0,
then 0 = 3*x*x - 12*x + 2

using formula for roots:  (-b +- sqrt(b*b - 4*a*c)) / (2*a)

So ... roots are: (12 +- sqrt(12*12 - 4*3*2)) / (2*3)
                      = (12 +- sqrt(144 - 24)) / 6
                      = (12 +- sqrt(120)) / 6
                      = 2 +- 1.825
                      = 3.825 and 0.175


BUT this y = 0 = -3*(x - 2)*(x - 2) + 10 could also be more easily solved (find roots/zeros) like this:

-10 = -3*(x - 2)*(x - 2)

10/3  = (x-2)*(x-2)

Thus  x-2 = +- sqrt(10/3) 
and so x  = 2 +- sqrt(10/3) ...

NOW note that sqrt(10/3) is the very same thing as sqrt(120)/6

PROOF: sqrt(120)/6 = sqrt(120/36) = sqrt(20/6) = sqrt(10/3)

q.e.d





ADVANCED question:
(iv) If a parabola goes through the points (0, 2) and (12, 0)
     and reaches a max value of 10,
     can you find an equation (equations) for the graph(s) ?



Next to do ...

what is ... (more on) Completing the square ?
what is ... (more about) general formula to find roots of quadratic ?
what is ... discriminant (D) ... what is meaning if D < 0, D = 0, D > 0 ?

Advanced:
what is slope at point ?
what is slope at extreme point (the vertex) ?






SOLUTION to (iv - ADVANCED question);

Code: [Select]
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


Now use a Python program and root formula to find roots ...


Then X-check results ...


Code: [Select]
# 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 ...
'''
« Last Edit: May 19, 2016, 04:29:44 AM by David »

Offline David

  • Hero Member
  • *****
  • Posts: 647
    • View Profile
Re: High School type Math questions solved using Python 3.x ...
« Reply #9 on: April 02, 2016, 06:45:07 PM »
It's time to develope the formula to find the roots of the standard form of the quadratic ...
(using 'completing the square') ...


Find formula for roots of a quadratic by 'Completing the Square'


Re. 'Standard Form' :  y = a*x**2 + b*x + c

Code: [Select]
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'.)


Also re. 'Standard Form' :
(re-arranging to 'Vertex Form' we have ...)

Code: [Select]
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)


Also re. 'Root Form' :  y = a*(x - r1)*(x - r2)
(to go to 'Vertex Form', we have ...)

we know that h = mid point of r1 and r2,

Code: [Select]
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

       
Or ... to go from 'Root Form' to 'Standard Form' :  y = a*x**2 + b*x + c

Code: [Select]
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 
« Last Edit: April 19, 2016, 08:31:35 AM by David »

Offline David

  • Hero Member
  • *****
  • Posts: 647
    • View Profile
Re: High School type Math questions solved using Python 3.x ...
« Reply #10 on: April 02, 2016, 06:56:02 PM »
A Python 3 example that prints a Table of Values for all three forms of the quadratic ...
(and does a little more ... at the end ... It demos how to code, in Python 3,  to take in some data and show it back.)

1) Standard Form :  y = f(x) = a*x*x + b*x + c

2) Vertex Form     :  y = f(x) = a*(x - h)*(x - h) + k

3) Root Form        :  y = f(x) = a*(x - r1)*(x - r2)


Code: [Select]
# 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 ...
'''

« Last Edit: April 19, 2016, 08:45:52 AM by David »

Offline David

  • Hero Member
  • *****
  • Posts: 647
    • View Profile
Re: High School type Math questions solved using Python 3.x ...
« Reply #11 on: April 19, 2016, 08:49:08 AM »
An other ball trajectory problem ... solved using Python 3 ... that illustrates what is constant acceleration.


Code: [Select]
# 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 ...
'''
« Last Edit: April 21, 2016, 09:09:20 AM by David »

Offline David

  • Hero Member
  • *****
  • Posts: 647
    • View Profile
Re: High School type Math questions solved using Python 3.x ...
« Reply #12 on: April 21, 2016, 09:07:00 AM »
A slightly different path to a solution ...

Code: [Select]
# 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 ...
'''
« Last Edit: April 21, 2016, 09:13:58 AM by David »

Offline David

  • Hero Member
  • *****
  • Posts: 647
    • View Profile
Re: High School type Math questions solved using Python 3.x ...
« Reply #13 on: April 21, 2016, 09:12:32 AM »
More differences ... 1st, 2nd, 3rd and 4th demo'd here ...

Code: [Select]
# 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 ...
'''
« Last Edit: April 21, 2016, 09:15:49 AM by David »

Offline David

  • Hero Member
  • *****
  • Posts: 647
    • View Profile
Re: High School type Math questions solved using Python 3.x ...
« Reply #14 on: May 16, 2016, 03:59:50 PM »
Review of quadratics ...

beginning with this simple form:

y = f(x) = a*x*x + b*x

(i.e. NOTE: here we have NO c term, i.e. c = 0),

If you are then asked to find roots, (zeros),
i.e. values for x that make f(x) equal to zero,
how to start?

Firstly, note that you can factor out .. one 'x' factor ...
in each term of the expression for f(x)


So ... Step 1:

Factoring out 'x' gives ...

f(x) = x*(a*x + b)

And if f(x) is to reach the value of 'zero' ...

what is x then?

1st root is 0

Because:
0*(a*0 + b) is 0

To find the 2nd root,
solve: (a*x + b) = 0

and that is true when x is -b/a

Now what is mid way between x = 0 and x = -b/a ?


Answer: -b/(2*a)

So axis of symmetry for this parabola runs parallel to the y-axis ...
and that line crosses the x-axis at x = -b/(2*a)



If 'a' is a positive value, the parabola 'opens up'

If 'a' is a negative value, then the parabola 'opens down'

If 'a' is equal to zero, we do NOT have a parabola. ( Recall the graph of y = b*x is a straight line through the origin, with slope here equal to 'b' )





Review of some algebra stuff ...

If x, c, b, a are numbers ...

can you expand:

c * (a + b) = ?

c * (a - b) = ?


a * (a + b) = ?

a * (a - b) = ?

(a + b) * (a + b) = ?

(a + b) * (a - b) = ?

(a - b) * (a - b) = ?

3*(x+2) * (x-2) = ?

-2*(x-3)*(x-2) = ?


If case 1: f(x) = 3*(x+2) * (x-2)
or case 2: f(x) = -2*(x-3) * (x-2)

describe the graph of the relations in the (expanded) expressions?

For each list:

roots (i.e. the 'zeros', the values of x that set f(x) to zero)

the vertex point

if the graph opens up or down

if it has a max or min value, and what that value is

sketch the graph using the 3 points 'supplied' by the above 'questions being answered'

Recall that the graph of a parabola is 'symmetric' ...
and that the 'axis of symmetry' for the above two 'quadratic' expressions (parabolas)
is a line parallel to the y axis the intersects the x axis mid-way between their roots.

Recall also that the point midway between any two x values, say x=a and x=b is simply:

(a+b)/2

i.e. the 'mean value'
i.e. the 'average value'
i.e. the 'middle value'



Big hint, substitute the 'mid point of the roots' back into the original expressions
for f(x) to find the value then ...
and that f(x) is the max or min value of that parabola.



Practical exam question (here we overlook the effect of air resistance):

If a ball is thrown upward and leaves the throwers hand with an upward velocity of 32 feet per second,
and gravity acts (downward) constantly at -32 feet per second per second,
what is ...

(a) the max height (above the ground) reached by the ball,
    if it leaves the throwers hand at 8 feet above the ground?

(b) how long will I take to reach that max height in seconds?

(c) how long will it take to get back to the 8 feet height from which it started?

(d) how long will it take from the start time till it hits the (level) ground (in seconds)?

(e) if the ball had a constant positive x direction velocity of
    10 feet per second, and the thrower was standing at x = 0
    when he threw the ball,
    what is the value of x where the ball hits the (level) ground?

    Hint:
    Use the relation for the vertical height (h): h = f(t)
    and note that ...

    f(t) - 8 = 32*t - (1/2)*(32)*t*t

    where h is the height in feet
    and t is the time in seconds

    Note: in the above, we assume 'ideal conditions' of NO air resistance effect on the ball,
    to get an easier first approximation to the  path it takes ...
    i.e. we assume the (vertical part of the) path is a perfect parabola and is described perfectly by:

    h = f(t)

    with,

    f(t) - 8 = 32*t - 16*t*t

    for t taking values 0 to its final time.


    Their is an easy way to solve the 'ball trajectory ' problem  ...
    and also a 'grunt work' way ...

    Using the 'easy way' is more fun and you are less likely to make
    calculation mistakes on the way to the final answer.




Answers to review of algebra stuff:

If c, b, a are numbers ...

Expanded:

c * (a + b) = c*a + c*b = a*c + b*c

c * (a - b) = c*a - c*b = a*c - b*c

a * (a + b) = a*a + a*b = a**2 + a*b

a * (a - b) = a*a - a*b = a**2 - a*b

(a + b) * (a + b) = a*a + a*b + b*a + b*b  = a**2 + 2*a*b + b**2

(a + b) * (a - b) = a*a - a*b + b*a - b*b  = a**2 - b**2

(a - b) * (a - b) = a*a - a*b - b*a + b*b  = a**2 - 2*a*b + b**2


Answer to case 1:

3*(x+2) * (x-2) = 3*(x**2 - 4) = 3*x**2 - 12

And graph of:  y = 3*x**2 - 12
is:

a parabola opening up with roots -2 and 2 and mid-point of roots is 0,
so parabola is symmetric about the y-axis with minimum value of -12,
i.e. vertex is at point (0,-12), since

 3*(0)**2 -12 = -12


Answer to case 2:

-2*(x-3)*(x-2) = -2*(x**2 - 5*x + 6) = -2*x**2 +10*x - 12

And graph of:  y = -2*(x-3)*(x-2)
is:

a parabola opening down with roots 2 and 3 and mid-point of roots is (2+3)/2 = 5/2,
so axis of symmetry is a line parallel to the y-axis that intersects the x-axis at
x = 5/2 = 2.5

Substituting in this (middle-roots-point) x = 5/2, we get:

-2*(5/2-3)*(5/2-2) = -2*(-1/2)*(1/2) = 1/2

So vertex is point (5/2, 1/2) 
i.e. (2.5, 0.5)

And maximum value is 1/2 i.e. 0.5



For the two cases above ... can you now sketch the graph using the 3 points 'supplied' ?



Using the hint re. the ball trajectory problem ...

i.e. using the relation:
h = f(t) with:
f(t) - 8 = 32*t -(1/2)*(32)*t*t

where h is the height in feet and t is the time in seconds ...

The easy way to do this is to ... 'do the problem from the perspective of having the x-axis
at the point where the ball leaves the throwers hand.'

There, we can let:

y = 32*t - 16*t*t = 16*t(2 - t)

which has easy to find roots (i.e. zeros at) : t=0, t=2 with mid-point t=1

And when t=1, then y = 32 - 16 = 16

But we started here at 8 feet up already,
so the max height reached by the ball is: (8+16) feet = 24 feet, and that height is reached
after 1 second.

Note that because of symmetry of ideal conditions assumed here,
the time to fall back down to the level of the throwers hand
is the same time, 1 second, for the ball to reach its max height here,
so after 2 seconds the ball is back at 8 feet above the ground ...
but now traveling down at 32 feet per second ...
with acceleration down of 32 feet per second per second.

So ... we can now solve for an other t,
here, t is the added time to reach the ground,
in a new relation: 

8 = 32*t + 16*t*t

Re-writing this we can set:              y = 16*t*t +32*t - 8

and solving this for y = 0 we get:       0 = 16*t*t +32*t - 8

Thus: (dividing everything by 8 we get): 0 = 2*t*t +4*t - 1

We can solve for the roots here easily by the ... '
completing the square' method:

1 = 2*t*t +4*t

1/2 = t**2 + 2*t = (t + 1)**2 - 1

Thus:
3/2 = (t+1)**2

and then ...
(t+1) = sqrt( 3/2 )
and thus:
t = sqrt(3/2) - 1

And this is the added time, about 0.225 seconds,
for the ball to hit the ground,
as it descends from the 8 foot height, to the ground.

So the total flight time for the ball is: (2 + 0.225) sec's


And if the ball had a constant horizontal velocity component
(in the positive x direction) of 10 feet per second,
after 2.225 sec's it would be 22.25 feet away
from the place from which it was thrown.


Can you now see how to do this, the easy way,
with the horizontal axis moved to the place from which the ball starts to move up,
and to do the whole problem in easier steps ...
using the symmetry conditions of the perfect parabola shaped curve assumed here ?
« Last Edit: May 18, 2016, 12:05:41 AM by David »