0% found this document useful (0 votes)
45 views40 pages

1st - Yr - Lecture 02 - NEW

This lecture covered variables and operators in MATLAB. It discussed what variables are, limitations on variable types, and using MATLAB as a calculator. The key points covered were: 1. Variables allow storing and reusing numbers in MATLAB. Variable types like integers and floating point numbers have different memory usage and value ranges. 2. MATLAB can be used as a basic calculator, with operators like addition, subtraction, multiplication, and division working as expected based on order of operations. 3. Different variable types like floating point doubles and integer types store numbers differently, with integers using less memory but having smaller value ranges. Understanding variable types is important for memory usage and accuracy.

Uploaded by

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

1st - Yr - Lecture 02 - NEW

This lecture covered variables and operators in MATLAB. It discussed what variables are, limitations on variable types, and using MATLAB as a calculator. The key points covered were: 1. Variables allow storing and reusing numbers in MATLAB. Variable types like integers and floating point numbers have different memory usage and value ranges. 2. MATLAB can be used as a basic calculator, with operators like addition, subtraction, multiplication, and division working as expected based on order of operations. 3. Different variable types like floating point doubles and integer types store numbers differently, with integers using less memory but having smaller value ranges. Understanding variable types is important for memory usage and accuracy.

Uploaded by

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

ENG 1060

Computing for Engineers

Lecture 2:
Variables & operators / How to program
Last Lecture

• Introduction to the subject


• Syllabus
• Assessment
• Text books
• On-line resources
• Matlab – an introduction

2
Lecture Outline
• What is a variable, why is it important to know
the limitation of variables?
• Using Matlab as a calculator
• Some examples

At the End of this lecture you should be able to:

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.

• The velocity of the


rocket exceeded the
space allocated to
the computer
memory.
• The computer started
sending random
numbers to the
guidance system.
• The guidance system
became unstable and
the rocket was
detonated after 30
sec.
5
Variables
• What is a variable?
– A variable is a name we give to a number, so that
we can store it on a computer and reuse it when
we want to, eg:

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.

• Matlab remembers the commands you enter, as well


as the values of any variables you create.
• Here is an example, in the command window type:
>> tape = 3

• We have now created a variable called “tape” and set


its value to 3.
• What if we forget the value of tape? How do we get
Matlab to remind us? Just type tape and press return.

>> tape
tape =
3
7
Using MATLAB as a calculator

• We can now perform some simple example calculations:


Addition and Subtraction:

>> 3 + 22
ans =
25
>> tape + tape
ans =
6
>> bob = tape - tape
bob =
0

Pretty straight forward…


8
Using MATLAB as a calculator
We can now perform some simple example
calculations:
Division and Multiplication:

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

• We can now perform some simple example calculations:


Exponentiation (to the power of…):

>> 5^3
ans =
Operator Precedence… 125
What would be the answer of the following?

>> 8 * 3 – 6 * 4

The answer could be

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)

Variable names may be descriptive: The_speed_of_the_sphere


• Variables must start with a letter, however they may have any
number of digits, letters or underscores

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.

The following variable names would not be allowed:


six*eight, sphere.volume 12
Variable Types
• Just like you, Matlab treats different data types
in different ways…

• There are many different variable types


• These include:
Floating point numbers: 3.1419
Integers: 1, 5, 887
Characters: ‘Hello world’
Complex numbers: 3+5i
– Matlab assigns different amounts of
computer memory for each variable type
13
Variable Types #1 : Floating point numbers
• How does the amount of memory that MATLAB
allocates to a number affect that number?
• (MOST) Computers store numbers in binary format
• The number of bits (1’s and 0’s) that a computer uses
to store a number affects:

• The maximum number which may be stored


• The number of decimal places that may be stored

– 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.

Usually you won’t need to do this, but


some engineering equations are large and
repetitive. At each repetition, the error can
grow larger and larger.

Also, it would be silly to use a lot of


computer memory to store a number if
you don’t need it to be so accurate.
(normally pi to 3 decimal places is
accurate enough) 15
Variable Types #1 : Floating point numbers

• Matlab has a default for floating point numbers called


“double”

• Your computer has a default amount of space to store a


floating point number. MATLAB takes up twice this amount
of space to store a “double” precision floating point
number.

• The actual amount of space used depends on your


computer, a 32 bit computer stores a floating point number
with 32 bits ( that is 32 one’s and zero’s), a 64 bit computer
uses 64 bits.

16
Variable Types #1 : Floating point numbers

• When you write a variable name, MATLAB instantly


assumes that it is of type “double”. For example:

There are limitations on the largest number which


can be stored, on any computer, the largest floating
point number which MATLAB can store is:
>> realmax
ans =
1.797693134862316e+308 17
Variable Types #2 : Integer variables

• Floating point numbers take up a lot more space


than integers!
• If you make up the following variable:

>> example_integer = 9876


• This number hasn’t got a decimal point in it,
however Matlab will store it as:

• 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
>>

>> example_integer3 = int16(11.8)


example_integer3 =
12
>>
19
Variable Types #2 : Integer variables
MATLAB provides many different
types of integers, the following table
summarizes some of them:
Data Type Description
int8 8 bit integer in the range -128 to 127
int16 16 bit integer in the range -32,768 to 32,767
int32 32 bit integer in the range -231 to 231 – 1
int64 64 bit integer in the range -263 to 263 – 1

• What if I try to store the number 400


in an 8 bit integer variable?

>> example_integer = int8(400)


example_integer =
127 20
Testing a Data Type
• What if you forget what data type your variable
is??
• You may try and store a floating point number
in an integer variable.
• You may check by using the inbuilt functions
‘isnumeric’ and ‘isinteger’, or you may type
‘whos’ for a complete listing of all your
variables.
>> whos

Name Size Bytes Class


ans 1x1 8 double array
example_integer 1x1 1 int8 array
example_variable 1x1 8 double array

Grand total is 3 elements using 17 bytes


21
Variable Types #4 : Complex variables

• Occasionally you may need to perform


calculations using complex variables…

• Matlab allows you to create complex numbers


just as easily as floating point numbers:
>> complex_value_1 = 32 + 6i

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:

pi - The number 3.141592…

eps - A tiny number (2.220446049250313x 10-16 on


my computer)

Inf - infinity

NaN - Not a number = 0/0

i or j - The square root of -1.

There will be examples of how to use these


in the next few slides.

23
Some simple inbuilt functions
Matlab has many inbuilt functions to
make your programming easier.

Today we just look at some simple ones…

exp(A) - The exponential of A

log(A) - The natural log of A

log10(A) - The base 10 logarithm of A

log2(A) - The base 2 logarithm of A

sqrt(A) - The square root of A

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:

cos(A) - The cosine of A

sin(A) - The sine of A

tan(A) - The tangent of A

cot(A) - The cotangent of A

csc(A) - The cosecant of A

sec(A) - The secant of A


26
Some simple inbuilt functions

Here are some examples:


>> sin(pi)

ans =
1.224646799147353e-016
>>

>> csc(pi/2)

ans =
1
>>

• Note that MATLAB assumes we are


using radians not degrees.
27
Inverse Trigonometric functions

Finally, Matlab has many inverse


trigonometric functions, for example:

acos(A) - The inverse cosine of A

asin(A) - The inverse sine of A

atan(A) - The inverse tangent of A

acot(A) - The inverse cotangent of A

acsc(A) - The inverse cosecant of A

asec(A) - The inverse secant of A


28
Some simple inbuilt functions
• Here are some examples:
>> acos(0.5)

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…

Chemical reactions don’t happen


instantaneously.

Chemical companies use tanks to


store reactants together until they
have had time to react completely.
Then the reactants are expelled to
another facility, or to the customer.

30
A simple example problem
Imagine you are a chemical engineer.
Your boss informs you there is a leak
in the system.

You need to transfer the chemical


process to a new system with one
tank, an inlet and an outlet. It is your
responsibility to ensure that the
mixing tank shown on the left does
not overflow.

Also, the reactants must have at


least 25 minutes in the tank to react.
Will the tank do the job?
31
A simple example problem
You have to work out how long the inlet
and outlet valves can remain open before
the tank overflows.

You measure the fluid velocity at the inlet and


outlet and find that the flow travels at:

Vinlet = 28 metres/sec
Voutlet = 29 metres/sec

You also know:

Tank Volume = 3600 cubic metres


Inlet Pipe diameter = 0.6 metres
Outlet pipe diameter = 0.5 metres
32
A simple example problem

The question could be restated as:


how fast will the tank fill?
 Volume in tank   velocity  Areainlet
inlet
time
 velocityoutlet  Areaoutlet

As we know the inlet and outlet


velocities, and the diameter of the
inlet and outlet pipes, we can solve
this equation…
33
A simple example problem

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:

Volume in tank   velocity  Areainlet  velocityoutlet  Area outlet


inlet
time

>> tank_volume_per_unit_time =
inlet_area*inlet_velocity - outlet_area*outlet_velocity

tank_volume_per_unit_time =

2.22267680241478

Matlab has just told us how much the volume


of fluid in the tank increases per second.
36
A simple example problem
We know our tank can hold 3600 cubic
metres of fluid. So how long can this
tank operate?
>> operation_time = tank_volume/tank_volume_per_unit_time
operation_time =
1.619668678815048e+003
>>

MATLAB has just given you your


answer, the tank system can
operate for about 1620 seconds
(about half an hour) before the inlet
valve must be turned off.
37
Before Next Lecture

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.

• We also discussed using MATLAB as a


calculator.

• You should be able to use all the functions


presented in this lecture.

39
Next Lecture
•Writing ‘M-files’

•How to program:
- Setting up a problem
- programming
- commenting your
code

40

You might also like