Lecture 2 – Arrays and Interpolation

What are  Arrays?

You will find Arrays to be one of your best friends in programming. An array is simply a a series of objects all of which are the same size and type. Each object in an array is called an array element. These elements are indexed;

For example here is a representation of an Array

Value A B C
Index 0 1 2

This array can be defined (in Python) by simply stating

array = [“A”, “B”, “C”] and we can easily access the elements

array[0] is A, array[1] is B, array[2] is C

Parallel Arrays in Engineering

For engineering applications Arrays will normally contain numbers. For example consider the array of probabilities shown below;

p_array = [0.001, 0.01, 0.1, 0.5, 0.9, 0.99, 0.999]

This array would be useful for keeping track of extreme probabilities on the low end and high end of a probability distributions. Of course these are simply the probabilities, but not the values associated with the probabilities. If we continue our example we could say these probabilities coincide with the amount of rainfall exceeding a value for monthly annual rainfall.

r_array = [9.3, 7.8, 5.6, 4.0, 3.4, 2.3, 1.2]

The 0.001 value coincides with 9.3 and means that there is a 0.001 (0.1%) chance that the rainfall will exceed a value of 9.3.

The 0.999 value coincides with a 1.2 and means there is a 0.999 (99.9%) chance the rainfall will exceed a value of 1.2.

What is important here is that the first array coincides with the second array or in mathematical terms

P(r > r_array[i]) = p_array[i]

These are also termed parallel arrays.

Now what would happen if we wanted to calculate P(r > 7.0) or the probability that the rainfall will exceed 7.0 inches? This is not in the array – but it can still be estimated.

(1) Calculate a non-integer index for the value 7.0

(7 – 5.6)/(7.8 – 5.6) = 0.636, so the “index” is 0.636 greater than the index for 7.8

Non-integer index  = 1 + 0.636 = 1.636  (1 is the index of 7.8)

(2) This non-integer index can then be used to estimate the value from the other array

So this value is 0.636 between the values in the other array 0.01 and 0.1

(x – 0.1)/(0.01 – 0.1) = 0.636    (I wrote it this way so you can see parallel)

x = 0.0427

The probability of a rainfall > 7.0 is 0.0427 or 4.27%

Parallel Arrays Thought Example (Do this by hand)

Suppose you had 2 sets of numbers like below, say time and kW-hr. This would be a typical case of reading a power meter.

time 0.35 1.27 3.47 5.87
kW-hr 9.57 11.35 15.65 19.57

I record the time and the reading of kW-hr of power use (cumulative) to that point.

How would you estimate the power used at time 2.5? Do it by hand first. Now figure out how to put that in code.

(hint – the answer is 13.7537)

Video

This video will help you determine how to do these types of calculation and many more like it in Python.

Here is an example of using parallel arrays in Python