Solving System of Equations in MATLAB & Octave



Prev TutorialNext Tutorial

Solving System of Equations in MATLAB

The solve function can also be used to generate solutions of systems of equations involving more than one variables. Let us take up a simple example to demonstrate this use.
Let us solve the equations −
5x + 9y = 5
3x – 6y = 4
Create a script file and type the following code −
s = solve('5*x + 9*y = 5','3*x - 6*y = 4');
s.x
s.y
When you run the file, it displays the following result −
ans =
 22/19
ans =
-5/57
In same way, you can solve larger linear systems. Consider the following set of equations −
x + 3y -2z = 5
3x + 5y + 6z = 7
2x + 4y + 3z = 8

Solving System of Equations in Octave

We have a little different approach to solve a system of 'n' linear equations in 'n' unknowns. Let us take up a simple example to demonstrate this use.
Let us solve the equations −
5x + 9y = 5
3x – 6y = 4
Such a system of linear equations can be written as the single matrix equation Ax = b, where A is the coefficient matrix, b is the column vector containing the right-hand side of the linear equations and x is the column vector representing the solution as shown in the below program −
Create a script file and type the following code −
A = [5, 9; 3, -6];
b = [5;4];
A \ b
When you run the file, it displays the following result −
ans =

   1.157895
  -0.087719
In same way, you can solve larger linear systems as given below −
x + 3y -2z = 5
3x + 5y + 6z = 7
2x + 4y + 3z = 8

Expanding and Collecting Equations in MATLAB

The expand and the collect function expands and collects an equation respectively. The following example demonstrates the concepts −
When you work with many symbolic functions, you should declare that your variables are symbolic.
Create a script file and type the following code −
syms x %symbolic variable x
syms y %symbolic variable x
% expanding equations
expand((x-5)*(x+9))
expand((x+2)*(x-3)*(x-5)*(x+7))
expand(sin(2*x))
expand(cos(x+y))
 
% collecting equations
collect(x^3 *(x-7))
collect(x^4*(x-3)*(x-5))
When you run the file, it displays the following result −
ans =
 x^2 + 4*x - 45
 ans =
 x^4 + x^3 - 43*x^2 + 23*x + 210
 ans =
 2*cos(x)*sin(x)
 ans =
 cos(x)*cos(y) - sin(x)*sin(y)
 ans =
 x^4 - 7*x^3
 ans =
 x^6 - 8*x^5 + 15*x^4

Expanding and Collecting Equations in Octave

You need to have symbolic package, which provides expand and the collectfunction to expand and collect an equation, respectively. The following example demonstrates the concepts −
When you work with many symbolic functions, you should declare that your variables are symbolic but Octave has different approach to define symbolic variables. Notice the use of Sin and Cos, which are also defined in symbolic package.
Create a script file and type the following code −
% first of all load the package, make sure its installed.
pkg load symbolic

% make symbols module available
symbols

% define symbolic variables
x = sym ('x');
y = sym ('y');
z = sym ('z');

% expanding equations
expand((x-5)*(x+9))
expand((x+2)*(x-3)*(x-5)*(x+7))
expand(Sin(2*x))
expand(Cos(x+y))
 
% collecting equations
collect(x^3 *(x-7), z)
collect(x^4*(x-3)*(x-5), z)
When you run the file, it displays the following result −
ans =

-45.0+x^2+(4.0)*x
ans =

210.0+x^4-(43.0)*x^2+x^3+(23.0)*x
ans =

sin((2.0)*x)
ans =

cos(y+x)
ans =

x^(3.0)*(-7.0+x)
ans =

(-3.0+x)*x^(4.0)*(-5.0+x)

Factorization and Simplification of Algebraic Expressions

The factor function factorizes an expression and the simplify function simplifies an expression. The following example demonstrates the concept −

Example

Create a script file and type the following code −
syms x
syms y
factor(x^3 - y^3)
factor([x^2-y^2,x^3+y^3])
simplify((x^4-16)/(x^2-4))
When you run the file, it displays the following result −
ans =
 (x - y)*(x^2 + x*y + y^2)
 ans =
 [ (x - y)*(x + y), (x + y)*(x^2 - x*y + y^2)]
 ans =
 x^2 + 4