Class02 Basic Calculations STUDENTS 2020
Class02 Basic Calculations STUDENTS 2020
& Variables
EECS 1011 Class Session #2
James Andrew Smith, PhD, PEng
Copyright James Andrew Smith & others. Not for distribution outside York University.
1 2019
Part 1: Matlab as a calculator
Copyright James Andrew Smith & others. Not for distribution outside York University.
3 2019
Reminder
} Do your Lab A (WHMIS Lab Safety) online
} Submit result to Moodle before Lab B
} Lab B
} Intro to MATLAB.
} Simple, turn on computer, try it out
} TA’s will answer questions, debug, help out.
} Instructions will be posted on eClass.
Copyright James Andrew Smith & others. Not for distribution outside York University.
4 2019
https://twitter.com/celestiallight_/status/1156296200586289153
6 Copyright James Andrew Smith & others. Not for distribution 2019
outside York University.
MATLAB as a Calculator: Arithmetic.
} compute the circumference and area of a circle with
radius 2.5
Copyright James Andrew Smith & others. Not for distribution outside York University.
7 2019
MATLAB as a Calculator: Arithmetic.
} Calculate
} The circumference of a circle
} The area of a circle
} Assuming a radius of 2.5
} (ignore units for now)
Copyright James Andrew Smith & others. Not for distribution outside York University.
8 2019
MATLAB as a Calculator: Arithmetic.
} Calculate
} The circumference of a circle
} The area of a circle
} Assuming a radius of 2.5
} (ignore units for now)
Copyright James Andrew Smith & others. Not for distribution outside York University.
9 Images: http://www.onetick.com/web/images/logo/matlab.pnghttps://upload.wikimedia.org/wikipedia/commons/thumb/c/c1/Computer-
2019
aj_aj_ashton_01.svg/2000px-Computer-aj_aj_ashton_01.svg.png;
why only 4 digits after the decimal?
Copyright James Andrew Smith & others. Not for distribution outside York University.
10 2019
why only 4 digits after the decimal?
Copyright James Andrew Smith & others. Not for distribution outside York University.
11 2019
why only 4 digits after the decimal?
Copyright James Andrew Smith & others. Not for distribution outside York University.
12 2019
why only 4 digits after the decimal?
Copyright James Andrew Smith & others. Not for distribution outside York University.
13 2019
why only 4 digits after the decimal?
Copyright James Andrew Smith & others. Not for distribution outside York University.
14 2019
MATLAB as a Calculator: Arithmetic.
} MATLAB computes values using 15-17 significant digits
} by default, MATLAB displays values between -1000
and 1000 using 4 digits after the decimal place
} for values outside this range MATLAB will use scientific
notation
} you can change this using the format command
Copyright James Andrew Smith & others. Not for distribution outside York University.
15 2019
format longg
Copyright James Andrew Smith & others. Not for distribution outside York University.
16 2019
format longg
Copyright James Andrew Smith & others. Not for distribution outside York University.
17 2019
format longg
Copyright James Andrew Smith & others. Not for distribution outside York University.
18 2019
format longg
Copyright James Andrew Smith & others. Not for distribution outside York University.
19 2019
format longg
Copyright James Andrew Smith & others. Not for distribution outside York University.
20 2019
Default versus format longg
Copyright James Andrew Smith & others. Not for distribution outside York University.
21 2019
Example 2
} A ball at rest is dropped from the top of a building.
How far has the ball travelled when it reaches a
velocity of 10 m/s (ignoring the effects of drag)?
𝑣!
distance =
2𝑔
Copyright James Andrew Smith & others. Not for distribution outside York University.
22 2019
that can't be right
Copyright James Andrew Smith & others. Not for distribution outside York University.
23 2019
that can't be right
Copyright James Andrew Smith & others. Not for distribution outside York University.
24 2019
that can't be right
Is this right?
Copyright James Andrew Smith & others. Not for distribution outside York University.
25 2019
MATLAB as a Calculator: Arithmetic.
} our previous attempt contains a “precedence error”
} We wanted to compute
Copyright James Andrew Smith & others. Not for distribution outside York University.
26 2019
MATLAB as a Calculator: Arithmetic.
} our previous attempt contains a “precedence error”
} We wanted to compute
Copyright James Andrew Smith & others. Not for distribution outside York University.
27 2019
MATLAB as a Calculator: Arithmetic.
} our previous attempt contains a “precedence error”
} vs vs
Copyright James Andrew Smith & others. Not for distribution outside York University.
28 2019
Remember?
https://twitter.com/celestiallight_/status/1156296200586289153
29 Copyright James Andrew Smith & others. Not for distribution 2019
outside York University.
Try again.
Copyright James Andrew Smith & others. Not for distribution outside York University.
30 2019
Try again.
Not right.
Copyright James Andrew Smith & others. Not for distribution outside York University.
31 2019
Try again.
Copyright James Andrew Smith & others. Not for distribution outside York University.
32 2019
Try again.
Copyright James Andrew Smith & others. Not for distribution outside York University.
33 2019
MATLAB as a Calculator: Arithmetic.
} when computing anything you should always examine
the result critically
Copyright James Andrew Smith & others. Not for distribution outside York University.
34 2019
MATLAB as a Calculator: Arithmetic.
} when computing anything you should always examine
the result critically
} whenever you get a wrong answer, it is probably not
MATLAB's fault
Copyright James Andrew Smith & others. Not for distribution outside York University.
35 2019
MATLAB as a Calculator: Arithmetic.
} when computing anything you should always examine
the result critically
} whenever you get a wrong answer, it is probably not
MATLAB's fault
} in this case, MATLAB computed exactly what we asked
it to
} we just happened to ask MATLAB to compute the wrong
expression
Copyright James Andrew Smith & others. Not for distribution outside York University.
36 2019
MATLAB as a Calculator: Arithmetic.
} when computing anything you should always examine
the result critically
} whenever you get a wrong answer, it is probably not
MATLAB's fault
} in this case, MATLAB computed exactly what we asked
it to
} we just happened to ask MATLAB to compute the wrong
expression
𝑓 = 10𝑁
30°
𝑔
Copyright James Andrew Smith & others. Not for distribution outside York University.
41 2019
Example 3: Trigonometry Fill in here
𝑓 = 10𝑁
𝑓# =Fill10 sin 30°
in here
30°
𝑓"Fill=in 10
here
cos 30°
𝑔
Copyright James Andrew Smith & others. Not for distribution outside York University.
42 2019
Functions
} MATLAB provides functions named sin and cos to
compute the sine and cosine of an angle
Copyright James Andrew Smith & others. Not for distribution outside York University.
43 2019
Functions
} MATLAB provides functions named sin and cos to
compute the sine and cosine of an angle
Copyright James Andrew Smith & others. Not for distribution outside York University.
44 2019
Functions
} MATLAB provides functions named sin and cos to
compute the sine and cosine of an angle
Copyright James Andrew Smith & others. Not for distribution outside York University.
45 2019
Functions
} the function interface is documented through a built-
in help mechanism
Copyright James Andrew Smith & others. Not for distribution outside York University.
46 2019
Image: https://upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Help!_logo.svg/2000px-Help!_logo.svg.png
Functions
} the function interface is documented through a built-
in help mechanism
} to use the built-in help type
help function-name
or
doc function-name
Copyright James Andrew Smith & others. Not for distribution outside York University.
47 2019
Image: https://upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Help!_logo.svg/2000px-Help!_logo.svg.png
Copyright James Andrew Smith & others. Not for distribution outside York University.
48 2019
Copyright James Andrew Smith & others. Not for distribution outside York University.
49 2019
Functions
} the input to the sin function is the angle
} the input goes inside parentheses after the function name
} the output is the sine of the angle
} which is just a value
Copyright James Andrew Smith & others. Not for distribution outside York University.
50 2019
Functions
} the input to the sin function is the angle
} the input goes inside parentheses after the function name
} the output is the sine of the angle
} which is just a value
Copyright James Andrew Smith & others. Not for distribution outside York University.
51 2019
that can't be right
Copyright James Andrew Smith & others. Not for distribution outside York University.
52 2019
that can't be right
Copyright James Andrew Smith & others. Not for distribution outside York University.
53 2019
Hint is in the help documentation
Copyright James Andrew Smith & others. Not for distribution outside York University.
54 2019
Try again.
Copyright James Andrew Smith & others. Not for distribution outside York University.
55 2019
Elementary mathematical functions
} MATLAB has many elementary mathematical
functions for trigonometry, exponents and logarithms,
and rounding
} to see the complete list type
help elfun
or
doc elfun
Copyright James Andrew Smith & others. Not for distribution outside York University.
56 2019
Variables
} except for trivial calculations, you will almost always
want to store the result of a computation
Copyright James Andrew Smith & others. Not for distribution outside York University.
57 2019
Variables Fill in here
Copyright James Andrew Smith & others. Not for distribution outside York University.
58 2019
Variables Fill in here
Copyright James Andrew Smith & others. Not for distribution outside York University.
59 2019
Variables
} the = operator is the assignment operator
Copyright James Andrew Smith & others. Not for distribution outside York University.
60 2019
Variables Fill in here
z = 1 + 2
is an error in MATLAB
means:
} evaluate the expression on the right-hand side of =
} store the result in the variable on the left-hand size of =
Copyright James Andrew Smith & others. Not for distribution outside York University.
61 2019
Variables Fill in here
z = 1 + 2
is an error in MATLAB
means:
} evaluate the expression on the right-hand side of =
} store the result in the variable on the left-hand size of =
Copyright James Andrew Smith & others. Not for distribution outside York University.
62 2019
A simple memory model
} a memory model is useful for understanding some
important programming concepts
Copyright James Andrew Smith & others. Not for distribution outside York University.
63 2019
A simple memory model
} a memory model is useful for understanding some
important programming concepts
} our model is very simple:
} a table with 3 columns
1. an address
¨ starts at 1 and counts up by 1 for each row of the table
2. the variable name
3. the value stored
Copyright James Andrew Smith & others. Not for distribution outside York University.
64 2019
z = 1 + 2;
Copyright James Andrew Smith & others. Not for distribution outside York University.
65 2019
Variable names
} a variable name must start with a letter
Copyright James Andrew Smith & others. Not for distribution outside York University.
66 2019
Variable names
} a variable name must start with a letter
} the rest of the name can include letters, digits, or
underscores
Copyright James Andrew Smith & others. Not for distribution outside York University.
67 2019
Variable names
} a variable name must start with a letter
} the rest of the name can include letters, digits, or
underscores
} names are case sensitive, so A and a are two different
variables
Copyright James Andrew Smith & others. Not for distribution outside York University.
68 2019
Variable names
} a variable name must start with a letter
} the rest of the name can include letters, digits, or
underscores
} names are case sensitive, so A and a are two different
variables
} MATLAB has some reserved words called keywords
that cannot be used as variable names
} use the command iskeyword to get a list of keywords
Copyright James Andrew Smith & others. Not for distribution outside York University.
69 2019
Variable names
Copyright James Andrew Smith & others. Not for distribution outside York University.
70 2019
Advice on choosing variable names
} use short, meaningful names
} a name that conveys the purpose of the variable is often
useful for others who need need to read your code, e.g., use
massEarth instead of mE
massSun instead of mS
g, c, v0, h, hBar
Copyright James Andrew Smith & others. Not for distribution outside York University.
71 2019
Advice on choosing variable names
} use lowerCamelCase for most variable names, e.g., use
Copyright James Andrew Smith & others. Not for distribution outside York University.
72 2019
Advice on choosing variable names
} use lowerCamelCase for most variable names, e.g., use
filteredData instead of
measurementsFilteredToRemoveOutliers
Copyright James Andrew Smith & others. Not for distribution outside York University.
73 2019
More on variable assignment Fill in here
means:
1. evaluate the expression on the right-hand side of =
2. store the result in the variable on the left-hand size of =
Copyright James Andrew Smith & others. Not for distribution outside York University.
74 2019
More on variable assignment Fill in here
Copyright James Andrew Smith & others. Not for distribution outside York University.
75 2019
z = 1 + 2;
Copyright James Andrew Smith & others. Not for distribution outside York University.
76 2019
y = z;
Copyright James Andrew Smith & others. Not for distribution outside York University.
77 2019
y = 4;
Copyright James Andrew Smith & others. Not for distribution outside York University.
78 2019
More on variable assignment
} the statement:
y = z;
means:
1. evaluate the expression on the right-hand side of =
2. store the result in the variable on the left-hand size of =
Copyright James Andrew Smith & others. Not for distribution outside York University.
79 2019
To do: Prep for Lab
} Read through Lab B documentation before Lab
} Lab B is this week
} Monday, Tuesday and Thursday
} Check your official York schedule.
} The Zoom link to join the TA will be posted on eClass.
Copyright James Andrew Smith & others. Not for distribution outside York University.
80 2019