Algebra is all about the study of mathematical symbols and rules for manipulating these symbols. Here variables are used to store/represent quantities. In MATLAB, there are certain in-built methods to solve algebra equations. Roots of equations, solving for x, simplifying an expression are some things we can perform in MATLAB.
Following are some functions of MATLAB to solve algebra:
- solve()
- roots()
- expand()
- simplify()
- factor()
Solving Equations: To solve an equation for finding x value or if the equation contains multiple variables, we can solve for a particular variable.
Syntax: solve(equation,variable)
Here default variable will be x.
Example 1: This example illustrates the solve() function for the latest versions of MATLAB.
MATLAB
% MATLAB program to illustrate
% solve() function
syms x
% Below eqn is nothing but
% x-28 = 0
eqn = x-28 == 0;
S = solve(eqn,x);
disp(S)
Output:
28
Example 2:
MATLAB
% MATLAB program to illustrate
% solve() function
syms x y z
% Here solving the equation for y
eqn = solve(x-y+28*z^2,y);
disp(eqn)
Output:
y =
28*z^2 + x
Finding Roots: Using roots() and solve() functions, one can find roots using an equation or coefficients of a variable in a particular equation. Let's see an example to get a better understanding
Syntax: roots(p)
where p = column vector
Example 1:
MATLAB
% MATLAB program to find roots
% of a quadratic equation
% Finding roots for the equation 'x-28=0'
roots([1,-28])
% Finding roots for the equation 'x^2-7*x+12=0'
roots([1,-7,12])
Output:
ans =
28
ans =
4
3
Example 2:
MATLAB
% MATLAB program to find roots
% using solve function
syms x
a = solve(x-28)
b = solve(x^2 -7*x + 12)
% The solve function can also
% higher order equations
c = solve((x-3)^2*(x-7)==0)
Output:
a =
28
b =
3
4
c =
3
3
7
Factorization and Simplification: To find factors of an expression, factor() function is used. And to simplify an expression, simplify() function is used. When you work with many symbolic functions, you should declare that your variables are symbolic.
factor Syntax:
factor(expression)
or
factor(integer)
simplify Syntax:
simplify(expression)
If an expression is passed into factor() function, then it returns an array of factors. If an integer is passed into factor() function, then it returns prime factorization of the given integer.
Example 1:
MATLAB
% MATLAB program to illustrate
% factor function to find factors
syms x
syms y
% Finding prime factorization of
% given integer
factor(28)
% Finding factors of
% given expressions
factor(x^3-y^3)
factor(x^6-1)
Output:
ans =
2 2 7
ans =
(x - y)*(x^2 + x*y + y^2)
ans =
(x - 1)*(x + 1)*(x^2 + x + 1)*(x^2 - x + 1)
Example 2: The simplify() function performs algebraic simplification of the given expression.
MATLAB
% MATLAB program to illustrate
% factor function
a = simplify(sin(x)^2 + cos(x)^2)
b = simplify((x^4-16)/(x^2-4))
Output:
a =
1
b =
x^2 + 4
Expand Function: Using the expand() function, we can expand given expressions and simplify inputs of functions using identities.
Syntax:
expand(expression)
Example:
MATLAB
% MATLAB function to illustrate expand()
% function and expand the given polynomials
expand((x - 2)*(x - 4))
% Simplifying inputs of functions
% by using identities
expand(cos(x + y))
expand(sin(2*x))
Output:
ans =
x^2 - 6*x + 8
ans =
cos(x)*cos(y) - sin(x)*sin(y)
ans =
2*cos(x)*sin(x)
Note:
- For the latest versions of MATLAB, solve() function parameter should not be a string. Instead, use symbolic variables in equations.
- In the latest versions of MATLAB, equation parameter of solve() should be like x-28==0 instead of x-28=0. We should use conditional operator (i.e ==) instead of assignment operator (i.e =)
- In MATLAB editor, we don't need to write print functions unless you put a semicolon(;) at the end of a command.
- While working with numerous symbolic functions, we must declare symbolic variables.
- While solving polynomials default solving variable will be x, unless you specify any other variable.
Similar Reads
Algebra of Matrices Matrices are the arrangement of numbers or any other mathematical elements in the form of rectangular arrays. Algebra of Matrices denotes the various algebraic operations performed on a matrix.Algebra of Matrices includes operations such as Addition, Subtraction, Multiplication, transpose, negative,
14 min read
Class 12 NCERT Solutions- Mathematics Part I - Chapter 3 Matrices - Exercise 3.4 | Set 2 Matrices are a fundamental concept in mathematics providing a compact way to represent and manipulate data and equations. They are used in various fields including algebra, calculus, and applied mathematics. This chapter focuses on the operations and properties of matrices essential for solving the
12 min read
Class 12 NCERT Solutions- Mathematics Part I - Chapter 3 Matrices - Exercise 3.2 Matrices are a fundamental concept in linear algebra, used extensively in mathematics, physics, engineering, computer science, and various other fields. A matrix is a rectangular array of numbers, symbols, or expressions arranged in rows and columns. The numbers or elements inside a matrix are enclo
15+ min read
Practice Questions on Subtraction of Matrices Subtraction of Matrices is one of the operations that are performed between two matrices. It is similar to addition of matrices. This article provides practice questions on subtraction of matrices along with solved examples and concepts related to it. This article serves as a one stop solution for p
10 min read
Addition of Matrices Worksheet : Solved and Unsolved Addition of Matrices Worksheet will help students grasp the concept of matrix addition, improve their problem-solving skills, and prepare them for more advanced topics in mathematics.Matrices are a fundamental concept in mathematics, particularly in linear algebra. They are rectangular arrays of num
9 min read