Matrix: Functional Description

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

1.

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:

>> A=[-5 -2 -4 ; 2 -3 -1 ; 4 1 4];


>> B=[6 ; -2 ; 1];
>> x = A\B

x =
-3.6522
-3.3478
4.7391

The solution: a = 3.6522, b = 3.3478, c = 4.7391

Note: Left & right division: X = A-1B = A\B X = DC-1 = D/C

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);

Input MATLAB Output MATLAB


Functional description
Enter vector u=[1,2,3];v=[2,-1,2];
Generate a vector of equally- u=[1:5] 1 2 3 4 5
spaced elements
Create Vector with Specified x = 0:0.1:1;
Increment
Generate a vector of equally- x=[1:5] 1 2 3 4 5
spaced elements
find vector dimension length (u) 3
Vector norm norm(u) 3.7417
Addition of vectors u+v 3 1 5
Subtraction of vectors u-v -1 3 1
element-wise multiplication u.*v 2 -2 6
element-wise division u./v 0.5000 -2.0000 1.5000
element-wise exponentiation u.^2 1 4 9
Vector dot product dot(u,v) 6
Vector cross product cross(u,v) 7 4 -5
3. Function operation
a) Enter elementary function –MATLAB has many built-in functions, these include sqrt,
cos, sin, tan, log, exp … for example sin(x), cos(x) etc

b) Define an 'anonymous function' using the special notation

>> f = @(x) 3*x + 2


f =

@(x)3*x+2

>> f(0.5) % evaluate f(0.5)

ans =

3.5000

c) Created a new function –


A function is defined in an m-file that begins with a line of the following form:
function [output1,output2,...] = name(input1,input2,...)
Save the file either in the current folder or in a folder on the MATLAB search path.
Use the same name for the function and the file (in this example, name.m).

Here is a function that generates

Example: Create the function of one variable, defined as follows: y = sin(x2)

The file fcn.m contain this text:

function y = fcn(x)
y = sin(x.^2);
Running M-file in command window:

>> fcn(pi)

ans =

-0.4303

Example: Create the function of two variables, defined as follows: z = sin(x2)cos(y2)

The file fce.m contain this text:

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

4. Conditionals and loops


a) Relational & logical operators

The logical operators in Matlab are


Operator Description
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to**
~= Not equal to

Logical operators

Operator Operator Description


& A&B A AND B
= true if both A and B are true,
= false otherwise
| A|B A OR B
= true if A or B is true,
= false if both are false
~ ~A NOT A
= true if A is false,
= false if A is true

b) The IF statement
The IF statement is used to select between two courses of action.

a. if-end  a block of commands is executed if the conditional expression is true,


skipped if it’s false.

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:

Alternatively, a loop may be executed as long as a conditional expression remains true.

while conditional expression


MatLab commands
end

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

You might also like