Matrix: Functional Description
Matrix: Functional Description
Matrix
Input MATLAB Output MATLAB
Functional description
Enter matrix A=[1,1,1;3,1,-1;1,-2,4] 1 1 1
3 1 1
1 2 4
Find matrix dimension size(A) 3 3
Transpose matrix A' 1 3 1
1 1 2
1 1 4
Diagonal matrices and A=[1,1,1;3,1,-1;1,-2,4]; 1
diagonals of matrix diag(A) 1
4
Block diagonal blkdiag(2,7,4) 2 0 0
concatenation 0 7 0
0 0 4
Extract lower triangular tril(A) 1 0 0
part 3 1 0
1 1 4
Extract upper triangular triu(A) 1 1 1
part 0 1 1
0 0 4
Determinant det(A) -18
Matrix inverse inv(A) 0.1111 0.3333 0.1111
0.7222 0.1667 0.2222
0.3889 0.1667 0.1111
the eigenvalues eig(A) -1.3723
3.0000
4.3723
Matrix norm norm(A) 4.6984
norm(A,2) 4.6984
L-norm of matrix, the norm(A,1) 6
largest column sum
Infinity norm of matrix, the norm(A,inf) 7
largest row sum
Frobenius (Eukleides) norm norm(A,'fro') 5.9161
Estimate the matrix 2-norm normest(A) 4.6984
Addition of matrices A=[1,1,1;3,1,-1;1,-2,4]; 4 4 4
A+B B=[3,3,3;4,2,5;1,5,7]; 7 3 4
A+B
2 3 11
Subtraction of matrices A=[1,1,1;3,1,-1;1,-2,4]; 2 2 2
A–B B=[3,3,3;4,2,5;1,5,7]; 1 1 6
A-B
0 7 3
Multiplication of matrices A=[1,1,1;3,1,-1;1,-2,4]; 8 10 15
A*B B=[3,3,3;4,2,5;1,5,7]; 12 6 7
A*B
1 19 21
Example: Solve the following linear system with Matlab.
We get the following set of equations
2b 4c 5a 6 5a 2b 4c 6 (1)
2a c 3b 2 2a 3b c 2 (2)
4a b 4c 1 4a b 4c 1 (3)
In Matlab:
x =
-3.6522
-3.3478
4.7391
2 Vector
The easiest way to define a vector is to list its values inside of square brackets, and separated
by spaces or commas:
>> x = [ 0, 1, 3, 6, 10 ]
MATLAB has a special notation for generating a set of equally spaced values. The format is:
u = startValue:stepSize:endValue;
u = linspace(startValue,endValue,nElements);
@(x)3*x+2
ans =
3.5000
function y = fcn(x)
y = sin(x.^2);
Running M-file in command window:
>> fcn(pi)
ans =
-0.4303
function[z]=fce(x,y);
%
% we use the function fcn
%
z=fcn(x).*cos(y.^2);
Running M-file in command window:
>> fce(pi,pi)
ans =
0.3884
Logical operators
b) The IF statement
The IF statement is used to select between two courses of action.
if conditional expression
Matlab commands
end
b. if-else-end in this case, there are two blocks of MatLab commands—one is
executed if the conditional expression is true, the other if it is false.
if conditional expression
MatLab® commands
else
Matlab commands
end
c. if-elseif-else-end using two conditional expressions, one of three sets of
Matlab commands is executed.
if conditional expression
MatLab commands
elseif conditional expression
MatLab commands
else
Matlab commands
end
Example:
>> t = rand(1);
>> if t > 0.75
s = 0;
elseif t < 0.25
s = 1;
else
s = 1-2*(t-0.25);
end
>> s
s =
0
>> t
t =
0.7622
c) Loop for-end
The for-end loop executes a block of MatLab commands a specified number of times.
for k = f:s:t
MatLab commands
end
The loop executes for k = f, f+s, f+2s, f+3s, . . ., t. The increment, s, may be omitted in which
case it is assumed to be 1.
Example:
>> for i=[1,2,3,4]
disp(i^2)
end
1
4
9
16
d) Conditional loop:
The variables in the conditional expression must have initial values assigned, and at least one
of the variables must be changed within the loop. The while-loop repeats as long as the given
expression is true (nonzero).
Example:
>> x=1;
>> while 1+x > 1
x = x/2;
end
>> x
x =
1.1102e-16