## Documentation Center |

This example shows how to solve algebraic and differential equations using Symbolic Math Toolbox™.

On this page… |
---|

The solve function can handle a wide variety of algebraic equations, including both single equations and systems of equations. By default, the solver always tries to return a complete set of solutions, including complex solutions and multiple branches. For example, the following polynomial has two real and two complex solutions.

```
syms x
xsol = solve(x^4 + x^3 - x == 1, x)
```

xsol = 1 -1 (3^(1/2)*i)/2 - 1/2 - (3^(1/2)*i)/2 - 1/2

Use indexing to select only the real solutions.

xsol(xsol == real(xsol))

ans = 1 -1

Some equations, such as those that include trigonometric elements, have an infinite number of solutions. For such equations, the solver returns only one solution from the infinite set.

solve(sin(x) == 1, x)

ans = pi/2

**Solving Systems of Algebraic Equations**

The solve function can also be used to solve systems of equations. Simply specify the system of equations and the variables to solve for.

syms x y [x2, y2] = solve(y + x^2 == 1, x - y == 10)

x2 = (3*5^(1/2))/2 - 1/2 - (3*5^(1/2))/2 - 1/2 y2 = (3*5^(1/2))/2 - 21/2 - (3*5^(1/2))/2 - 21/2

As with an individual equation, if a system has an infinite number of solutions, the solver returns only one solution.

[x3, y3] = solve(exp(x + y) == 1, x - y == 1)

x3 = 1/2 y3 = -1/2

In some cases, you may be able to express the system of equations in matrix form. For these cases, you can use MATLAB's backslash operator to solve the equations. Note that the result is a double precision number since the equations are solved numerically.

A = [1 2; 3 4]; b = [5 6]'; x = A\b

x = -4.0000 4.5000

You can use the 'sym' command to solve the system exactly.

sym(A)\b

ans = -4 9/2

**Solving Ordinary Differential Equations (ODEs)**

You can solve various types of ordinary differential equations using the dsolve function. For example, you can solve a simple 2nd order linear ODE.

```
syms y(t)
D2y = diff(y,2);
Dy = diff(y);
dsolve(D2y + Dy + y == 0)
```

ans = C5*exp(-t/2)*cos((3^(1/2)*t)/2) + C6*exp(-t/2)*sin((3^(1/2)*t)/2)

You can specify initial conditions or boundary conditions along with an ODE.

ysol = dsolve(D2y == 2*t/Dy, y(0) == 0, Dy(0) == 1)

ysol = (2^(1/2)*log(t + (t^2 + 1/2)^(1/2)))/4 - (2^(1/2)*log(2^(1/2)/2))/4 + (2^(1/2)*t*(t^2 + 1/2)^(1/2))/2

You can visualize the solution to your differential equation using the ezplot function.

ezplot(ysol)

Was this topic helpful?