Skip to content

Latest commit

 

History

History
120 lines (86 loc) · 2.17 KB

File metadata and controls

120 lines (86 loc) · 2.17 KB

Algebraic Equations

Some of the most fundamental of computer programs have been ones that take the drudgery of doing math by a person, and making the computer do the math. These kinds of computations rely on the fact that the computer won’t do the wrong thing if it’s programed carefully.

Given a simple math equation like:

Tip

a = b3 - 6 and if b equals 3, then a equals ?

In math class, your teacher would have said "How do we solve for a?" The best way to solve for a = b3 - 6 is to

  • figure out what b times 3 is (well, if b equals 3, then 3 times 3 is 9)

  • subtract 6 from b times 3 (and then 9 minus 6 is 3)

# And in Python:
# a = b3 - 6

b = 3
a = b * 3 - 6
print(a) # -> 3

Now you try it.

Tip

Solve the equation with Python…​

q = 2j + 20

if j = 5, then q = ?

Take a moment and write down your solution before reading on.

q = 0
j = 5
q = 2 * j + 20
print(q) # -> 30

Let’s try another…​

Tip

Solve the equation with Python…​

x = 5y + y3 - 7

if y=2, x = ?

and print out x.

My solution is pretty simple.

import math # if you haven't already!

y = 2
x = 5 * y + math.pow(y, 3) - 7
print(x) # -> 11

Trigonometry

The word trigonometry comes from the Greek words, trigonon ("triangle") + metron ("measure"). We use trigonometry to find angles, distances and areas.

For example, if we wanted to find the area of a triangular piece of land, we could use the equation AreaOfaTriangle = height * base / 2

Therefore we just need to create variables for each and use the operators to calculate the area.

Tip

Calculate Area of a Triangle in Python Height is 20 Base is 10 Formula: A = h * b / 2

height = 20
base = 10
areaOfaTriangle  =  height * base / 2
print(areaOfaTriangle) # -> 100
Tip

Calculate the area of a circle whose radius is 7.7 Formula: area = Pi * radius2

Hint: You’ll need to use a constant Math property!

Here is a possible solution to the calculation.

radius = 7.7
area  =  math.pi * math.pow(radius, 2)
print(area) # -> 186.26502843133886 (wow)

See how we used math.pi to handle the equation?