Matrix and Vector Math

McLabEn environment variables are all matrixes. Matrix variables should be declared between [ ] and lines are separed by ;.
To solve linear systems Mx = b (including complex numbers) the function linsolve(M;b) should be used. It returns the value of the solution x.
It is possible to invert the matrix M by using the function inverse(M).
The function det(M) calculates the determinant of the matrix M.

ps.: The decimal number separator is the comma (‘,’), according to the Portuguese language convention.

Examples

Example 1: Solve linear system:

a+b+c+d = 1
a-b+c+d = 2
a+3b+4c-d = 3
a-b-c-d = 4

>>M = [1 1 1 1;1 -1 1 1;1 3 4 -1; 1 -1 -1 -1]
>>b = [1 2 3 4]
>>x=linsolve(M;b)
xx = [2,5 -0,5 0,2 -1,2 ]

Which means a solution exists and a = 2,5 b = -0,5 c = 0,2 d = -1,2.

Example 2: Invert matrix

>>M = [1 1 1 1;1 -1 1 1;1 3 4 -1; 1 -1 -1 -1].

>>invM=inversa(M)
invM =
[0,5 2,77555756156289E-17 0 0,5
0,5 -0,5 0 0
-0,4 0,4 0,2 -0,2
0,4 0,1 -0,2 -0,3 ]

Example 3: Calculate the determinant of matrix M = [1 2 3;4 5 6;7 8 9]

>>M=[1 2 3;4 5 6;7 8 9]
>>det(M)
ans = 6,66133814775094E-16
In practice, the determinant is zero.

Exemplo 4: Balance the reaction equation aCH4 + bO2 -> cCO2+ dH2O by using a linear system.

Each element yields an equation:

C – a = c
H – 4a = 2d
O – 2b = d + 2c
The system is overdetermined and we can force d=2. Then:
a – c = 0
4a = 4.
2b – 2c = 2.
>>M=[1 0 -1;4 0 0;0 2 0]
>>b=[0 4 2]
>>x=linsolve(M;b)
x =
[1 2 1]
This means CH4 + 2O2 -> CO2 + 2H2O

Leave a Reply

Your email address will not be published. Required fields are marked *