Professional Documents
Culture Documents
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
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
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)
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
(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
A
true false
NOT(A)
false true
15
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
Logical functions
There are many ways to write the solution E.g. These 2 are equivalent:
temp>50 NOT(temp<=50)
Keep it simple!
Generally speaking, you should use expressions that are simplest to understand
These are not always the shortest
18
19
20
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
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
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
Solution
D8: D18: D19:
=IF(AND(B8>C$4, C8<C$5), "Pass", "Fail") =COUNTIF(D8:D17, "Pass") =D18/COUNTA(D8:D17)
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
32
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
34
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
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
38
39
40
41
42
43
44
45
Missing Values
The light meter is faulty and so a data value is missing
46
Missing Values
Enter =NA() into the cell, and Excel will connect the two adjacent points
47
Error Bars
Error bars show how much we can trust that measurement
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
50
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
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
54
55
56
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