1st - Yr - Lecture 02 - NEW
1st - Yr - Lecture 02 - NEW
Lecture 2:
Variables & operators / How to program
Last Lecture
2
Lecture Outline
• What is a variable, why is it important to know
the limitation of variables?
• Using Matlab as a calculator
• Some examples
1. Open Matlab
2. Use Matlab as a calculator
3. Understand the difference between an integer
and a floating point number.
3
An Engineering Problem…
• (June 1996) Ariane
5 was the European
Space Agencies
latest and most
powerful rocket.
• This rocket
exploded resulting
in the loss of $500
million.
• Why?
4
An Engineering Problem…
• A variable on the on-
board computer
stored the rocket
velocity.
Rocket_velocity = 36000
Altitude = 20034
Remaining_fuel = 2321.23
• MATLAB has several different types of
variables, which affect what you can do with
them, and also how much memory they use
on your computer.
6
Using MATLAB as a calculator
As an introduction to MATLAB we will perform
some very simple calculations in the command
window.
>> tape
tape =
3
7
Using MATLAB as a calculator
>> 3 + 22
ans =
25
>> tape + tape
ans =
6
>> bob = tape - tape
bob =
0
>> 3 * 5
ans =
15
>> tape / tape
ans =
1
>> bob = tape * tape / 9
bob =
1
Note operator precedence is left to right
(just like a calculator)
9
Using MATLAB as a calculator
>> 5^3
ans =
Operator Precedence… 125
What would be the answer of the following?
>> 8 * 3 – 6 * 4
0 Or 72
10
Operator Precedence
The answer is:
>> 8 * 3 – 6 * 4
ans = 0
Why?
Matlab has the following operator precedence:
() Brackets
^ Exponent
*, / Multiplication and Division
+, - Addition and Subtraction
BEMDAS
At times you will need to use the brackets to force the
correct equation to be calculated. To get the answer “72” we
would have to write:
>> (8 * 3 – 6) * 4 11
Rules for Variable Names
• Variable names are case sensitive
The variable names: Time, time, TIME and timE are all different
• Variable names can contain up to 63 characters (beyond that
the characters are ignored)
The following variable names would not be allowed: _fill, 3D, 97893475
The following variable names would be allowed: Terminal3, velocity_1
• Punctuation characters are not allowed – Matlab uses these
later when programming – it would get confused if
punctuation was part of the variable name.
– For example:
3.14159265358979323846264338327950288419716939937510582097494
45923078164062862089986280348253421170679
is stored in MATLAB as:
3.14159265358979
14
Variable Types #1 : Floating point numbers
Imagine if you need the number pi to 30
decimal places, MATLAB will normally
store it only up to 14 decimal places.
16
Variable Types #1 : Floating point numbers
• This
>> is fine for small calculations.
example_integer = 9876.0000000000000
• However MATLAB also allows you to store variables
specifically as integers (in order to save computer
memory).
18
Variable Types #2 : Integer variables
• Here are some examples of
storing variables as integers:
>> example_integer = int16(9876)
example_integer =
9876
>>
>> example_integer2 = int16(9.2)
example_integer2 =
9
>>
complex_value_1 =
32.00000000000000 + 6.00000000000000i
>>
>> complex_value_2 = 8+ 287.75i
complex_value_2 =
8.00000000000000 + 287.75000000000000i
>> 22
Special (in-built) Variables
Matlab has a few “special” variables inbuilt, these
are listed below:
Inf - infinity
23
Some simple inbuilt functions
Matlab has many inbuilt functions to
make your programming easier.
24
Some simple inbuilt functions
• Here are some examples:
>> sqrt(pi)
ans =
1.77245385090552
>>
>> log(exp(3))
ans =
3
>>
>> log10(100)
ans =
2
>> 25
Trigonometric functions
Matlab also has many trigonometric
functions, for example:
ans =
1.224646799147353e-016
>>
>> csc(pi/2)
ans =
1
>>
ans =
1.04719755119660
>>
>> asin(0.707)
ans =
0.78524716339515
>>
• Note that MATLAB returns solutions
using radians not degrees.
29
A simple example problem
• We now know enough to use
MATLAB to solve some simple
engineering problems.
• Here is an example…
30
A simple example problem
Imagine you are a chemical engineer.
Your boss informs you there is a leak
in the system.
Vinlet = 28 metres/sec
Voutlet = 29 metres/sec
I have made
our known
parameters
variables in
MATLAB
using the
command
window.
34
A simple example problem
Next we
determine the
inlet and outlet
pipe areas.
2
D
Area
4
35
A simple example problem
Now we can solve our equation:
>> tank_volume_per_unit_time =
inlet_area*inlet_velocity - outlet_area*outlet_velocity
tank_volume_per_unit_time =
2.22267680241478
Go to the computer
laboratories, log on to a
computer and repeat
some of the examples
you saw today on
MATLAB.
38
Lecture Summary
• Today we discussed variables
– It is important to know how big, and how small a
number a variable can hold.
39
Next Lecture
•Writing ‘M-files’
•How to program:
- Setting up a problem
- programming
- commenting your
code
40