English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

MATLAB Algebra

So far, we have seen that all examples can be run in MATLAB and its GNU (also known as Octave). However, MATLAB and Octave are almost identical in solving basic algebraic equations, so we will try to introduce MATLAB and Octave separately in a dedicated section.

We will also discuss the decomposition and simplification of algebraic expressions.

Solving Basic Algebraic Equations in MATLAB

solveThe function is used to solve algebraic equations. The simplest form is that the solve function takes the equation enclosed in quotes as a parameter.

For example, let's solve the equation x-5 = '0' of x

solve('x-5= '0'

MATLAB will execute the above statements and return the following results-

ans =
   5

You can also call the Solve function as-

y = solve('x-5 = '0'

MATLAB will execute the above statements and return the following results-

y =
   5

You may even not include the right side of the equation-

solve('x-5')

MATLAB will execute the above statements and return the following results-

ans =
   5

If the equation contains multiple symbols, by default, MATLAB assumes that you are solving for x, but the solve function has another form-

solve(equation, variable)

In this case, you can also mention the variables.

For example, let's solve the equation v – u – 3t 2 = '0'. In this case, we should write-

solve('v-u-3*t^2= '0', 'v')

MATLAB will execute the above statements and return the following results-

ans =
   3*t^2 + u

Solving basic algebraic equations using Octave method

rootsThe function is used to solve algebraic equations in Octave. You can write the following example, as shown below:

For example, let's solve the equation x-5 = '0' of x

roots([1, -5]

Octave will execute the above statements and return the following results-

ans = 5

You can also call the Solve function as-

y = roots([1, -5]

Octave will execute the above statements and return the following results-

y = 5

Solving quadratic equations in MATLAB

solveThe function can also solve higher-order equations. It is usually used to solve quadratic equations. The function returns the roots of the equation in array form

The following example solves a quadratic equation x 2 -7x +12 = '0'. Create a script file and type the following code-

eq = 'x^2 -7*x + 12 = '0';
s = solve(eq);
disp('The first root is: '), disp(s(1));
disp('The second root is: '), disp(s(2));

When running the file, it displays the following result-

The first root is: 
   3
The second root is: 
   4

Solving quadratic equations using Octave method

The following example solves a quadratic equation x 2 -7x +12 = '0'. Create a script file and enter the following code-

s = roots([1, -7, 12]);
disp('The first root is: '), disp(s(1));
disp('The second root is: '), disp(s(2));

When running the file, it displays the following result-

The first root is: 
   4
The second root is: 
   3

Solving higher-order equations in MATLAB

solveThe function can also solve higher-order equations. For example, let's solve a cubic equation for (x-3)2(x-7)= 0

solve('(x-3)^2*(x-7)=0'

MATLAB will execute the above statements and return the following results-

ans =
   3
   3
   7

For higher-order equations, the root length contains many terms. You can obtain the numerical value of such roots by converting them to double. The following example solves a quartic equation x 4 − 7x 3 + 3x 2 − 5x + 9 = 0.

Create a script file and enter the following code-

eq = 'x^4 - 7*x^3 + 3*x^2 - 5*x + 9 = '0';
s = solve(eq);
disp('The first root is: '), disp(s(1));
disp('The second root is: '), disp(s(2));
disp('The third root is: '), disp(s(3));
disp('The fourth root is: '), disp(s(4));
% Convert the roots to double type
disp('Numeric value of the first root'), disp(double(s(1);
disp('Numeric value of the second root'), disp(double(s(2);
disp('Numeric value of the third root'), disp(double(s(3);
disp('Numeric value of the fourth root'), disp(double(s(4);

When the file is run, it returns the following results-

The first root is: 
6.630396332390718431485053218985
 The second root is: 
1.0597804633025896291682772499885
 The third root is: 
- 0.34508839784665403032666523448675 - 1.0778362954630176596831109269793*i
 The fourth root is: 
- 0.34508839784665403032666523448675 + 1.0778362954630176596831109269793*i
Numeric value of the first root
   6.6304
Numeric value of the second root
   1.0598
Numeric value of the third root
   -0.3451 - 1.0778i
Numeric value of the fourth root
   -0.3451 + 1.0778i

Note that the last two roots are complex.

Solving high-order equations in Octave

The following example solves the fourth-order equation x 4 − 7x 3 + 3x 2 − 5x + 9 = 0.

Create a script file and enter the following code-

v = [1, -7,  3, -5, 9];
s = roots(v);
% Convert the roots to double type
disp('Numeric value of the first root'), disp(double(s(1);
disp('Numeric value of the second root'), disp(double(s(2);
disp('Numeric value of the third root'), disp(double(s(3);
disp('Numeric value of the fourth root'), disp(double(s(4);

When the file is run, it returns the following results-

Numeric value of the first root
 6.6304
Numeric value of the second root
-0.34509 + 1.07784i
Numeric value of the third root
-0.34509 - 1.07784i
Numeric value of the fourth root
 1.0598

Solving systems of equations in MATLAB

solveThe function can also be used to generate solutions for systems of equations involving multiple variables. Let's take a simple example to demonstrate this usage.

Let's solve the equation-

5x + 9y = 5

3x - 6y = 4

Create a script file and enter the following code-

s = solve('5*x + 9*y = 5','3*x - 6*y = 4');
s.x
s.y

When running the file, it displays the following result-

ans =
   22/19
ans =
   -5/57

Similarly, you can solve larger linear systems. Consider the following set of equations-

x + 3y -2z = 5

3x + 5y + 6z = 7

2x + 4y + 3z = 8

Octave system of equations solution

We have different methods to solve linear equations with n unknowns. Let's take a simple example to demonstrate this usage.

Let's solve the equation-

5x + 9y = 5

3x - 6y = 4

Such a linear equation system can be written as a single matrix equation Ax = b, where A is the coefficient matrix, b is the column vector containing the right-hand side of the linear equation, and x is the column vector representing the solution, as shown below: The following program displays-

Create a script file and enter the following code-

A = [5, 9; 3, -6];
b = [5;4];
A \ b

When running the file, it displays the following result-

ans =
   1.157895
  -0.087719

Similarly, you can solve larger linear systems as follows-

x + 3y -2z = 5

3x + 5y + 6z = 7

2x + 4y + 3z = 8

Expansion and collection of equations in MATLAB

expandandcollectThey are used separately to expand and collect an equation. The following examples demonstrate the concept-

When using many symbolic functions, it should be declared that the variables are symbolic.

Create a script file and enter the following code-

syms x % Symbolic variable x
syms y % Symbolic variable y
%Expand the equation
expand((x-5)*(x+9))
expand((x+2)*(x-3)*(x-5)*(x+7))
expand(sin(2*x))
expand(cos(x+y))
 
%Collect the equation
collect(x^3 *(x-7))
collect(x^4*(x-3)*(x-5))

When running 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

expansion and collection of equations in a harmonic series

You need to have asymbolicpackage, which provides respectivelyexpandandcollectfunctions to expand and collect equations. The following examples demonstrate the concept-

When using many symbolic functions, it should be declared that the variables are symbolic variables, but the method of defining symbolic variables in Octave is different. Note the use ofSinandCosThey are also defined in the symbolic package.

Create a script file and enter the following code-

%First, load the package to ensure it is installed.
pkg load symbolic
%Make the symbols module available
symbols
%Define symbolic variables
x = sym('x');
y = sym('y');
z = sym('z');
%Expand the equation
expand((x-5)*(x+9))
expand((x+2)*(x-3)*(x-5)*(x+7))
expand(Sin(2*x))
expand(Cos(x+y))
 
%Collect the equation
collect(x^3 *(x-7), z)
collect(x^4*(x-3)*(x-5), z)

When running 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

factorThe function factors an expressionsimplifyThe function simplifies an expression. The following examples demonstrate the concept-

Example

Create a script file and enter 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 running 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