You are on page 1of 34

MATLAB Fundamentals

MATLAB Environment Command window

٢
Command prompt (>>)
You can type your commands only beside this
prompt

Command
prompt

٣
MATLAB Environment

• For each command, MATLAB will display the


result.
• MATLAB assigns the result to ans whenever
you do not assign the calculation to a certain
variable.

٤
MATLAB Environment

Example:
If you type

>> 55-16
MATLAB will display

Ans=
39

٥
Assignment
• Scalars
• Arrays

٦
Scalars
• You can assign numbers to a scalar by this
simple command
>> a = 5
a=
5

٧
Scalars
MATLAB is case-sensitive
That means a is not the same as A
Example
>>a=3;
>>A=4;
>>a
a=
3

٨
Arrays
I-Generating arrays from command line
• Vectors are one-dimensional array,
Matrices are two-dimensional array
• Brackets [] used to enter arrays in MATLAB
Example
To define a row vector

>>a=[1 2 3 4 5]
a=
1 2 3 4 5
٩
Arrays
To define a column vector, you have many ways
>>b=[1;2;3]
OR
>>b=[1;
2;
3]
OR
>>b=[1 2 3]’
In all cases MATLAB will display
b=1
2
3
١٠
Arrays
A matrix can be assigned as follows
>>c=[1 2 3;4 5 6;7 8 9]
OR
>>c=[1 2 3
456
7 8 9]
In these cases MATLAB will display
c=
1 2 3
4 5 6
7 8 9
١١
Arrays
Subscript notation used To access an individual element of
any array

Example
To display the third element of the column vector b

>> b=[1 2 3 5];


>> b(4)
ans =
5

To modify the third element of vector b


>>b(4)=10
b=
1 2 3 10

١٢
Arrays
c(m,n) display the element in mth row and the nth column of
matrix c
Example
To display the element in second row and the third column of matrix c
>> c=[1 4 5;2 4 6;2 4 6];
>> c(1,3)

ans =

١٣
Arrays
II-Generating arrays using built-in functions
Zeros (n,m) Creates n x m matrix of zeros
Ones (n,m) Creates n x m matrix of ones
Rand (n,m) Generate random vectors and matrices
whose elements are Uniformly distributed
in the interval(0,1)

Linspace(x1,x2,n) Creates a linearly spaced vector of n


points between X1 andX2

Logspace(d1,d2,n) Creates logarithmically spaced vector of


n points between decades 10^ d1 and
10^d2
Eye (n) Creates n x n identity matrix
١٤
Mathematical Operations
• Operations with scalar
^ Exponentiation
quantities handled in a
straight forward Negation
-
manner
Multiplication
*/
• The common operators and Division
are shown in the table Addition and
+-
in order of priority Subtraction

١٥
Mathematical Operations
Mathematical Operations fall into three
categories
I-Numerical operations
II- matrix Operation
III-Array Operation

١٦
I-Numerical operations
• Example
>> y=-4 ^2
Y=
-16
Exponentiation has higher priority then negation

>> y=2*2^2
Y=8

١٧
II- Matrix Operation
(A*B , 3*A , A^ -1 , A+B , A-B , A^3 )

>>a=[1 2 3];
>>b=[5 1 1]’;
>>a * b
ans=
10

>> b*a

ans =

5 10 15
1 2 3
1 2 3
١٨
II- Matrix Operation
>>a=[1 2 3 ; 5 6 7 ; 8 9 10];
>>a ^ 4
ans =

7560 9288 11016


17118 21033 24948
26676 32778 38880

١٩
II- Matrix Operation
To get the inverse of matrix

>>a=[1 2;10 6];


>>a^-1

ans =
-0.4286 0.1429
0.7143 -0.0714

٢٠
III-Array Operation

To carry out calculations item by item in a


matrix or vector
>>a=[1 2 ; 3 4];
>>a^2
ans =
7 10
15 22

>>a.^2
Ans=
1 4
9 16

٢١
III-Array Operation
>>a=[1 2 ;3 4];
>>b=[1 3;2 3];
>>a.*b

ans =
1 6
6 12

٢٢
III-Array Operation
>>x=[1 2 3];
>>x^2
Will display error

>>x.^2
ans=

1 4 9

٢٣
MATLAB operators
• Semicolon Operator
• Colon operator

٢٤
Semicolon operator(;)
It used after any command to prevent echo printing of
MATLAB

If you type

>>a=5;
MATLAB will display the prompt

>>

٢٥
Colon operator(:)
1-A simple way to create arrays
Array=start :step :end
Example 1
To generate numbers from 1 to 5 using an increment of one

>>t=1:5
t=
1 2 3 4 5

٢٦
Colon operator(:)
Example 2
To generate numbers from 1 to 3 using an increment of 0.5

>>t=1:0.5:3
t=
1.0000 1.5000 2.000 2.5000 3.0000

Example 3
>>t=10:-1:5
t=
10 9 8 7 6 5
٢٧
Colon operator(:)
2-Used to select individual rows and columns of
a matrix

Example 1
To select the second row of matrix c

>>c(2,:)
Ans=
4 5 6
٢٨
Colon operator(:)
3-Used to extract a series of elements from within an array

Example 2
To select the second through fourth elements of the vector t
>>t=[1 9 8 7 4];
>>t(2:4)
Ans=
9 8 7

٢٩
Built-In Functions
• MATLAB has a rich collection of built-In
functions
• For example Log ,Sin ,Cos , …
• For a list of all elementary math functions
,type
>>help elfun

٣٠
Built-In Functions
• To learn more about any function, type
>> help function name

>>help log

٣١
Built-In Functions
>>x=0.5;
>>y=sin(x)
y=
0.4794

٣٢
Built-In Functions
Hint:TO write y as a function of x (without predefining of x)

>>y=@(x) sin(x);
>>y(0.5)

Ans=
0.4794

٣٣
Selected elementary functions for matrices

Min (A) Smallest component. for vectors, min (A) is the ◌ٍ


smallest element in A. For matrices, min (A) is a
row vector containing the minimum element from
each column
Max (A) Largest value

Length (A) Returns the length of A (number of elements) ,if A


is a vector. Returns the number of columns of A, if
A is a matrix

Sum (A) Returns the Sum of elements of A, if A is a vector.


Returns row vector containing the sum of each
column ,if A is a matrix
Sort (A) Sorts the elements of A in ascending order, if A is a
vector. Sorts each column of A ,If A is a matrix
٣٤

You might also like