MATLAB Algebra (original) (raw)

Last Updated : 9 Feb, 2021

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.

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 + xy + 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: