You are on page 1of 34

omputing Applications for Engineers (ENG2314 / ENG60104)

Introduction
to MATLAB

Addition

MATLAB as
Calculator
Rules

Subtraction

+
-

Multiplication *
Divide

Follow the usual order of


evaluation, i.e., brackets,
then power, then
multiplication or division,
then addition or
subtraction

Addition

MATLAB as
Calculator

Subtraction

+
-

Multiplication *
Divide

Example

Example

x=+
5 *3

2/2-10

2
x=11

ans=-9

Example
y=(5+3)*2
y=16
4

MATLAB as
Calculator
Basic scalar arithmetic operations
Matlab
Command
Exponential
Multiplication
Right division (Divide)
Left division
Addition
Subtraction

^
*
/

a
a
a

b
b
b

\
+

a
a

b
b

b
5

MATLAB as
Calculator
Basic scalar arithmetic operations
Matlab
Command
Exponential
Multiplication
Right division (Divide)
Left division
Addition
Subtraction

*
/

a^b
a* b
a/ b

\
+

a\ b
a+b

a- b

Variable
Variables are a fundamental concept in MATLAB.
To assign variable in MATLAB, = sign is used
e.g.,
x=1
y=sin(x)
It is case sensitive (e.g., NAME, Name, name)

Example

The volume of a
cylinder of height h
and a radius r is given
by
. A
particular cylindrical
tank is 150 m tall and
has a radius of 80m.
Using
MATLAB
to
calculate the volume
of the tank.

>> r=80;
>> h=150;
>> V=pi*r^2*h
V=
3.0159e+06

Example

>> V=pi*r^2*h
V=
3.0159e+06
>> format long

If we want to display
all the decimal places,
type
format long
Also try format short

Example

>> format long


>> V
V=

If we want to display
all the decimal places,
type
format long
also try format short
Also try format short

3.015928947446202e+06

MATLAB built-in function


mean(A)

Mean value of a vector

max(A) / min(A)

Maximum and minimum

sum(A)

Summation

std(A)

standard deviation

inv(A)

Inverse of a matrix
There are many built-in function
See MATLAB help file

Mathematics expression
Function

MATLAB code

Managing The Work Session


To remove variables from memory type
clear

clear all variables

clear functions clear all m-files and other


defined functions
clear x y
clear all
clc

clear variables x and y only


variables, functions, and MEX-files
from memory, leaving the
workspace empty
clear everything in the command window

Matrices, Vectors, and Scalars

A matrix with only one row and one column is a scalar

a_value=23
a_value =
23

Matrices, Vectors, and Scalars


A matrix with only one row is called row vector

rowvec = [12, 14, 63]


rowvec =
12 14 63

Matrices, Vectors, and Scalars

A matrix with only one column is called column vector

colvec = [13 ; 45 ; -2]


colvec =
13
45
-2

Matrices, Vectors, and Scalars


A matrix can be created in MATLAB as follow:

matrix = [1, 2, 3 ; 4, 5, 6; 7, 8, 9]
matrix =
1 2 3
4 5 6
7 8 9

Matrices, Vectors, and Scalars


A row vector can be converted to a column
vector and vice versa using the apostrophe:
matrix = [1, 2, 3 ; 4, 5, 6; 7, 8, 9]
matrix =
1 4 7
2 5 8
3 6 9

Extracting a Sub-matrix
matrix =
row 1
row 2
row 3

1 2 3
4 5 6
7 8 9
col 1
col 2
col 3

Extracting a Sub-matrix
matrix =
1 2 3
4 5 6
7 8 9
>> matrix(: , 2)
row

column

If we want to extract a
sub-matrix from the
matrix shown on the
left
Anything before comma,
is row;
Anything after comma,
is column

Matrices, Vectors, and Scalars


Creating a row vector from starting from 1 to 5
with an interval of 1

[1, 2, 3, 4, 5]
ans =
1 2 3 4 5

Matrices, Vectors, and Scalars


Creating a row vector from starting from 1 to
100 with an interval of 1.
Are you going to type from 1 to 100? NO
We can use colon operator, : , to build the vector
x = [start : step : stop]

Matrices, Vectors, and Scalars


Example

a=[1: 1 :5]
a=
1 2 3 4 5

Matrices, Vectors, and Scalars


Example

a=[1: 2 :5]
a=
1 3 5

Matrices, Vectors, and Scalars


Example

a=[1: -2 : -5]
a=
1 -1 -3 -5

Scalar - Matrix Addition

a=3;
>>
b=[1, 2, 3; 4, 5, 6];
>>c= a + b

1 2 3
3 +
4 5 6

c=
4 5 6
7 8 9

Scalar - Matrix Subtraction

a=3;
>>
b=[1, 2, 3; 4, 5, 6];
>>c= a - b

1 2 3
3 4 5 6

c=
-2 1 0
-1 -2 -3

Scalar - Matrix Multiplication and Division


Using the previous example, determine the
multiplication and division.
c= a * b
c= a / b

Try this
example

Array Dimensions - size

b=[1, 2, 3; 4, 5, 6]
b=
1 2 3
4 5 6
>>size(b)

If you are not clear about the


command, please search for
MATLAB help file

ans =
row

column

Element Operator - .
b=
1 2 3
Divide each
element of b
by 2
>> b ./ 2

4 5 6
Multiply each
element of b by
2
>> b.* 2

Square each
element of b by
2
>> b.^ 2

ans =

ans =

ans =

0.50001.0000 1.5000

2.00002.5000 3.0000

10

12

16

25

36

Graph - 2D plot
Linear plot - command plot(x, y)
Example
x = [-3 -2 -1 0 1 2 3];
y = [8 3 0 -1 0 3 8];
plot(x, y)

x=[-3:3];
>>y=[8 3 0 -1 0 3 8];

Graph of x vs y

>>plot(x, y)
>>ylabel(y)

>>xlabel(x)
>>title(Graph of x vs y)
>>hold on
>>y1=(4/3)*x+4;
>>plot(x, y1, r)

plot(x, y)

Try
plot(x, y, ro)

If you unclear the command,


type plot in the MATLAB help
file

Also see semilogx,semilogy, title, xlabel, ylabel, hold on,


subplot, figure

Thank You!

You might also like