0% found this document useful (0 votes)
55 views

MATLAB - Algebra

The document discusses how to solve algebraic equations, including linear, quadratic, and higher order equations, using the solve and roots functions in MATLAB and Octave. It also covers how to expand, collect, factorize, and simplify algebraic expressions. Key points include: - The solve function in MATLAB and roots function in Octave can be used to find solutions to basic algebraic equations. - Both can also solve quadratic and higher order equations, returning the roots in an array. - Functions like expand collect factor simplify can manipulate algebraic expressions symbolically. - Systems of linear equations can be solved using solve or matrix operations like A\b in Octave.

Uploaded by

Mourya Shashank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

MATLAB - Algebra

The document discusses how to solve algebraic equations, including linear, quadratic, and higher order equations, using the solve and roots functions in MATLAB and Octave. It also covers how to expand, collect, factorize, and simplify algebraic expressions. Key points include: - The solve function in MATLAB and roots function in Octave can be used to find solutions to basic algebraic equations. - Both can also solve quadratic and higher order equations, returning the roots in an array. - Functions like expand collect factor simplify can manipulate algebraic expressions symbolically. - Systems of linear equations can be solved using solve or matrix operations like A\b in Octave.

Uploaded by

Mourya Shashank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

MATLAB - Algebra

Solving Basic Algebraic Equations in MATLAB


The solve function is used for solving algebraic equations. In its simplest form, the solve function takes
the equation enclosed in quotes as an argument.
Example-1: let us solve for x in the equation x-5 = 0 solve('x-5=0')
ans = 5
Example-2: solve('x-5')
ans = 5
We can solve equation by this method: solve(equation, variable) where, you can also mention the
variable.
Example-3: solve the equation v – u – 3t2 = 0, for v.
solve('v-u-3*t^2=0', 'v')
ans = 3*t^2 + u
Solving Basic Algebraic Equations in Octave
The roots function is used for solving algebraic equations in Octave and you can write above examples
as follows:
Example-4: solve for x in the equation x-5 = 0
roots([1, -5])
ans = 5
Solving Quadratic Equations in MATLAB
The solve function can also solve higher order equations. It is often used to solve quadratic equations.
The function returns the roots of the equation in an array.
Example-5: solve the quadratic equation x2 -7x +12 = 0.
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));
ans = The first root is: 3
The second root is: 4
Solving Quadratic Equations in Octave
Example -6: solves the quadratic equation x2 -7x +12 = 0 in Octave.
eq= x2 -7x +12 = 0;
roots([1, -7, 12]);
disp('The first root is: '), disp(s(1)); disp('The second
root is: '), disp(s(2));
ans = The first root is: 4
The second root is: 3
Solving Higher Order Equations in MATLAB
Example-7: solve a cubic equation as(x-3)2(x-7) = 0 solve('(x-3)^2*(x-7)=0')
ans = 3
3
7
We can get the numerical value of long value roots by converting them to double.
Example-8: solves the fourth order equation x4 − 7x3 + 3x2 − 5x + 9 = 0.
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)));
ans = The first root is: 6.63039633239071843148505321895
The second root is: 1.059780463302589629168277249985
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
Solving Higher Order Equations in Octave
Example-8: Solve the fourth order equation x4 − 7x3 + 3x2 − 5x + 9 = 0.
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)));
ans = 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
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.
Example-9: solve the equations:5x + 9y = 5,3x – 6y = 4
s= solve('5*x + 9*y = 5','3*x - 6*y = 4');
s.x
s.y
ans = 22/19
ans = -5/57
Solving System of Equations in Octave
Example-10: 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.
A = [5, 9; 3, -6];
b = [5;4];
A\b
ans = 1.157895
-0.087719
Expanding and Collecting Equations in MATLAB
The expand and the collect function expands and collects an equation respectively.When you work with
many symbolic functions, you should declare that your variables are symbolic.
Example-11:
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))
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^47*x^3
ans = x^6 - 8*x^5 + 15*x^4
Expanding and Collecting Equations in Octave
Octave has different approach to define symbolic variables. Sin and Cos, which are defined in symbolic
package.

Example-12:
% 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)
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
Example-13: The factor function factorizes an expression and the simplify function simplifies an
expression.
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))
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

You might also like