0% found this document useful (0 votes)
2 views79 pages

Class02 Basic Calculations STUDENTS 2020

The document outlines the second class session of EECS 1011, focusing on basic calculations and variables using MATLAB. It introduces MATLAB as a calculator, covering arithmetic operations, the significance of result accuracy, and the use of built-in functions for trigonometry. Additionally, it emphasizes the importance of critical examination of results and the use of variables for storing computation outcomes.

Uploaded by

tatopla20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views79 pages

Class02 Basic Calculations STUDENTS 2020

The document outlines the second class session of EECS 1011, focusing on basic calculations and variables using MATLAB. It introduces MATLAB as a calculator, covering arithmetic operations, the significance of result accuracy, and the use of built-in functions for trigonometry. Additionally, it emphasizes the importance of critical examination of results and the use of variables for storing computation outcomes.

Uploaded by

tatopla20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 79

Basic Calculations

& 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

2 Copyright James Andrew Smith & others. 2020


Not for distribution outside York
University.
A Reminder: What is EECS 1011?
} Computational Thinking Through Mechatronics
This course provides a first exposure to programming; teaches a set of
soft computing skills; and demonstrates how computational thinking is
used to solve problems in engineering. It uses an interactive computing
environment to expose the underlying concepts and experiential labs to
implement them. Two lecture hours and three lab hours weekly.

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

} But we actually computed

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

} this is a very common error


} if the denominator contains a sum, difference, product, or
quotient, it probably needs to be enclosed by parentheses

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.

that seems more sensible

Copyright James Andrew Smith & others. Not for distribution outside York University.
30 2019
Try again.

Not right.

that seems more sensible

Copyright James Andrew Smith & others. Not for distribution outside York University.
31 2019
Try again.

that seems more sensible

Copyright James Andrew Smith & others. Not for distribution outside York University.
32 2019
Try again.

that seems more sensible

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

Do it wrong. Understand result.


Learn through failure.
Copyright James Andrew Smith & others. Not for distribution outside York University.
37 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

Do it wrong. Understand result.


Learn through failure.
38
This is the natural way to learn.
Copyright James Andrew Smith & others. Not for distribution outside York University.
2019
Basic Calculations
& Variables
EECS 1011 Class Session #2
James Andrew Smith, PhD, PEng
Copyright James Andrew Smith & others. Not for distribution outside York University.
39 2019
Part 2: Variables & Built-in Functions

40 Copyright James Andrew Smith & others. 2020


Not for distribution outside York
University.
Example 3: Trigonometry
} The free-body diagram for an object having mass 1.1 kg
is shown below. Compute the net horizontal and
vertical force acting on the object.

𝑓 = 10𝑁

30°

𝑔
Copyright James Andrew Smith & others. Not for distribution outside York University.
41 2019
Example 3: Trigonometry Fill in here

} The standard solution is to find the components of the


vector 𝑓

𝑓 = 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

} a function provides the MATLAB programmer a way to


perform a well-defined task using a well-defined
interface

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

} a function provides the MATLAB programmer a way to


perform a well-defined task using a well-defined
interface
} interface
} function name
} inputs to the function
} outputs of the function

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

where function-name is the name of the function

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

} Back to the trig problem:

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

This doesn’t look 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

} except for trivial calculations, you will almost always


want to store the result of a computation
} a variable is a name given to a stored value; the
statement:
Note: The statement
z = 1 + 2
causes the following to occur: is an error in MATLAB

} compute the value 1 + 2


} store the result in the variable named z

Copyright James Andrew Smith & others. Not for distribution outside York University.
58 2019
Variables Fill in here

} except for trivial calculations, you will almost always


want to store the result of a computation
} a variable is a name given to a stored value; the
statement:
Note: The statement
z = 1 + 2
causes the following to occur: is an error in MATLAB

} compute the value 1 + 2


} store the result in the variable named z
} MATLAB automatically creates z if it does not already
exist

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

} the = operator is the assignment operator


} the statement: Note: The statement

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

} the = operator is the assignment operator


} the statement: Note: The statement

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 =

} the word “store” means store inside computer memory

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;

Address Variable name Value


1 z 3
2
3
4
5
6
...

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

valid variable invalid variable reason invalid


names names
x $ • does not begin with a letter
• $ is not allowed in variable names
x6 6x • does not begin with a letter
lastValue if • if is a keyword
pi_over_2 pi/2 • / is not allowed in 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

} exceptions to the rule:


} if you are solving a problem that contains variable names, you
should try to use the same names, e.g., in physics the following
would likely be common:

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

thetaRad instead of thetarad

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

thetaRad instead of thetarad

} avoid long 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

} remember that the statement:

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

} what is the result of the following assignment


statements?

} is the value of z equal to 3 or 4?


} draw the memory diagram for each step

Copyright James Andrew Smith & others. Not for distribution outside York University.
75 2019
z = 1 + 2;

Address Variable name Value


1 z 3
2
3
4
5
6
...

Copyright James Andrew Smith & others. Not for distribution outside York University.
76 2019
y = z;

Address Variable name Value


1 z 3
2 y 3
3
4
5
6
...

Copyright James Andrew Smith & others. Not for distribution outside York University.
77 2019
y = 4;

Address Variable name Value


1 z 3
2 y 4
3
4
5
6
...

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 =

} it does not mean that y and z are the same variable

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

You might also like