Solve Linear Algebraic Equations using Octave
Overview
In this tutorial, we will learn how to solve linear algebraic equations using Octave software. We will use the Gaussian elimination to solve the system of equations.
Example
Let’s solve the following system of three linear equations. The unknowns are x, y, and z.
4x + 3y + 2z = 44
3x + 7y + 4z = 60
8x + 9y + 5z = 101
We will define three matrices so that we can represent the equations in matrix form:
Ax = b
where A, x, and b are matrices.
To solve the unknowns, we need to solve for x. Let’s write the linear equations in the form:
x = A\b
Octave Code
>> % Define A
>> A = [4 3 2;3 7 4;8 9 5];
>> % Define b
>> b = [44; 60; 101];
>> x = A\b
x =
6
2
7
>>
It is clear that the solution is
x = 6
y = 2
z = 7
Alternatively, we can use the inverse matrix of A using the inv() function.
x = inv(A)*b
Note that the inv() function is slow and inaccurate for a large set of equations.
We can verify the solution by computing the
A*x – b
The result should be close to zero.
>> format bank
>> A*x -b
ans =
0.00
0.00
0.00
—
Octave Tutorials
Octave Tutorial on this website can be found at:
https://www.testingdocs.com/octave-tutorial/
More information on Octave can be found on the official website:
https://www.gnu.org/software/octave/index