Basic Matlab Operations
Basic Matlab Operations
Matlab
Starting Matlab
Using Matlab as a calculator
Introduction to variables and functions
1 2
Starting Matlab
Matlab Workspace (version 6)
•Double click o the Matlab icon, or on UNIX systems type
matlab at the command line.
•After startup Matlab displays a command window that is
used to enter commands and display text-only results.
•Enter Commands at the command prompt:
>>for full version
EDU>for educational version
•Matlab responds to commands by printing text in the
command window, or by opening a figure window for
graphical output.
•Toggle between windows by clicking on them with the
mouse.
3 4
1
Matlab as a Calculator Built-in Variables
Enter formulas at the command prompt pi (=Л) and ans are a built-in variables
>>2 +6 -4 (press return after “4”)
ans=
4 >>pi
>>ans/2 ans=
ans= 3.1416
2
5 6
7 8
2
On-line Help
Ways to Get Help Syntax:
•Use on-line help to request info on a specific
function help functionName
>>help sqrt
Example:
•The helpwin function opens a separate >>help log
window for the help browser Produces
>>helpwin(“sqrt “) LOG Natural logarithm.
LOG(X) is the natural logarithm of the
•Use lookfor to find functions by keywords elements of X.
>>lookfor functionName Complex results are produced if X is not
positive.
See also LOG2, LOG10, EXP, LOGM.
9 10
11 12
3
Suppress Output with Semicolon Multiple Statements per Line
Results of intermediate steps can be suppressed with semicolons.
13 14
15 16
4
Matrices and Vectors
All Matlab variables are matrices
A Matlab vector is a matrix with one row or one
column
Matrices and Vectors column
A Matlab scalar is a matrix with one row and one
17 18
19 20
5
Transpose Operator Matrix Operation
Once it is created, a variable can be transformed with other operators. The transpose
operator converts a row vector to a column vector (and vice versa), and it changes the Product of a matrix and a constant
rows of a matrix to columns. >>F= 3*A
>>v = [2 4 1 7 ]
v= Matrix multiplication
2 4 1 7 >>A = [1 2 3; 4 5 6];
>>v ’ = >>B = [2 4 6 8; 1 2 3 4; 1 3 5 7];
ans= >>C = A*B
2 C=
4
1 >>D = B*A
7 %???
>>A = [1 2 3; 4 5 6; 7 8 9] Matrix division
A= >>C = A/B
1 2 3
4 5 6 Inverse of a Matrix
7 8 9 >>C = A*Inv(B)
>>A’ = Determinant of a Matrix
ans = >>C = det(B)
1 4 7
2 5 8
3 6 9
21 22
23 24
6
Creating vectors with linspace Example: A Table of Trig Functions
The linspace function creates vectors with elements having uniform >>x =linspace(0,2*pi,6)’; (note transpose)
linear spacing. >>y =sin(x);
Syntax: >>z =cos(x);
x = linspace(startValue,endValue) >>[x y z ]
x = linspace(startValue,endValue,nelements) ans=
Examples: 0 0 1.0000
>>u = linspace(0.0,0.25,5) 1.2566 0.9511 0.3090
u= 2.5133 0.5878 -0.8090
0 0.0625 0.1250 0.1875 0.2500 3.7699 -0.5878 -0.8090
>>u =linspace(0.0,0.25); 5.0265 -0.9511 0.3090
>>v =linspace(0,9,4)’ 6.2832 0 1.0000
v=
0 The expressions y =sin(x) and z =cos(x) take advantage of vectorization. If
3 the input to a vectorized function is a vector or matrix, the output is often a
6 vector or matrix having the same shape.
9
Note: Column vectors are created by appending the transpose operator
to linspace
25 26
27 28
7
Syntax: Syntax:
A = ones(nrows ,ncols) A = eye(n )
A = zeros(nrows ,ncols) A = eye(nrows ,ncols )
Examples:
Examples:
>>D = ones(3,3)
D=
>>C = eye(5)
1 1 1 C=
1 1 1 1 0 0 0 0
1 1 1 0 1 0 0 0
>>E = ones(2,4) 0 0 1 0 0
E= 0 0 0 1 0
0 0 0 0 0 0 0 0 1
0 0 0 0 >>D = eye(3,5)
ones and zeros are also used to create vectors. To do so, set either nrows or ncols to 1.
>>s=ones(1,4)
D=
s= 1 0 0 0 0
1 1 1 1 0 1 0 0 0
>>t =zeros(3,1) 0 0 1 0 0
t=
0 The diag function can either create a matrix with specified diagonal elements,
0 or extract the diagonal elements from a matrix
0
The eye function creates identity matrices of a specified size. It can also create non-
square matrices with ones o the main diagonal.
29 30
Syntax:
A = diag(v )
v = diag(A)
Subscript Notation
Use diag to create a matrix
>>v = [1 2 3];
>>A = diag(v) If A is a matrix, A(i,j) selects the element in the ith row and jth column.
A= Subscript notation can be used on the right hand side of an expression to refer to
1 0 0 a matrix element.
0 2 0
0 0 3 >>A = [1 2 3; 4 5 6; 7 8 9];
Example:
>>b = A(3,2)
Use diag to extract the diagonal of a matrix
>>B = [1:4; 5:8; 9:12] b=
B= 8
1 2 3 4 >>c = A(1,1)
5 6 7 8 c=
9 10 11 12 1
>>w = diag(B)
w=
1
6
11
Note: The action of the diag function depends on the characteristics and number of the
input(s). This polymorphic behavior of Matlab functions is common. The on-line documentation
(help diag ) explains the possible variations.
31 32
8
Subscript Notation Subscript Notation
Subscript notation is also used to assign matrix elements Assigning an element outside of current matrix dimensions
causes the matrix to be resized!
>>A(1,1)=c/b
A= >>A = [1 2 3; 4 5 6; 7 8 9];
0.2500 2.0000 3.0000 A=
4.0000 5.0000 6.0000 1 2 3
7.0000 8.0000 9.0000 4 5 6
7 8 9
Referring to elements outside of current matrix dimensions results in
an error >>A(4,4) = 11
A=
>>A = [1 2 3;4 5 6;7 8 9]; 1 2 3 0
>>A(1,4) 4 5 6 0
7 8 9 0
???Index exceeds matrix dimensions. 0 0 0 11
Matlab automatically resizes matrices on the fly.
33 34
Colon Notation
Colon Notation
Creating row vectors:
Colon notation is very powerful and very important in the effective use of >>s = 1:4
Matlab .The colon is used as both an operator and as a wildcard. s=
1 2 3 4
Use colon notation to: >>t = 0:0.1:0.4
• create vectors t=
• refer to or extract ranges of matrix elements 0 0.1000 0.2000 0.3000 0.4000
Creating column vectors:
Syntax: >>u =(1:5)’
startValue : endValue u=
startValue : increment : endValue 1
2
3
Note: startValue, increment, and endValue do not need to be integers 4
5
>>v =1:5 ’
v=
1 2 3 4 5
v is a row vector because 1:5 ’creates a vector between 1 and the transpose of 5.
35 36
9
Colon Notation
37 38
Examples:
Exercise: Formula Evaluation
>>x =1:4;
>>y =x(:)
y=
1
2 V = [√ (g m / cd)] [ tanh √( g cd t / m) ]
3
4 Where: v = velocity, m/s
>>A =rand(2,3); g = acceleration due to gravity (9.81 m/s2)
>>v =A(:) m = mass, kg
v= cd = drag coefficient, kg/m
0.9501 t = time, s
0.2311
0.6068 I. Create a column vector, t, that contains values from 0 to 20 in
0.4860 steps of 2
0.8913 II. Create the number of items in the t array with the length function
0.7621 Note: Syntax: length(variable)
0.4565 III. Assign values to the parameters of g equal 9.81, m equal 68.1
Note: The rand function generates random elements between zero and one. and cd equal 0.25.
Repeating the preceding statements will, in all likelihood, produce different IV. Enter the formula of v
numerical values for the elements of v.
39 40
10
Exercise:
Define the Matrices
Exercise:
A= B=
1 2 3 4 1 1 1 1 Create this Matrix from the given Matrices:
6 8 1 5 1 1 1 1
9 4 2 7 1 1 1 1 T=
9 4 7 2 1 1 1 1 1 2 3 4 2 2 2 2 4 3 3 3
C= D= 6 8 1 5 2 2 2 2 3 4 3 3
16 2 3 13 1 0 0 0 9 4 2 7 2 2 2 2 3 3 3 4
5 11 10 8 0 3 0 0 9 4 7 2 2 2 2 2 4 3 3 3
9 7 6 12 0 0 5 0 1 4 7 10 16 2 3 13 1 0 0 0
4 14 15 1 0 0 0 7 16 22 1 13 5 11 10 8 0 3 0 0
E= F= 25 10 4 19 9 7 6 12 0 0 5 0
1 0 0 0 3 6 9 12 25 10 19 4 4 14 15 1 0 0 0 7
0 1 0 0 18 24 3 15
0 0 0 1 27 12 6 21
1 0 0 0 27 12 21 6
Solve:
3A + 4F 2DEF+4B
ABC/4F FEB/2CB
(2B – 3D) * (2ABC)
41 42
11