So far in this class we have worked to review some mathematics fundamentals and to introduce you to the ways that Python can help you do the calculations and check your answers. The goal of this notebook is to give you answers to all the review problems and to be your cheat sheet for what we have done so far.
Rounding
# What will the code do?'''my_number = 14.9858187509decimal_place = 0rounded_number = round(my_number,decimal_place)print(rounded_number)'''print('Make a guess (do the calculation) before you run the code!')
Make a guess (do the calculation) before you run the code!
Answer:
This code takes the number 14.9858187509 and rounds it right at the decimal place. The result will be 15.
Exponential Notation
# What will the code do?'''my_number = 135726384769325my_format = ".2e"scientific_notation = format(my_number, my_format)print(scientific_notation)'''print('Make a guess (do the calculation) before you run the code!')
Make a guess (do the calculation) before you run the code!
Answer:
This code will take the number 135726384769325 and reformat it in scientific notation, with just one number before the decimal point. To do this we have to move the decimal point to the left 14 times. Giving us \(1.35726384769325 \times 10^{14}\). The we round to two places after the decimal point giving us \(1.36 \times 10^{14}\) or in Python notation 1.36e+14.
Calculator Calculations
# What will the code do?a =10b =3c =27d =2e =-4'''first_answer = a**d/eprint(first_answer)'''print('Make a guess (do the calculation) before you run the code!')
Make a guess (do the calculation) before you run the code!
Answer:
This code first sets some variables so that the different numbers are stored in the variables a-e. The we see that we are taking a raised to the power of d and then dividing by e:
\[ \frac{10^2}{-4} = \frac{100}{-4} = -25\]
so now first_answer = -25
Chaining Together Calculator Calculations
# What will the code do?'''second_answer = first_answer - cprint(second_answer)'''print('Make a guess (do the calculation) before you run the code!')
Make a guess (do the calculation) before you run the code!
Answer:
Since we stored the first_answer = -25 above. When we run this code it will continue the calculation:
\[ -25 - 27 = -52 \]
so now second_answer = -52
Using numpy to Access Functions
# What will the code do?import numpy as np'''first_answer = np.sin(b)second_answer = np.sqrt(first_answer)print(second_answer)'''print('Make a guess (do the calculation) before you run the code!')
Make a guess (do the calculation) before you run the code!
Answer:
First we have to import numpy so that python has access to the extra functions. Then when we do np.sin(b) this is calculating the trigonometric function (sine):
first_answer \(=\sin(3)=0.1411200080598672\).
After we figure out the first_answer then we are taking the square root of it.
print('How would you write the fraction 5/7 using sp.Rational(a,b)?')
How would you write the fraction 5/7 using sp.Rational(a,b)?
Answer:
# import sympyimport sympy as sp# create the variable for the fractionmy_fraction = sp.Rational(5,7)# show the answermy_fraction
\(\displaystyle \frac{5}{7}\)
Fraction Calculations
First calculate by hand:
\[\frac{\frac{2}{3}+\frac{5}{4}}{\frac{3}{2}}\]
Then use Python and Sympy to confirm your answer.
Answer:
By hand we need to get a common denominator for the numerator, before we can divide the denominator. A common denminator between \(3\) and \(4\) is \(12\). I see I need to multiply \(3\times4=12\). In each case I multiply by a fancy one:
now we can apply the exponent in the numerator. Here we have to do the exponent of each term.
\[ \frac{(2x^5)^2}{x^2} = \frac{4x^{10}}{x^2} \]
finally we can divide out the denominator
\[ \frac{4x^{10}}{x^2} = 4x^8 \]
In Python
# We already have sympy loaded above# Define the symbolsx = sp.symbols('x')# Enter the equationmy_expr = ((x**5+x**2*x**3)**2)/x**2# Show the answer... it is already simplifiedmy_expr
\(\displaystyle 4 x^{8}\)
Simplifying Logarithms and Exponents
First simplify by hand
\[ \ln((e^{x^2})^2) \]
Then use Python and Sympy to confirm your answer
Answer:
By hand we will first deal with the stuff inside the natural log. Here we see and exponent raised to a power so we will use the rule \((x^a)^b=a^{ab}\) again
\[ \ln((e^{x^2})^2) = \ln(e^{2x^2})\]
then we know that the natural log and the exponent base e are inverses.
\[\ln(e^{2x^2}) = 2x^2\]
In Python
# We already have sympy loaded above# Define the symbolsx = sp.symbols('x')# Enter the equationmy_expr = sp.log(sp.exp(x**2)**2)my_expr
\(\displaystyle \log{\left(e^{2 x^{2}} \right)}\)
# If we want it to simplify more, we have to give it more information about xx = sp.symbols('x',real=True, positive=True)my_expr = sp.log(sp.exp(x**2)**2)my_expr
\(\displaystyle 2 x^{2}\)
More Review Problems (from Day3)
For these ones I will show you the python code:
Add the fractions: \[ \frac{x}{2}+\frac{x}{3}+\frac{x}{4}\]
# Define the piecesx = sp.symbols('x')a = x/2b=x/3c=x/4# Do the calculationanswer = x+b+c# Show the answeranswer
\(\displaystyle \frac{19 x}{12}\)
Solve for \(x\): \[3x+2-5=4^2\]
# Define the piecesx = sp.symbols('x')# Enter the equation with everything solved to one sidemy_eqn =3*x+2-4-4**2display(my_eqn) # This shows the equation# Solve for xsp.solve(my_eqn,x)
\(\displaystyle 3 x - 18\)
[6]
Simplify the expression: \[2^33-3\]
# Just enter the numbers2**3*3-3
21
Simplify the expression: \[\ln(e^{\frac{4-2}{3^3}})\]
# Just enter the numbers - you can use numpy or sympy#numpyprint(np.log( np.exp((4-2)/3**3) ))#sympydisplay(sp.log( sp.exp((4-2)/3**3) ))
0.07407407407407406
\(\displaystyle 0.0740740740740741\)
Solve for \(x\): \[\log_4(x)=2\]
# Define the piecesx = sp.symbols('x')# Enter the equation with everything solved to one sidemy_eqn = sp.log(x,4) -2display(my_eqn)# Solve for xsp.solve(my_eqn,x)
# Define the piecesx,y = sp.symbols('x,y')# Enter the equations with everything solved to one sideeqn1 =7*x+2*y-3eqn2 =4*x+3*y-11# Solve the systemsp.solve([eqn1,eqn2], [x,y])
{x: -1, y: 5}
# To plot we need to enter the equations as y = f(x)y_eq1 = (3-7*x)/2y_eq2 = (11-4*x)/3# Then plot - the should intersect at the correct points. In this case (-1,5)plt = sp.plot(y_eq1,y_eq2)
# Define the piecesx,y = sp.symbols('x,y')# Enter the equations with everything solved to one sideeqn1 = x/2-y/5+4eqn2 =3*x+y/2+7# Solve the systemsp.solve([eqn1,eqn2], [x,y])
{x: -4, y: 10}
# To plot we need to enter the equations as y = f(x)y_eq1 = (4+x/2)*5y_eq2 = (-7-3*x)*2# Then plot - the should intersect at the correct points. In this case (-4,10)plt = sp.plot(y_eq1,y_eq2)
Linear Systems from Word Problems
Problem 1
You run an accounting business that specializes in auditing (verifying accounting records). One of your auditors is working on the payroll records for a company with 75 employees. Some are part-time and some are full-time. After working for three days, your auditor tells you that the audit is completed for half of the full-time employees, but there are still 50 employee records to audit. Find out how many of the employees are full time and how many are part-time.
Answer
First you need to turn the words into math. Here is the set of questions I ask:
What are we trying to solve for?
In this case we want to know how many full time and how many part time employees there are. So we can let:
\(x=\)full time
\(y=\)part time
Now what do we know about combinations of these variables:
The company has 75 employees: \(x+y=75\)
The auditor has finished half of the full time employees \(\frac{x}{2}\) and none of the part time \(y\). Since they have 50 records left this means that \(\frac{x}{2}+y = 50\)
Solve the system of two equations for two unknowns
\[x+y=75\]\[\frac{x}{2}+y = 50\]
# Define the piecesx,y = sp.symbols('x,y')# Enter the equations with everything solved to one sideeqn1 = x+y-75eqn2 = x/2+y-50# Solve the systemsp.solve([eqn1,eqn2], [x,y])
{x: 50, y: 25}
Interpret your result: There are 50 full time employees and 25 part time.
Problem 2
A financial planner wants to invest \$8000, some in stocks earning 15% annually and the rest in bonds earning 6% annually. How much should be invested at each rate to get a return of $930 annually from the two investments?
First you need to turn the words into math. Here is the set of questions I ask:
What are we trying to solve for?
In this case we want to know how many stocks and how bonds there are. So we can let:
\(x=\)money in stocks
\(y=\)money in bonds
Now what do we know about combinations of these variables:
The total amount invested is \(x+y=8000\)
The stocks earn 15% and the bonds earn 6% and we want a total return of $930: \(.15x+.06y=930\)
Solve the system of two equations for two unknowns
\[x+y=8000\]\[0.15x+0.06y = 930\]
# Define the piecesx,y = sp.symbols('x,y')# Enter the equations with everything solved to one sideeqn1 = x+y-8000eqn2 =0.15*x+0.06*y-930# Solve the systemsp.solve([eqn1,eqn2], [x,y])
{x: 5000.00000000000, y: 3000.00000000000}
Interpret your results: we should invest $5000 in stocks and $3000 in bonds.
Factoring expressions in Sympy
\[x^2+9x\]
\[x^2+9x-1\]
Answers:
# Problem 1# Define the piecesx = sp.symbols('x')# Enter the expressionmy_expr = x**2+9*xdisplay(my_expr)# Use sympy to factorsp.factor(my_expr)
\(\displaystyle x^{2} + 9 x\)
\(\displaystyle x \left(x + 9\right)\)
# Problem 2# Define the piecesx = sp.symbols('x')# Enter the expressionmy_expr = x**2+9*x-1display(my_expr)# Use sympy to factorsp.factor(my_expr)
\(\displaystyle x^{2} + 9 x - 1\)
\(\displaystyle x^{2} + 9 x - 1\)
What happened here? Well this expression was not able to be nicely factored!
Solving expressions in Sympy
\[x^2-16=0\]
\[x^2+9x-1=0\]
# Problem 1# Define the piecesx = sp.symbols('x')# Enter the expression - always make sure there is a zero on the right hand sidemy_expr = x**2-16display(my_expr)# Use sympy to factordisplay(sp.factor(my_expr))# Use sympy to solvesp.solve(my_expr)
# Problem 2# Define the piecesx = sp.symbols('x')# Enter the expression - always make sure there is a zero on the right hand sidemy_expr = x**2+9*x-1display(my_expr)# Use sympy to factordisplay(sp.factor(my_expr))# Use sympy to solvesp.solve(my_expr)
\(\displaystyle x^{2} + 9 x - 1\)
\(\displaystyle x^{2} + 9 x - 1\)
[-9/2 + sqrt(85)/2, -sqrt(85)/2 - 9/2]
Here we see that the result is what would come out of the quadratic formula!
Expanding expressions using sympy.
\[ (x+4)^2 \]
\[ \]
remember if you are doing these by hand you have to FOIL the polynomials together!
# Problem 1# Define the piecesx = sp.symbols('x')# Enter the expression - always make sure there is a zero on the right hand sidemy_expr = (x+4)**2display(my_expr)# Use sympy to expanddisplay(sp.expand(my_expr))
\(\displaystyle \left(x + 4\right)^{2}\)
\(\displaystyle x^{2} + 8 x + 16\)
# Problem 2# Define the piecesx = sp.symbols('x')# Enter the expression - always make sure there is a zero on the right hand sidemy_expr = x*(x-3)*(x+2)display(my_expr)# Use sympy to expanddisplay(sp.expand(my_expr))
\(\displaystyle x \left(x - 3\right) \left(x + 2\right)\)