Related Articles

# MATLAB – Algebra

• Last Updated : 09 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:

• 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.

My Personal Notes arrow_drop_up