Professional Documents
Culture Documents
٢
Command prompt (>>)
You can type your commands only beside this
prompt
Command
prompt
٣
MATLAB Environment
٤
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
١٢
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)
١٥
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 =
١٩
II- Matrix Operation
To get the inverse of matrix
ans =
-0.4286 0.1429
0.7143 -0.0714
٢٠
III-Array Operation
>>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