You are on page 1of 59

ENGG1801 Engineering Computing

Lecture 2-1 Functions & Plotting Semester 1, 2013 School of Information Technologies The University of Sydney, Australia www.it.usyd.edu.au/~engg1801 engg1801help@it.usyd.edu.au Jason Chan j.chan@sydney.edu.au
ENGG1801 Engineering Computing Jason Chan 1

Announcements
If you enrolled late, go through wk 1 lectures and lab exercises: www.it.usyd.edu.au/~engg1801 All students will be allowed to score marks for the wk 1 and wk 2 labs in this weeks (wk 2) lab

ENGG1801 Engineering Computing Jason Chan

Announcements
You are guaranteed a computer in the lab that you are enrolled in You may attend extra labs if you need more help There are more rules that you can take advantage of read and listen to lecture 1-1 Make sure to attend your classes on time
ENGG1801 Engineering Computing Jason Chan 3

Announcements

ENGG1801 Engineering Computing Jason Chan

Semester 1, 2013 Schedule


Week 1 2 3 4 5 6 7 8 9 10 11 12 13 Exam Start Date 4 March 11 March 18 March 25 March 8 April 15 April 22 April 29 April 6 May 13 May 20 May 27 May 3 June 17 June Lecture topics Introduction, Part 1: Excel Basics Functions, Plotting, Solving equations, File I/O Matrix algebra Part 2: Matlab Basics, If statements, Arrays Mid-Semester Break Loops Functions Functions Text & File I/O, Character strings 2-D and 3-D plotting, Surface plots Creating movies from matrices, Project help Matrix algebra Interpolation and curve fitting, Images Help for the Final Exam Student Vacation (Stuvac) --Final Exam (50%) Project (25%) due 7pm Monday 27 May No lecture on Wednesday No lecture on Wednesday, ANZAC Day (public holiday Thursday) Lab Exam 2 (15%), no lab participation mark Lab Exam 1 (10%), no lecture on Wednesday Good Friday (public holiday Friday) Notice Labs begin (each worth 0.5%)

ENGG1801 Engineering Computing Jason Chan

Functions
A function takes input data and gives back output data depending on the input Very similar to functions in maths: f(n) = 3n + 2 We can use the function by giving input: f(2) f(-1) And the function gives us back an output: f(2) = 3x2 + 2 f(-1) = 3x-1 + 2 =8 = -1
ENGG1801 Engineering Computing Jason Chan

Functions
In Excel, we use functions by giving input data and it gives back output data E.g. we can use the SQRT() function, which gives the square root of the input: =SQRT(2)

ENGG1801 Engineering Computing Jason Chan

Functions
Some other functions:
SIN() COS() TAN() ASIN() ACOS() ATAN() RADIANS() DEGREES() ABS() ROUND() CEILING() FLOOR()

There are many other functions, which we will discover later Note that we dont need to know how the function does its job, we only need to know what it does
ENGG1801 Engineering Computing Jason Chan 8

Functions
In maths, we can have functions that take many inputs: f(x,y) = x + 2y We can use the function by giving inputs: f(-1,3) And the function gives us back an output: f(-1,3) = -1 + 2x3 =5
ENGG1801 Engineering Computing Jason Chan 9

Functions
In general, a function in Excel can also take many inputs
Inputs are called arguments / parameters Different functions can take different numbers of arguments The order of arguments matters Not all arguments are supposed to be numbers Not all functions return numbers
ENGG1801 Engineering Computing Jason Chan 10

IF functions
An example is the IF function:
=IF(disc>0, (-bSQRT(disc))/(2*a), "")

This means:
If disc>0, then use (-bSQRT(disc))/(2*a) If not disc>0, then use "" (nothing between quotes means display nothing)
ENGG1801 Engineering Computing Jason Chan 11

IF functions
In general, an IF function looks like this: IF(A, B, C)
A must be a logical (boolean) expression which evaluates to true or false Both B and C must be things that can be displayed in a cell If A is true, then B is displayed in the cell Otherwise (A is false), C is displayed
ENGG1801 Engineering Computing Jason Chan 12

Logical functions
Some functions can be used in logical expressions (they return true or false) E.g. We want to display an error message if speed is too low or too high
=IF(OR(speed<0, speed>_c), "Speed error", speed)

If speed is less than 0 OR speed is greater than _c, then Speed error is displayed in cell Otherwise, the value of speed is displayed
ENGG1801 Engineering Computing Jason Chan 13

Logical functions
The OR function:
Returns (gives back, or evaluates to) true if either of the arguments (inputs) is true Also returns true if both arguments are true Returns false if all the arguments are false

On the previous slide, the result of


OR(speed<0, speed>_c)

(which can only be true or false) is used by the IF function to decide what to display
ENGG1801 Engineering Computing Jason Chan 14

Logical functions
A
true true false false

B
true false true false

OR(A,B)
true true true false

AND(A,B)
true false false false

There are also the AND and NOT functions

A
true false

NOT(A)
false true
15

ENGG1801 Engineering Computing Jason Chan

Logical functions
These functions can be combined multiple ways to form complex logical expressions E.g. Display Danger if either:
temp 70 or temp < 4 pressure > 30 and temp > 50

Otherwise, display the value of temp


=IF(OR(OR(temp>=70, temp<4), AND(pressure>30, temp>50)), "Danger", temp)
ENGG1801 Engineering Computing Jason Chan 16

Logical functions
There are many ways to write the solution E.g. These 2 are equivalent:
temp>50 NOT(temp<=50)

These 2 are also equivalent:


OR(temp>=70, temp<4) NOT(AND(NOT(temp>=70), NOT(temp<4)))
ENGG1801 Engineering Computing Jason Chan 17

Keep it simple!
Generally speaking, you should use expressions that are simplest to understand
These are not always the shortest

ENGG1801 Engineering Computing Jason Chan

18

Different types of functions


As you type the name of a function, Excel will tell you how many and what kind of arguments it is expecting

ENGG1801 Engineering Computing Jason Chan

19

Different types of functions


Some functions can take many different number of arguments, this is represented by

ENGG1801 Engineering Computing Jason Chan

20

Different types of functions

For more information on what the function does or how to use it, just click on the function name in the little pop-up box
ENGG1801 Engineering Computing Jason Chan 21

Different types of functions


Some functions take no arguments, e.g. PI
Brackets should be empty, e.g. PI()

Some functions can take ranges of cells


The SUM function adds up all values in cells between and including 2 given cells, with a : between the cell references
ENGG1801 Engineering Computing Jason Chan 22

Different types of functions


When you click on the formula bar (top-left of picture), Excel highlights the cells that are in the range you are using
In this picture, the cells B1:B3 are highlighted with a blue border

ENGG1801 Engineering Computing Jason Chan

23

Examples of functions
AVERAGE COUNT COUNTA SUMIF COUNTIF
Finds the average of values in a range of cells Counts how many cells contain numbers in a range of cells Counts how many non-empty cells are in a range of cells Adds up only those values in a range of cells that meet some condition Counts only those values in a range of cells that meet some condition
ENGG1801 Engineering Computing Jason Chan 24

Practical Example
Q) Find the average mark of students who passed a course We use the SUMIF function like this: =SUMIF(B2:B7, ">=50") This looks at numbers between and including cells B2 and B7, and adds up the numbers greater than or equal to 50 We also use the COUNTIF function
ENGG1801 Engineering Computing Jason Chan 25

Practical Example
E1: =SUMIF(B2:B7, ">=50") E2: =COUNTIF(B2:B7, ">=50") E4: =E1/E2

ENGG1801 Engineering Computing Jason Chan

26

Exercise
Q) A factory makes items which are tested before being sold. There are 2 criteria that must be met:
P > 1.25 Q < 0.5

What formulas go in D8 (copied down to D17), D18 and D19?


ENGG1801 Engineering Computing Jason Chan 27

Solution
D8: D18: D19:
=IF(AND(B8>C$4, C8<C$5), "Pass", "Fail") =COUNTIF(D8:D17, "Pass") =D18/COUNTA(D8:D17)

Cell D19 needs to be formatted to be a percentage:


Right click on cell D19 Select Format Cells On the Number tab, select Percentage and choose the number of decimal places
ENGG1801 Engineering Computing Jason Chan 28

Modeling
Modeling is predicting the behaviour of real systems
Model how a telescope will work before spending $1 billion to build it Model how a bridge will behave under different weather conditions Model how the human brain works to create artificial hearing devices
ENGG1801 Engineering Computing Jason Chan 29

Why we need modeling


Consider the following data sets:
Set 1 x 10.0 8.0 13.0 9.0 11.0 14.0 6.0 4.0 12.0 7.0 5.0 y 8.04 6.95 7.58 8.81 8.33 9.96 7.24 4.26 10.84 4.82 5.68 x 10.0 8.0 13.0 9.0 11.0 14.0 6.0 4.0 12.0 7.0 5.0 Set 2 y 9.14 8.14 8.74 8.77 9.26 8.10 6.13 3.10 9.13 7.26 4.74 x 10.0 8.0 13.0 9.0 11.0 14.0 6.0 4.0 12.0 7.0 5.0 Set 3 y 7.46 6.77 12.74 7.11 7.81 8.84 6.08 5.39 8.15 6.42 5.73 x 8.0 8.0 8.0 8.0 8.0 8.0 8.0 19.0 8.0 8.0 8.0 Set 4 Y 6.58 5.76 7.71 8.84 8.47 7.04 5.25 12.50 5.56 7.91 6.89 30

ENGG1801 Engineering Computing Jason Chan

Why we need modeling


Statistically, these data sets are very similar! They each have:
N = 11 Mean of xs = 9.0 Mean of ys = 7.5 Equation of standard regression line y = 3 + 0.5x Standard error of slope estimate = 0.118 t = 4.24 Sum of squares = 110.0 Regression of sum of squares = 27.50 Residual sum of squares of y = 13.75 Correlation coefficient = 0.82 r2 = 0.67
ENGG1801 Engineering Computing Jason Chan 31

Graphing reveals the differences

ENGG1801 Engineering Computing Jason Chan

32

Reasons for Graphing


Engineers need to graph:
To explore the data ourselves To collaborate with colleagues To display in scientific / technical reports and presentations To sell an idea (e.g. to a client) To explain to a non-engineer (e.g. a manager)

Your graph should show the data clearly without distorting it


ENGG1801 Engineering Computing Jason Chan 33

Practical Example
Q) You have measured the resistance of a sample of material at various temperatures and want to know its thermal coefficient of resistance

ENGG1801 Engineering Computing Jason Chan

34

Scatter vs Line Plots


Scatter plots and line plots look very similar
Both have points with optional lines

But for engineering, you generally want to use scatter plots, not line plots
Scatter plots treat x-values as numbers (e.g. x-y plane) Line plots treat x-values as categories (e.g. populations of countries)
ENGG1801 Engineering Computing Jason Chan 35

Scatter vs Line Plots

ENGG1801 Engineering Computing Jason Chan

36

Practical Example
Q) You are investigating the synchronization of your equipment a thermometer and a light meter
Both have measured data during overlapping time intervals But the two instruments measure different things

We can plot both datasets on the same graph, with two y-axes
ENGG1801 Engineering Computing Jason Chan 37

Practical Example

ENGG1801 Engineering Computing Jason Chan

38

How to Plot your Data


Step 1) Highlight x and y-values of one dataset, then on the Insert tab, select Scatter, then the scatter chart that you want

ENGG1801 Engineering Computing Jason Chan

39

How to Plot your Data


Step 2) To add another series, right-click on the middle of the chart, and click Select Data

ENGG1801 Engineering Computing Jason Chan

40

How to Plot your Data


Step 3) Click Add, then select the x and y values by using the button, OK, OK

ENGG1801 Engineering Computing Jason Chan

41

How to Plot your Data


Step 4) Click on the curve for one of the data series, then right click and select Format Data Series

ENGG1801 Engineering Computing Jason Chan

42

How to Plot your Data


Step 5) Select Secondary Axis, then Close

ENGG1801 Engineering Computing Jason Chan

43

How to Plot your Data


Step 6) Now add x and y-axis titles, gridlines. First click the graph, then use the buttons under the Layout tab

ENGG1801 Engineering Computing Jason Chan

44

How to Plot your Data


Step 7) You can also change the axes minimum and maximum values, etc. by right-clicking on the axis values, then selecting Format Axis

ENGG1801 Engineering Computing Jason Chan

45

Missing Values
The light meter is faulty and so a data value is missing

ENGG1801 Engineering Computing Jason Chan

46

Missing Values
Enter =NA() into the cell, and Excel will connect the two adjacent points

ENGG1801 Engineering Computing Jason Chan

47

Error Bars
Error bars show how much we can trust that measurement

ENGG1801 Engineering Computing Jason Chan

48

Error Bars
Click on the graph, then on the Layout tab, select Error Bars, then the option you want If you only want vertical error bars, click on a horizontal bar then press delete on keyboard
ENGG1801 Engineering Computing Jason Chan 49

Add a Plot as a New Page


You can make your plot occupy an entire page of your spreadsheet

ENGG1801 Engineering Computing Jason Chan

50

Add a Plot as a New Page


Click on the chart, then on the Design tab, click Move Chart, then select New sheet

ENGG1801 Engineering Computing Jason Chan

51

Regression Analysis
Regression Analysis: Fitting a line that characterizes your experimental data A common algorithm is Least Squares Fitting
Line of best fit is y = mx + b Minimize the sum of residuals

Excel allows you to do this using Trendline


ENGG1801 Engineering Computing Jason Chan 52

Practical Example
The resistance at temperature t is:

Rt R0 1 t
where is the coefficient of resistance. Lets rewrite this in the form of a straight line y = mx + b

Rt R0 t R0
We can use regression analysis to find the slope (R0) and intercept (R0) to find
ENGG1801 Engineering Computing Jason Chan 53

A linear trend fit with Trendline


Click on a data point, then on the Layout tab, click Trendline then select the trendline that you want

ENGG1801 Engineering Computing Jason Chan

54

A linear trend fit with Trendline


To show the equation of the trendline, right click on the trendline and select Format Trendline

ENGG1801 Engineering Computing Jason Chan

55

A linear trend fit with Trendline


then select Display Equation on chart

ENGG1801 Engineering Computing Jason Chan

56

A linear trend fit with Trendline

ENGG1801 Engineering Computing Jason Chan

57

Lab Exercise
Q) Predict the height of a balls bounce when dropped from a height of 20cm onto a flat surface We use the equation:

hn khn 1
where hi is the height on the ith bounce and k is a constant (k = 1 for elastic collisions, k < 1 for inelastic collisions)
ENGG1801 Engineering Computing Jason Chan 58

Summary
Functions
Function inputs / arguments / parameters Logical functions Functions that take a range of cells

Plotting
Trendlines

We now understand more complicated functions and can plot data using Excel
ENGG1801 Engineering Computing Jason Chan 59

You might also like