Calculators to Computers

Once we are comfortable with the basic calculator syntax, we can use computers for calculations.

Computers allow much more power, speed, and scope than our calculators.

Computing platforms

  • You are likely most familiar with a calculator
  • You enter a series of commands
  • When you press enter or equals, they are sent to a small computer for evaluation
  • The results are printed for you
  • There are many computer programs that do similar things and allow much more power and flexibility
  • Other platforms are Mathematica and spreadsheets

Computing languages

  • Computing languages allow us to instruct the computer to do things
  • As our models and computations become more complex, we will want to do things besides addition and multiplication
  • Using a computing program (Excel) or language (Python) helps us achieve that

Spreadsheets

Spreadsheets are an ubiquitous computational tool based on placing data and calculations in rows and columns.

  • Each box is called a cell
  • Typing an = as the first character in a cell tells the spreadsheet you are entering a formula
  • To reference a number in another cell, use the numerical row and alphabetical column. For example second row in the Bth column would be B2.
  • You can use this perform mathematics. For example =B1+B2 adds two numbers.

Variables

To make the details of a computation more clear, we can use readable names for our numbers and then use the names in the calculation.

This makes it possible to more easily communicate the details of our calculation to others.

power = 100
time = 30
energy = power * time

This makes the intention of the calculation more clear to the reader.

In our language (python) the computer decides how to interpret each variable by what you put inside it.

This is possible in spreadsheets.

In spreadsheets, each box is a variable that uses the row and column (battleship notation) to refer to it. These boxes can hold either numbers or calculations.

Functions

You have often used functions on your calculator and you have encountered the idea in your math classes.

A function takes a number or numbers as an input and provides a number or numbers as an output.

You have probably used sine or cosine functions on your calculator.

You may want to make your own function for a calculation that you do frequently. The syntax for this often varies but the idea is usually the same.

m = 1
b = 10
f(x) = m * x + b
f(5) => 15

Units

Computation of physical quantities often relies on the human to define and use a consistent set of units of measurement. There are tools that allow us to add physical quantities to our calculations, but they are not as rich as I could like them to be. One good practice is to explicitly include the unit name in the variable name.

power_watt = 100
time_sec = 30
energy_joule = power_watt * time_sec

Units

There are also computer libraries that allow you to include units in the definition of your variables.

Pint python library

%pip install pint
u = pint.UnitRegistry()
power = 100 * u.watt
time = 100 * u.sec
energy = power * time

The energy will now have units. These libraries are also able to convert these units.

Spreadsheet Conventions

When we are creating spreadsheets your audience expects certain conventions to be followed

  • Computations usually flow from left to right or from top to bottom
  • Cells that are to be changed by the user are clearly marked
  • Cells that should not be changed by the user are clearly marked
  • The details of the computation should be relatively easy to see

Try Jupyter

You can experiment with this library using the TryJupyter web application and launching the JupyterLab environment.