Programming Lesson: Distance Between Two Points

Assignment: Write a program to find the distance between two points.

To find the distance (c) between two points (x1, y1) and (x2, y2) you use the equation:

  c = \sqrt{a^2 + b^2}

where a and b are:

  a = x_2 - x_1
  b = y_2 - y_1

So, let’s find the distance between the points (1,7) and (5, 2) using python:

# define inputs
x1 = 1
y1 = 7
x2 = 5
y2 = 2

# calculate distance
a = x2 - x1
b = y2 - y1

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

# output the result
print("Distance between two points.")
print("Point 1:", x1, y1)
print("Point 2:", x2, y2)
print("Distance:", c)

The result should look like:

Distance between two points.
Point 1: 1 7
Point 2: 5 2
Distance: 6.40312

Coatis

Coati at the Pocosol Research Station in the Children's Eternal Rainforest , Costa Rica.
Coati at the Pocosol Research Station in the Children’s Eternal Rainforest , Costa Rica.

White-nosed Coatis (Nasua narica) are diurnal and, unusually for most Central America carnivores, highly social. They seem to fill a similar niche to raccoons. Ran into this one wandering around the Pocotal Research Station in the Bosque Eterno de los NiƱos, Costa Rica. It did not seem too perturbed that we were within 10 meters of each other.

Headless Pi Zero

Pi Zero‘s are even cheaper versions of the Raspberry Pi. The price you pay is that they’re harder to connect to since all the ports are small (micro-USB’s and mini-HDMI’s), so you need adapters to connect to keyboards, mice, and monitors. However, if you have the wireless version (Pi Zero W) you can set it up to automatically connect to the WiFi network, and work on it through there using the command line. This is a brief summary of how to do this (it’s called a “headless” setup) based on Taron Foxworth’s instructions. It should work for the full Raspberry Pi as well.

Set up the Operating System

You can install the Raspbian Stretch (or Raspbian Lite which does not include the desktop GUI that you will not use) on a SD card (I used 8 or 16 Gb cards).

  • Download Raspbian.
  • SD Card Formatter to format the SD Card. It’s pretty quick, just follow the instructions.
  • Etcher: to install the operating system on the SD Card

Set up automatic connection to WiFi

You may have to remove and reinsert the SD Card to get it to show up on the file system, but once you have you can set it up to automatically connect to WiFi by:

  • Go into the /boot partition (it usually shows up as the base of the SD Card on your file manager) and create a file named “ssh”.
  • Create a text file called “wpa_supplicant.conf” in the /boot folder, and put the following into it, assuming that the wifi router you’re connecting to is called “myWifi” and the password is “myPassword”.
  • country=US
    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    update_config=1
    
    network={
     ssid="myWifi"
     scan_ssid=1
     psk="myPassword"
     key_mgmt=WPA-PSK
    }
    

Now put the SD Card into the Pi and plug in the power.

Find the IP address

First you have to find the Pi on the local network. On Windows I use Cygwin to get something that looks and acts a bit like a unix terminal (on Mac you can use Terminal, or any shell window on Linux).

To find the ip address of your local network use:

on Windows:

> ipconfig

look for the “IPv4 Address”.

Mac (and Linux?)

> ifconfig

look under “en1:”

Connecting

To connect you use ssh on the command line (Mac or Linux) or something like Putty

Static IP

Without a static IP address it is possible that the Pi’s IP address will change occasionally. I followed the instructions on Circuit Basics and MODMYPI, but basically you have to:

  • Identify your network information:
    • Find your Gateway:
    • > route -ne
      
    • Find your Domain Server:
    • > cat /etc/resolv.conf
      
  • Add the information to the end of your /etc/dhcpcd.conf file (here the wlan0 means you’re doing this to the wireless interface (WiFi) and the static ip is 10.0.0.99, the static router the gateway ip you found above, and the domain_name_servers takes the domain server ip):
  • interface wlan0
    static ip_address=10.0.0.99
    static routers=10.0.0.1
    static domain_name_servers=75.75.75.75
    
  • Now reboot and you should be able to ssh or sftp into the new static IP.

A Web Server

Adding a webserver (apache with php) is pretty easy as well, just run the commands (from the Raspberry Pi Foundation)

Apache web server:

sudo apt-get install apache2 -y

PHP for server-side scripting

sudo apt-get install php libapache2-mod-php -y

Now your can find the webpage by going to the ip address (e.g. http://10.0.0.1).

The actual file that you’re seeing is located on the Pi at:

/var/www/html/index.html

You will probably need to change the ownership of the file in order to edit it by running:

> sudo chown pi: index.html

Solving 1D Heat Transfer Problems with Matricies

Say, for example, we have a furnace with an interior temperature of 1560 K (hot enough to melt copper) while the outside temperature is a balmy 300 K (27 Celcius). The wall of the furnace is 1 meter thick (it’s a big furnace). Can we figure out how the temperature changes inside the wall as goes from hot on the inside to cool on the outside?

Figure 1. Five nodes (points of interest) where we will calculate the heat flow through a furnace wall.
Figure 1. Five nodes (points of interest) where we will calculate the heat flow through a furnace wall.

To start with, we’ll look at the temperature at five points (call them nodes): three are in the wall and two at the edges (see Figure 1). We’ll say the temperature at node 1 which is next to the furnace is T1, and the temperature at the outer edge of the wall is the 5th node (T5) so:

  • T1 = 1560 K
  • T5 = 300 K

Now we need to find the temperature at nodes 2, 3, and 4.

Heat Flow at Equilibrium

For each of the interior nodes, we can consider how things are at equilibrium. Heat is always moving through the wall, but as some point in time the heat flowing from the furnace into the wall will be equal to the heat flowing out of the wall, and for each node the heat flowing in will equal the heat flowing out.

The heat flow (Q) from one location to another is driven by the difference in temperature (ΔT): heat flows from high temperature to cooler temperatures (which makes ΔT negative). However, you also need to consider the distance the heat is traveling (Δx) since, given the same temperature drop, heat will flow faster if the distance is short. So, it’s best to consider the temperature gradient, which is how fast the temperature is changing over distance:

 \text{Temperature gradient} = \frac{\Delta T}{\Delta x}

How fast heat flows is also mediated by the type of material (different materials have different thermal conductivities (K)), so our heat flow equation is given by the equation:

 Q = - K \frac{\Delta T}{\Delta x}

Consider Node 2

HEAT IN: Heat is flowing into Node 2 from Node 1, so:

 Q_{in} = - K \frac{T_2 - T_1}{\Delta x}

HEAT OUT: Heat flows out to Node 3 so:

 Q_{out} = - K \frac{T_3 - T_2}{\Delta x}

At equilibrium the heat into the node equals the heat out:

 Q_{in} = Q_{out}

so:

 - K \frac{T_2 - T_1}{\Delta x} =  - K \frac{T_3 - T_2}{\Delta x}

Which we can simplify a lot because we’re assuming the thermal conductivity of the wall material is constant and we’ve (conveniently) made the spacing between our nodes (Δx) the same. Therefore:

 T_2 - T_1 =  T_3 - T_2

And now we solve for temperature at T2 to get:

 T_2 =  \frac{T_1 +T_3}{2}

Finally, we can break the fraction into separate terms (we need to do this to make it easier to solve using matricies as you’ll see later) and start using decimals.

 T_2 =  0.5 T_1 + 0.5 T_2

If we do the same for all the internal nodes we get:

 T_3 =  0.5 T_2 + 0.5 T_3

 T_4 =  0.5 T_3 + 0.5 T_5

You should be able to see here that the temperature at each node depends on the temperature of the nodes next to it, and we can’t directly solve this because we don’t know the temperatures of the interior nodes.

A System of Equations

Let’s collect all of our information to get a system of equations:

  • T1 = 1560 K
  • T2 = 0.5 T1 + 0.5 T3
  • T3 = 0.5 T2 + 0.5 T4
  • T4 = 0.5 T3 + 0.5 T5
  • T5 = 300 K

Now to rewrite this as a matrix. We start by putting all terms with variables on the left and all the constants on the right.

  • T1 = 1560 K
  • -0.5 T1 + T2 – 0.5 T3 = 0
  • -0.5 T2 + T3 – 0.5 T4 = 0
  • -0.5 T3 + T4 – 0.5 T5 = 0
  • T5 = 300 K

Now to matrixize:

\begin{bmatrix} 1 & 0 & 0 & 0 & 0 \\ -0.5 & 1 & -0.5 & 0 & 0 \\ 0 & -0.5 & 1 & -0.5 & 0 \\ 0 & 0 & -0.5 & 1 & -0.5 \\ 0 & 0 & 0 & 0 & 1  \end{bmatrix}  \begin{bmatrix} T_1 \\ T_2 \\ T_3 \\ T_4 \\ T_5  \end{bmatrix} =  \begin{bmatrix} 1560 \\ 0 \\ 0 \\ 0 \\ 300 \end{bmatrix}

This you can solve on paper, and, if you’d like, check your answer using Alex Shine’s Gaussian Elimination solver, which gives step by step output (although it’s a little hard to follow). I used Alex’s solver to get the result:

Solution
Variable 1 = 1560.0
Variable 2 = 1245.0
Variable 3 = 930.0
Variable 4 = 615.0
Variable 5 = 300.0

This problem is set up to be easy to check because the results should be linear (if you plot temperature versus distance through the wall you’ll get a straight line). It will also give you whole number results up to 10 nodes.

Next Steps

Any Number of Nodes

This procedure for setting up the matrix give the same basic equations no matter how many nodes you use, because as long as the distance between the nodes (Δx) and the thermal conductivity (K) are constant, the equation for each internal node (Ti) will be:

[math] T_i = \frac{T_{i-1} + T_{i+1}}{2}

and the matrix will continue to just have three terms along the diagonal.

Non-uniform Wall and Non-uniform Node Spacing

If the wall is not uniform then the thermal conductivity coefficient does not just drop out of the equation, so you’ll have to pay attention to the conductivity going into and out of each node. Same goes with the node spacing.

Alternative Solution Methods

Students could try:

  • writing their own matrix solvers.
  • setting up the system of equations in a spreadsheet program (they’ll need to change your program’s settings so that it uses its iterative solver).

Emotional Words

The NRC Word-Emotion Association Lexicon (aka EmoLex) associates a long list of english words with their emotional connotations. It’s stored as a large text file, which should make it easy for my programming students to use to analyse texts.

David Robinson used this database to help distinguish tweets sent by Donald Trump from those sent by his staff (all on the same twitter account).

Emotional sentiment of Donald Trump's Twitter feed.  From Variance Explained
Emotional sentiment of Donald Trump’s Twitter feed. From Variance Explained

Bloom’s Taxonomy

From Wikimedia: https://commons.wikimedia.org/wiki/File:BloomsCognitiveDomain.svg
From Wikimedia: https://commons.wikimedia.org/wiki/File:BloomsCognitiveDomain.svg

Bloom’s cognitive taxonomy offers a useful model for defining learning objectives. You start with the basic knowledge of the subject that requires some memorization: fundamental constants like the speed of light; fundamental concepts like conservation of mass and energy; and basic equations like Newton’s laws. On the second level, you use these basic facts and concepts to extrapolate and generalize with questions like: is the Earth an open or closed system with respect to mass and energy? And then we can start to apply our knowledge and understanding to problem solving: determine the average temperature of the Earth based on conservation of energy. Finally, at the highest level, we can analyse our models and evaluate their advantages and disadvantages.

Building a 3d Printer

Building the printer.
Building the printer.

I took three students to a workshop were we’re building a 3d printer. It’s run out of the Whitfield School. We spend today putting together the electronics to run the five motors we need to get the thing to work, and starting to put the frame together.

The frame is based on the RepRap Prusa i3 (via DIY Tech Shop), and the plastic parts that hold the rods, electronics and other metal bits together are 3d printed themselves.

Students learn to solder.
Students learn to solder.

The motors is run by an Arduino, which is great because I’ve been thinking about using one to operate the doors to the chicken coops.

Notes on hardware

Microcontroller

  • Arduino Mega 2560 R3
  • RAMPS Shield 1.4
  • RepRap StepStick Pololu A4988 (I think) stepper driver (plus heat sinks)

Filament: 1.75 mm PLA