MATLAB – Algebra
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:
Solving Equations: To solve an equation for finding x value or if the equation contains multiple variables, we can solve for a particular variable.
Here default variable will be x.
Example 1: This example illustrates the solve() function for the latest versions of MATLAB.
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
ans = 28 ans = 4 3
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.
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.
a = 1 b = x^2 + 4
Expand Function: Using the expand() function, we can expand given expressions and simplify inputs of functions using identities.
ans = x^2 - 6*x + 8 ans = cos(x)*cos(y) - sin(x)*sin(y) ans = 2*cos(x)*sin(x)
- 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.