As of this section, we have seen all the examplesMATLABWords andGNU(Or called Octave). However, when it comes to solving basic algebraic equations, MATLAB and Octave are a bit different, so MATLAB and octave will be introduced separately.
forFactorizationAnd simplified algebraic expressions, we will also make contact.
Solve basic algebraic equations in MATLAB
Use the solve command in MATLAB to solve systems of algebraic equations. In its simplest form, solvefunctionNeed to be enclosed in quotes as parameter equations.
For example, let's solve x in the equation, x-5 = 0
solve('x-5=0')
MATLAB executes the above statement and returns the following results:
ans = 5
You can also call the solution function as:
y = solve('x-5 = 0')
MATLAB executes the above statement and returns the following results:
y = 5
Even the equation to the right that may not be included:
solve('x-5')
MATLAB executes the above statement and returns the following results:
ans = 5
However, if the formula involves multiple symbols, then by default, MATLAB, assuming that x is being resolved, the resolution command has another form:
solve(equation, variable)
There, variables can also be mentioned.
For example, let's solve the equation v - u - 3t2 = 0, or v In this case we should write this:
solve('v-u-3*t^2=0', 'v')
MATLAB executes the above statement and returns the following results:
ans = 3*t^2 + u
MATLAB solution is basicallyOctaveSystem of algebraic equations
The root command is used to solve the system of algebraic equations Octave . You can write the above example as follows:
For example, let's solve x in the equation, x-5 = 0
roots([1, -5])
Octave executes the above statement and returns the following results:
ans = 5
You can also call the solution function as:
y = roots([1, -5])
Octave executes the above statement and returns the following results:
y = 5
Solving quadratic equations in MATLAB
The solve command can also solve higher-order equations. It is often used to solve quadratic equations, and the function returnsArrayroot of equation in.
The following example is used to solve the quadratic equation x2 -7x +12 = 0.
Create a script file in MATLAB and enter the following code:
-
s = solve(x^2 -7*x + 12 == 0);
-
disp('The first root is: '), disp(s(1));
-
disp('The second root is: '), disp(s(2));
Run the file and display the following results:
The first root is: 3 The second root is: 4
Solve Octave quadratic equations
The following example solves the quadratic equation x2 -7x +12 = 0 in Octave. 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));
Run the file and display the following results:
The first root is: 4 The second root is: 3
Solving higher order equations in MATLAB
The solve command can also solve higher-order equations. For example, let's solve a cubic equation (x-3)2(x-7) = 0
solve([(x-3)^2*(x-7)=0])
MATLAB executes the above statement and returns the following results:
ans = 3 3 7
In the case of higher order equations, root length contains many terms. The values you can get are like roots, convert them into twice.
The following example solves the fourth-order equation x4 − 7x3 + 3x2 − 5x + 9 = 0.
Create a script file in MATLAB 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)); % converting the roots to double type disp('Numeric value of first root'), disp(double(s(1))); disp('Numeric value of second root'), disp(double(s(2))); disp('Numeric value of third root'), disp(double(s(3))); disp('Numeric value of fourth root'), disp(double(s(4)));
Run the file and return 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 first root 6.6304 Numeric value of second root 1.0598 Numeric value of third root -0.3451 - 1.0778i Numeric value of fourth root -0.3451 + 1.0778i
Note that the two roots in the past are plural.
Solving higher-order equations in Octave
The following example solves the fourth-order equation x4 − 7x3 + 3x2 − 5x + 9 = 0.
Create a script file and enter the following code:
v = [1, -7, 3, -5, 9]; s = roots(v); % converting the roots to double type disp('Numeric value of first root'), disp(double(s(1))); disp('Numeric value of second root'), disp(double(s(2))); disp('Numeric value of third root'), disp(double(s(3))); disp('Numeric value of fourth root'), disp(double(s(4)));
Run the file and return the following results:
Numeric value of first root 6.6304 Numeric value of second root -0.34509 + 1.07784i Numeric value of third root -0.34509 - 1.07784i Numeric value of fourth root 1.0598
Solve system of equations in MATLAB
The solve command can also be used to generate solutions for equation systems involving more than one variable.
We solve the equation:
5x + 9y = 5
3x – 6y = 4
Create a script file in MATLAB and enter the following code:
s = solve([5*x + 9*y = 5],[3*x - 6*y = 4]);
Run the file and display the following results:
ans = 22/19 ans = -5/57
In the same way, large linear systems can be solved.
Consider the following system of equations:
x + 3y -2z = 5
3x + 5y + 6z = 7
2x + 4y + 3z = 8
Octave equation solution system
We have a little different approach to solving the linear system of equations of the 'n' unknown number of the system 'n'.
Let's solve the equation:
5x + 9y = 5
3x – 6y = 4
A single matrix equation for a system of linear equations in such a system can be written as Ax = b, where A is a coefficient matrix, b is a column vector containing the right side of the system of linear equations, and x is a column vector, representing it as shown in the following program
Create a script file and type the following code:
A = [5, 9; 3, -6]; b = [5;4]; A b
Run the file and display the following results:
ans = 1.157895 -0.087719
Using the same method, a large linear system can be solved is given as follows:
x + 3y -2z = 5
3x + 5y + 6z = 7
2x + 4y + 3z = 8
MATLAB expansion and collection equations
In MATLAB, the expand and collect commands are used to expand and collect one equation respectively. The following example demonstrates the concept:
When there are many symbolic functions in your work, you should declare that your variables are symbolic.
Create a script file in MATLAB and enter 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))
Run the file and display the following results:
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
Octave extension and collection equations
You need the symbolic package, which provides the expand and collect commands to expand and collect equations. The following example demonstrates the concept:
When there are many symbolic functions in the work, variables should be declared to be symbolic, but octaves have different ways to define symbolic variables. Pay attention to using sin and cos, they are also symbolic in the package definition.
Create a script file and enter 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 running the file, it displays the following results:
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)
Decompose and simplify algebraic expressions
factor command expression factorizes is a simplified expression of simplified commands.
Specific examples
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))
Run the file and display the following results:
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