Download as pdf or txt
Download as pdf or txt
You are on page 1of 53

ENGG1801

Engineering Computing

Lecture 1-2
Excel Basics
Semester 1, 2017
School of Information Technologies
The University of Sydney, Australia
sydney.edu.au/engineering/it/courses/engg1801
Jason Chan
j.chan@sydney.edu.au
ENGG1801 Engineering Computing 1
Jason Chan
Semester 1, 2017 Schedule
Week Date Lecture topics Notice
1 6 – 10 Mar Introduction, Excel basics
Part 1 –
Excel

2 13 – 17 Mar Functions, Plots, Solving equations, File I/O


3 20 – 24 Mar Matrix algebra Lab Exam 1 (5%), No Wednesday lecture
4 27 – 31 Mar Matlab basics, If statements, Arrays
5 3 – 7 Apr Loops
Matlab Basics
Part 2 –

6 No Friday labs (Good Friday holiday),


10 – 14 Apr Functions
Extra “Lab 6’s” on Tuesday - Wednesday
Mid-Semester Break
No Tuesday lecture (ANZAC Day holiday),
7 24 – 28 Apr Functions
Extra “Lab 6’s” on Monday - Wednesday
8 1 – 5 May Character strings, Text & File I/O Lab Exam 2 (20%)
Matlab Applications

9 8 – 12 May 2-D and 3-D plotting, Surface plots


Part 3 –

10 15 – 19 May Matrix algebra


11 22 – 26 May Images, Movies
12 29 May – 2 Jun Interpolation and curve fitting No Tuesday lecture
13 5 – 9 Jun Help for the Final Exam Lab Exam 3 (20%), No Tuesday lecture
Student vacation (stuvac)
Exam 19 – 30 Jun --- Final Exam (50%)

ENGG1801 Engineering Computing 2


Jason Chan
What will I learn today?
• Microsoft Excel is the most commonly
used spreadsheet
• We will be using Microsoft Excel 2013

• Excel Basics
– How to use Excel to do the simplest things
– How to solve some substantial problems
using Excel

ENGG1801 Engineering Computing 3


Jason Chan
Microsoft Excel
• You’ve probably used spreadsheets like
Excel before
• In ENGG1801, you will become much
more powerful by learning extra features
– Define and solve equations / functions
– Curve fitting
– Write basic code to perform complex
calculations

ENGG1801 Engineering Computing 4


Jason Chan
Spreadsheets
• Before electronic spreadsheets,
spreadsheets were on actual sheets of
paper
• The 1st electronic visual spreadsheet for
PC’s contained up to 5 columns and 20
rows of data

ENGG1801 Engineering Computing 5


Jason Chan
VisiCalc (1st visual spreadsheet)

http://www.bricklin.com

ENGG1801 Engineering Computing 6


Jason Chan
Advertisement for VisiCalc

http://www.bricklin.com

ENGG1801 Engineering Computing 7


Jason Chan
Excel is powerful
• Calculators typically handle up to ±1099
• Excel can handle up to 9.9999×10307

ENGG1801 Engineering Computing 8


Jason Chan
Starting Excel
• Click (in order):

ENGG1801 Engineering Computing 9


Jason Chan
Formatting
• You can change the appearance of cells,
making your results clearer
– Cell color
– Text color
– Cell size
– Cell borders

ENGG1801 Engineering Computing 10


Jason Chan
Precision
• Formatting can affect the apparent
precision (appearance) of numbers
– Numbers stored with 15 digit precision
– Number of digits displayed
depends on cell size
and format
– Too many digits to fit in
a cell will show ####

ENGG1801 Engineering Computing 11


Jason Chan
Formats for engineering
• There are many standard formats for
numbers in Excel
• The speed of light is c = 299 792 458 m/s
Format in Excel Represented number
A number 299 792 458 299 792 458
Scientific Notation 3.00E+08 3.00 x 108
Engineering Notation 299.8E+06 299.8 x 106

• In Engineering notation, the power of 10 is


a multiple of 3
ENGG1801 Engineering Computing 12
Jason Chan
Formulas
• A formula is an expression in which Excel
will perform calculations / operations
• Formulas start with the = sign
• Without the =
sign, we just
have a value

• Type into cell C1: =(A1+B1)*2


• Type into cell D3: =1/(C1-C3)
ENGG1801 Engineering Computing 13
Jason Chan
Copying to other cells
• Formulas can also be copied to other cells

• In this example, we have typed a formula


into cell C2: =A2+B2

ENGG1801 Engineering Computing 14


Jason Chan
Copying to other cells
• We want to continue finding the sums on
rows 3-5
• Click on cell C2, then click on the little
black square on
the bottom right
corner of the
cell, then drag
down to cell C5

ENGG1801 Engineering Computing 15


Jason Chan
Copying to other cells
• The formula in C2 (=A2+B2) will be
“copied” to cells C3 to C5
• In C3, the formula will become =A3+B3
etc…

ENGG1801 Engineering Computing 16


Jason Chan
Copying to other cells
• The formula was not exactly copied, but
changed depending on where we copied it
to – this is called relative referencing

• C2: =A2+B2
• C3: =A3+B3
• C4: =A4+B4
• C5: =A5+B5
ENGG1801 Engineering Computing 17
Jason Chan
Copying to other cells
• Suppose we always want to use the same
cell A2 in different places (C2 to C5)
• So the correct formulas should be:

• C2: =A2+B2
• C3: =A2+B3
• C4: =A2+B4
• C5: =A2+B5
ENGG1801 Engineering Computing 18
Jason Chan
Copying to other cells
• But if we use relative referencing, then we
will get wrong results:

• C2: =A2+B2
• C3: =A3+B3
• C4: =A4+B4
• C5: =A5+B5
ENGG1801 Engineering Computing 19
Jason Chan
Copying to other cells
• We want row 2 of cell A2 in the formula
=A2+B2 to remain constant when we copy
the formula
• We put a $ sign
in front of the
row number so
that it doesn’t
change
• C2: =A$2+B2
ENGG1801 Engineering Computing 20
Jason Chan
Copying to other cells
• Now, when we copy the formula to other
cells, the 2 in A2 will not change
• This is called absolute referencing

• C2: =A$2+B2
• C3: =A$2+B3
• C4: =A$2+B4
• C5: =A$2+B5
ENGG1801 Engineering Computing 21
Jason Chan
Copying to other cells
• We can use a combination of relative and
absolute referencing
Cell What happens to cell
reference reference when copied
A2 Both row and column may change
A$2 Row 2 remains constant
$A2 Column A remains constant
$A$2 Row 2 and column A both remain constant

ENGG1801 Engineering Computing 22


Jason Chan
Variables
• We can give names to cells (at top left of
window) to make them easier to refer to
• These are called
variables
• Warning: Some
variable names are
not allowed, e.g.
cell references,
“r”, “c”, etc.
ENGG1801 Engineering Computing 23
Jason Chan
Variables
• To change or delete variable names
– Click on the
Formulas tab
(top of page)
– Click
Name Manager
– Select the variable
you want, then
click Edit or Delete

ENGG1801 Engineering Computing 24


Jason Chan
Practical Example
• Q) A ball is dropped off a cliff. Find the
distance travelled in 10 seconds.
• We use the
equation:
1 2
s  ut  at
2

ENGG1801 Engineering Computing 25


Jason Chan
Practical Example

1 2
s  ut  at
2

• Type into B4: =B1*B3 + 1/2*B2*B3^2

ENGG1801 Engineering Computing 26


Jason Chan
Practical Example

1 2
s  ut  at
2

• Or we can give variable names to cells B1


to B3 to make them much easier to use
• Then type into B4: =u*t + 1/2*a*t^2

ENGG1801 Engineering Computing 27


Jason Chan
Refer to cells, don’t use raw values
• It would be incorrect to type into B4:
=0*10 + 1/2*9.8*10^2
• This gives the correct answer, but it is the
incorrect technique!
– If the value of a variable changes (e.g. initial
velocity, u), you now have to change many
places in your code, instead of just a cell
– It is unclear what the numbers represent,
because they are not stored in cells
ENGG1801 Engineering Computing 28
Jason Chan
Refer to cells, don’t use raw values
• You should refer to (use) cells in formulas
• Do not use raw values in formulas
• Instead, only the raw value goes into a cell,
then you refer to that cell in your formula

• “Just because you got the right answer,


doesn’t mean that you did it correctly”

ENGG1801 Engineering Computing 29


Jason Chan
Variables
• Make formulas much easier to read
=B1*B3 + 1/2*B2*B3^2
=u*t + 1/2*a*t^2
• Reduce chance of making mistakes
– Absolute referencing if copied to other cells
• Save time
• No need to keep searching for the cell
reference
ENGG1801 Engineering Computing 30
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 31
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 32


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

• By default, Excel deals with angles in


radians (not degrees)
• Note that we don’t need to know
how the function does its job,
we only need to know what it does
ENGG1801 Engineering Computing 33
Jason Chan
Practical Example
• Q) Find the roots of the quadratic equation
x  5x  6  0
2

ENGG1801 Engineering Computing 34


Jason Chan
Practical Example
• For any quadratic equation of the form:
ax  bx  c  0
2

we can use the quadratic formula:


b  b 2  4ac
x
2a
where b 2
 4ac is called the discriminant
whose value determines the number of
real roots
ENGG1801 Engineering Computing 35
Jason Chan
Practical Example

• First, assign your variables


• Let cell A3 be called “a”, B3 be “b”, and
C3 be “_c” (“c” cannot be used, slide 23)
ENGG1801 Engineering Computing 36
Jason Chan
Practical Example

• Then type into E3: =b^2 – 4*a*_c


• Assign cell E3 the variable “disc”

ENGG1801 Engineering Computing 37


Jason Chan
Practical Example

• B5: =(-b + SQRT(disc)) / (2*a)


• D5: =(-b – SQRT(disc)) / (2*a)

ENGG1801 Engineering Computing 38


Jason Chan
Another Practical Example
• Q) Let’s try a different quadratic equation:
x  2x  6  0
2

• Now we have
a problem
when there
aren’t 2 roots
(i.e.
discriminant ≤ 0)
ENGG1801 Engineering Computing 39
Jason Chan
Another Practical Example
• We want to do different things depending
on different conditions
– If there are 2 roots, we want one of them
displayed in D5
– But if there is
1 root or no
roots, we
want nothing
displayed in
D5
ENGG1801 Engineering Computing 40
Jason Chan
IF functions
• So we use an IF function:
=IF(disc>0, (-b–SQRT(disc))/(2*a), "")

• This means:
– If disc>0, then use (-b–SQRT(disc))/(2*a)
– If not disc>0, then use "" (nothing between
quotes means display nothing)
ENGG1801 Engineering Computing 41
Jason Chan
IF functions
• In our previous example, we no longer get
the #NUM! error when there are no roots
– disc = -20 ≤ 0, so "" (blank) is displayed

ENGG1801 Engineering Computing 42


Jason Chan
IF functions
• If we go back to our first example and
change b back to 5, the root displayed
– disc = 1 > 0, so (-b - SQRT(disc)) / (2*a)
is calculated

ENGG1801 Engineering Computing 43


Jason Chan
IF functions
• We can use IF functions to make our
spreadsheet handle all cases:
– E.g. If there isn’t a 2nd root, then we should
not even display “Root 2” in cell C5, etc.

Situation What to display


disc > 0 2 roots
disc = 0 1 root
disc < 0 0 roots
ENGG1801 Engineering Computing 44
Jason Chan
IF functions
• D5: =IF(disc>0, (-b - SQRT(disc)) / (2*a), "")

• C5: =IF(disc>0, "Root 2", "")

• B5: =IF(disc>=0, (-b + SQRT(disc)) / (2*a), "")

• A5: =IF(disc>0, "Root 1", IF(disc=0, "Double root", ""))

ENGG1801 Engineering Computing 45


Jason Chan
IF functions
• So now, if there is only 1 root, we display:
– “Double root” (since there is only 1 root) in A5
– The root value in B5
– Nothing in cells C5 and D5

ENGG1801 Engineering Computing 46


Jason Chan
IF functions
• If there are no roots, we display:
– Nothing in cells A5 and D5

ENGG1801 Engineering Computing 47


Jason Chan
IF functions
• If there are 2 roots, we display:
– “Root 1” in A5
– The first root value in B5
– “Root 2” in C5
– The first root value in B5

ENGG1801 Engineering Computing 48


Jason Chan
Exercise
• Q) In cell A6, type “Number of real roots”.
What formula should you type into cell D6
to display number of real roots?

ENGG1801 Engineering Computing 49


Jason Chan
Error values

Displayed
Meaning
value
#DIV/0 Division by 0
#NAME? Undefined variable / function name
#N/A No value available
#NULL! A result has no value
#NUM! Numerical error (e.g. SQRT(-1))
#REF! Invalid cell reference
#VALUE! Invalid input type (e.g. SQRT("abc"))

ENGG1801 Engineering Computing 50


Jason Chan
Error values
• A circular reference occurs when a
formula refers to itself
(directly or indirectly), e.g.
– Direct example:
• A1: = A1+6
– Indirect example:
• A2: = A3+7
• A3: = A2*2

ENGG1801 Engineering Computing 51


Jason Chan
To Know
• How to use formulas
• Difference between relative and absolute
referencing, and when to use which one
• Why you should use cell references and
not raw values in formulas
• Variables, and why we use them
• Functions
– Especially the IF function
ENGG1801 Engineering Computing 52
Jason Chan
To Do
• If you have not yet done so, read through
yesterday’s lecture (Lecture 1-1)
• See course website:
sydney.edu.au/engineering/it/courses/engg1801
• Install Matlab on your computer
– Lecture 1-1, slides 64 – 65
• Attend lab on time; do not wait outside
room, just log in using unikey and
password, then open course website
ENGG1801 Engineering Computing 53
Jason Chan

You might also like