+ All Categories
Home > Documents > ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this...

ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this...

Date post: 23-Apr-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
47
ENGG1811 © UNSW, CRICOS Provider No: 00098G1 slide 1 Week 3: Functions, list, for, plotting ENGG1811 Computing for Engineers
Transcript
Page 1: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

ENGG1811 © UNSW, CRICOS Provider No: 00098G1 slide 1

Week 3: Functions, list, for, plotting

ENGG1811 Computing for Engineers

Page 2: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

The goal for this week

• We will cover 4 different topics this week. They are:– Functions– List– For-loop– Plotting

• The reason why we have put these 4 topics together is that you can do something useful by combining them

• We will work on a project with makes use of these four topics. We will start by telling you the project’s goal and show you the end product.

• We will then look at these 4 topics one by one.

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 2

Page 3: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Project: goal

• If you drop an object of mass m in a medium with drag coefficient d and acceleration due to gravity g, then the object’s speed v(t) at time t is given by:

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 3

• Given the numerical value of m, g and d, the goal of the project is to plot v(t) against t– for t = 0, 0.5, 1, 1.5, …., 39.5, 40

• You certainly know how to do this by using pen, paper and calculator

• You may also need a bit of perseverance because it does get a bit repetitive

Page 4: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Project: end product

• Goal: Given the numerical value of m, g and d, the goal of the project is to plot v(t) against t– for t = 0, 0.5, 1, 1.5, …., 39.5, 40

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 4

• You will do it in Python

• The end product

Page 5: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

This week’s topics

• Functions • List• For-loop• Plotting

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 5

Page 6: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Functions

• We talked about functions in Week 1 and you used it in your lab in Week 2

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 6

• We will show you how to write your own functions

Page 7: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Your first function

• You know that when you use the function math.cos(), you input a value and get an output

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 7

• You will now write a function which squares the input value and then outputs it

math.cos()1.34 cosine of 1.34

Page 8: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

my_square()• Open the file my_square_prelim.py that comes with this

week’s lecture• Type in Lines 12-14 as shown below

– Don’t forget the : at the end of Line 12– The indentation in Lines 13-14 is important

• And then run the program

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 8

Page 9: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Anatomy of a function• Line 12:

– def means you want to define a function– The name of the function is my_square– x is the identifier you give to the input

• Lines 13 and 14 are indented relative to def so they belong to the function definition

• Lines 16 and 17 are not indented, so they are not part of the function

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 9

Page 10: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Mechanics of function evaluation (1)• Line 17: The function my_square is called

– Terminology: Calling a function means executing the code inside a function

• Because the variable a has the value of 5, the identifier x in the function is assigned the value of 5

• The code inside the function is executed sequentially• Line 13: the identifier y is assigned the value of 25

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 10

Page 11: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Mechanics of function evaluation (2)

• return y in Line 14 means the value of y (which is 25) is to be put at the place where the function is called

• The right-hand-side of Line 17 is now 25 • b is then assigned the value of 25

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 11

Page 12: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Multiple inputs

• Code in my_power.py• You can have multiple inputs to a function

– For example, the function my_power has two inputs (Line 12)• When the function my_power is called in Lines 15 and 17,

there are 2 values inside the parentheses separated by a comma

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 12

Page 13: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Orderly assignment

x ← 5 n ← 2

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 13

x ← 2 n ← 5

Page 14: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Local scope• The code is in local.py• Note that there is a variable y in the function and there is

also a variable y outside the function• Are they the same?

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 14

def my_power(x,n): y = x ** n return y

y = 4z = my_power(y,2)

print('y = ' ,y)print('z = ' ,z)

We will copy the code to the Python tutor website which allows us to visualise the execution of the code

http://pythontutor.com/visualize.html

Page 15: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Local variable scope• The variables in the function are stored in a separate

memory space– This applies to data types int, float, str, bool– But not for all data types, will tell you more later

• We say the scope of the variable is local to the function

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 15

def my_power(x,n): y = x ** n return y

y = 4z = my_power(y,2)

print('y = ' ,y)print('z = ' ,z)

Memory space for my_power

x

n

4

2

Base memory space

y 4

y 16

Page 16: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Multiple outputs

• Code in my_power3.py

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 16

x ← 5 n1 ← 2n2 ← 3n3 ← 4

Page 17: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Functions can call other functions • The code is in my_power3_improved.py• A function can call other functions

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 17

Page 18: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Function must be defined before they can be called

• Python expects that you define the functions before they are called

• The following code will not work because the function my_square is called in Line 13 but its definition is only found later in Line 16

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 18

DOESN’T WORK

Page 19: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Function must be defined before they can be called (cont’d)

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 19

• Consider the code my_power3_improved.py where the function my_power3() calls the function my_power()

• The function my_power3() is called the first time in Line 22. So my_power3() and the function it calls must all be defined before this line.

• However, my_power3() and the functions it calls can appear in any order.

Can exchange the order but both must be defined before my_power3() is called

Page 20: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Project (Part 1)• mass m, drag coefficient d, acceleration due to gravity g• speed v(t) at time t is:

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 20

• Exercise:

– Open the file project_prelim.py– Write a function called free_fall() to compute v(t)

• The def line of the function is given in Line 16:�

• The calculation requires:– Constant g – defined in Line 13 – The math library (imported in Line 9) and math.exp() to calculate

exponential. E.g math.exp(-2.1) gives e-2.1

• The function should return the computed speed• Testing: Lines 28 & 29 have the expected values

def free_fall(t,mass,drag):

Page 21: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

This week’s topics

• Functions • List• For-loop• Plotting

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 21

Page 22: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

List • You have come across a number of data types:

– int, float, str, bool, etc.

• We will now introduce a new data type called list

• A list consists of a sequence of objects enclosed in [ ] and separated by commas

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 22

List is useful because you can use them with loops

Page 23: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Why using loops in programming?

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 23

• Let us hear from Mark Zuckerberg (founder of Facebook) on why you need loops in programming

• https://www.youtube.com/watch?v=mgooqyWMTxk

Page 24: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

ENGG1811 © UNSW, CRICOS Provider No: 00098G W6 slide 24

Iteration (Repetition)

• Often need to execute statements repeatedly

• Loops are statements that can do this

• Process is called iteration

• Kinds of loop:– For (iterate a fixed number of times)

– While (iterate as long as something is True)• We will spend a part of the lecture in the next few

weeks to learn about loops

Page 25: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

ENGG1811 © UNSW, CRICOS Provider No: 00098G W6 slide 25

G’day, mate!

• I wish to say G’day to each student in an ENGG1811 class.

• I’ve created a list of names. There are 484 names.

Page 26: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Let us type the following code:

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 26

There are still 468 lines L

Page 27: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

The enlightened way

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 27

These two lines of code print out the 484 G’day

• The code is in gday.py

Page 28: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

For loop

for name in ["Charlie", "Hannah", "Olivia", "Usman"]: print("G'day, ",name)

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 28

• The code is in gday_explained.py

• Let us copy the code to Python Tutor and see how it is executed

• http://pythontutor.com/

Page 29: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

The for-loop explained

• The variable name is called the loop variable • Code under for-loop is indented• The loop variable is assigned to the first item in the list• name is now the string "Charlie”. The code in the for-loop is

executed assuming this value of name• After executing the code under the for-loop, execution return

to the for-line. The computer checks whether there is a next item in the list. Yes, there is so the computer assigned "Hannah" to the variable name. The code in the for-loop is executed assuming this value of name

• This is repeated until all items in the list have been usedENGG1811 © UNSW, CRICOS Provider No: 00098G slide 29

for name in ["Charlie", "Hannah", "Olivia", "Usman"]: print("G'day, ",name)

Page 30: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Flowchart

http://interactivepython.org/runestone/static/thinkcspy/PythonTurtle/FlowofExecutionoftheforLoop.html

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 30

Page 31: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Examples of for-loop

• We will switch to Spyder to look at a number of examples of using for-loop – File name: for_examples.py

• Spyder allows us to divide the code into cells and we can run the code in each cell independently – Good for testing and debugging code– To run a cell, make sure your mouse cursor is in that cell

and click

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 31

Page 32: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

For-loop exercise:

• �You are asked to write a for-loop to print out all the negative numbers in a given list of numbers

• You can do it in for_exercise_prelim.py

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 32

Page 33: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

range()

• range() is a Python function that generates a sequence of integers

• The function can take 1 to 3 inputs and its behaviour depends on the number of inputs

• Examples in range_ex.py

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 33

range() expression sequence explanationrange(5) 0,1,2,3,4 One input. Starting from 0. Keep

increasing by 1. Does not including the number specified by the input.

range(2,8) 2,3,4,5,6,7 Two inputs. 1st number in list = 1st input

• With 2 inputs, the function has the form range(start,stop)– range(0,stop) is the same as range(stop)

• #elements in the list = stop - start

Page 34: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

range()

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 34

range() expression

sequence explanation

range(2,20,4) 2,6,10,14,16 The first input (=2 in this example) is the starting value of the sequence. The last input (= 4 in this example) is the increment. The next element of the sequence is obtained by adding the increment to the element before:

2, 2 + 4, 2 + 4 + 4

Keep incrementing until a number >= the last input (= 20 in this case) is reached. Stop but don’t include the last number generated.

• The general form is range(start,stop,inc)• #elements in the list = ceil ((stop-start)/inc)

– ceil(x) = smallest integer greater than or equal to x

Page 35: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

This week’s topics

• Functions • List• For-loop• Plotting

– Another application of list is to use it with experimental data

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 35

Page 36: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Tensile testing machine

• To understand how materials behave under tensile force • Pull the specimen and measure its length

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 36

Source: Holly Moore, Matlab for Engineers. p.197

Page 37: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Data from a test

Load [lbf] Length [inches]0 2.000

1650 2.0023400 2.0045200 2.0066850 2.0087750 2.0108650 2.0209300 2.040

10100 2.08010400 2.120

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 37

Source: Holly Moore, Matlab for Engineers. p.197

• Make two lists – One for load– The other for length

• Plot load on the horizontal axis and length on the vertical axis

Page 38: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Plotting graph • The code is in plot_demo.py

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 38

Page 39: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Code for graph plotting

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 39

Short form Lines 11-15Put the data in 2 lists

Lines 17-27 Plotting graphs

Import library

Page 40: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

matplotlib

• matplotlib is a large library with many functions• You can do plots of many different styles

– Pie chart, histogram, log-log, log-linear, 3D and even animation

• And also to customise them in many ways • We will only show you the basic plot types • The library is well documented and its website has

many examples – https://matplotlib.org

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 40

Page 41: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Operations on list

• You know how to append an element to a list • There are other operations that you can do on a list

– Finding the maximum or minimum element in a list– Sum the elements in a list – Determining the number of elements in a list

• Terminology: length of a list = number of elements in a list – Sort a list

• See list_processing.py

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 41

Page 42: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Line continuation

• Sometimes you have a long line of code, it is best that split it into multiple lines so that you don’t have to scroll to the right to read

• Python uses two methods to say that code typed in multiple lines of code is in fact one line of code– Implicit continuation with brackets (), [], {}– Explicit continuation with \

• Demo code in continuation.py

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 42

Page 43: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Project (Part 2)

• We will spend the rest of the lecture to complete the project

• Goal: Given the numerical value of m, g and d, the goal of the project is to plot speed v(t) against t– for t = 0, 0.5, 1, 1.5, …., 39.5, 40

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 43

• Note that you have already got a function that can compute the speed, so you should make use of it

Page 44: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Project: Producing the graph

• You want to plot a graph of the free fall speed against time• In order to produce the graph, you need to create two lists

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 44

Page 45: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

List of time instants

• There are 81 numbers in the list and of course you are not going to type these 81 numbers in

• The function range() will be useful here but you need to know range() can only generate a sequence of integers, it cannot generate numbers with decimal points

• The hints are:– You can generate this list by using range() together with a

for-loop– The numbers are all multiples of a constant

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 45

• The first list is a list of time instants (in seconds). We ask you to use:

[0 0.5 1 1.5 2 2.5 39.5 40]

Page 46: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

List of speeds

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 46

• The second list is a list of speeds• If you do this manually, you will do:

– Time is 0. Use the speed formula. Speed = 0.– Time is 0.5. Use the speed formula. Speed = 4.692400935– Time is 1. Use the speed formula. Speed = 8.98399681455

– Time is 40. Use the speed formula. Speed = 54.8885179036

• Of course, you aren’t going to do the manual way since you have seen the trick

• You should use the list of times and the function you wrote • File project_prelim.py

Page 47: ENGG1811 Computing for Engineersen1811/19T2/lecs/week03/... · 2019-02-06 · The goal for this week • We will cover 4 different topics this week. They are: – Functions – List

Summary

• Functions– Writing functions

• New data types: List and range • Using functions from matplotlib• For-loop

– To repeatedly do some actions

ENGG1811 © UNSW, CRICOS Provider No: 00098G slide 47


Recommended