| Contents | Index |
| On this page… |
|---|
If S is a symbolic expression,
solve(S)
attempts to find values of the symbolic variable in S (as determined by symvar) for which S is zero. For example,
syms a b c x S = a*x^2 + b*x + c; solve(S)
uses the familiar quadratic formula to produce
ans = -(b + (b^2 - 4*a*c)^(1/2))/(2*a) -(b - (b^2 - 4*a*c)^(1/2))/(2*a)
This is a symbolic vector whose elements are the two solutions.
If you want to solve for a specific variable, you must specify that variable as an additional argument. For example, if you want to solve S for b, use the command
b = solve(S,b)
which returns
b = -(a*x^2 + c)/x
Note that these examples assume equations of the form f(x) = 0. To solve equations of the form f(x) = q(x), use the operator ==. For example, this command
syms x s = solve(cos(2*x) + sin(x) == 1)
returns a vector with three solutions.
s =
0
pi/6
(5*pi)/6There are also solutions at each of these results plus kπ for integer k, as you can see in the MuPAD solution:

This section explains how to solve systems of equations using Symbolic Math Toolbox software. As an example, suppose you have the system

and you want to solve for x and y. First, create the necessary symbolic objects.
syms x y alpha
There are several ways to address the output of solve. One is to use a two-output call
[sol_x,sol_y] = solve(x^2*y^2 == 0, x-y/2 == alpha)
which returns
sol_x =
alpha
0
sol_y =
0
-2*alphaModify the first equation to x2y2 = 1. The new system has more solutions.
[sol_x,sol_y] = solve(x^2*y^2 == 1, x-y/2 == alpha)
produces four distinct solutions:
sol_x = alpha/2 + (alpha^2 + 2)^(1/2)/2 alpha/2 + (alpha^2 - 2)^(1/2)/2 alpha/2 - (alpha^2 + 2)^(1/2)/2 alpha/2 - (alpha^2 - 2)^(1/2)/2 sol_y = (alpha^2 + 2)^(1/2) - alpha (alpha^2 - 2)^(1/2) - alpha - alpha - (alpha^2 + 2)^(1/2) - alpha - (alpha^2 - 2)^(1/2)
Since you did not specify the dependent variables, solve uses symvar to determine the variables.
This way of assigning output from solve is quite successful for "small" systems. Plainly, if you had, say, a 10-by-10 system of equations, typing
[x1,x2,x3,x4,x5,x6,x7,x8,x9,x10] = solve(...)
is both awkward and time consuming. To circumvent this difficulty, solve can return a structure whose fields are the solutions. For example, solve the system of equations u^2 - v^2 = a^2, u + v = 1, a^2 - 2*a = 3:
syms u v a S = solve(u^2 - v^2 == a^2, u + v == 1, a^2 - 2*a == 3)
The solver returns its results enclosed in this structure:
S =
a: [2x1 sym]
u: [2x1 sym]
v: [2x1 sym]The solutions for a reside in the "a-field" of S. That is,
S.a
produces
ans = -1 3
Similar comments apply to the solutions for u and v. The structure S can now be manipulated by field and index to access a particular portion of the solution. For example, if you want to examine the second solution, you can use the following statement
s2 = [S.a(2), S.u(2), S.v(2)]
to extract the second component of each field.
s2 = [ 3, 5, -4]
The following statement
M = [S.a, S.u, S.v]
creates the solution matrix M
M = [ -1, 1, 0] [ 3, 5, -4]
whose rows comprise the distinct solutions of the system.
Linear systems of equations can also be solved using matrix division. For example, solve this system:
clear u v x y syms u v x y S = solve(x + 2*y == u, 4*x + 5*y == v); sol = [S.x; S.y] A = [1 2; 4 5]; b = [u; v]; z = A\b
sol =
(2*v)/3 - (5*u)/3
(4*u)/3 - v/3
z =
(2*v)/3 - (5*u)/3
(4*u)/3 - v/3Thus sol and z produce the same solution, although the results are assigned to different variables.
Use dsolve to compute symbolic solutions to ordinary differential equations. You can specify the equations as symbolic expressions containing diff or as strings with the letter D to indicate differentiation.
Before using dsolve, create the symbolic function for which you want to solve an ordinary differential equation. Use sym or syms to create a symbolic function. For example, create a function y(x):
syms y(x)
For details, see Creating Symbolic Functions.
To specify initial or boundary conditions, use additional equations. If you do not specify initial or boundary conditions, the solutions will contain integration constants, such as C1, C2, and so on.
The output from dsolve parallels the output from solve. That is, you can:
Call dsolve with the number of output variables equal to the number of dependent variables.
Place the output in a structure whose fields contain the solutions of the differential equations.
Suppose you want to solve the equation y'(t) = t*y. First, create the symbolic function y(t):
syms y(t)
Now use dsolve to solve the equation:
y(t) = dsolve(diff(y) == t*y)
y(t) = C2*exp(t^2/2)
y(t) = C2*exp(t^2/2) is a solution to the equation for any constant C2.
Solve the same ordinary differential equation, but now specify the initial condition y(0) = 2:
syms y(t) y(t) = dsolve(diff(y) == t*y, y(0) == 2)
y(t) = 2*exp(t^2/2)
Nonlinear equations can have multiple solutions, even if you specify initial conditions. For example, solve this equation:
syms x(t) x(t) = dsolve((diff(x) + x)^2 == 1, x(0) == 0)
results in
x(t) = exp(-t) - 1 1 - exp(-t)
Solve this second-order differential equation with two initial conditions. One initial condition is a derivative y'(x) at x = 0. To be able to specify this initial condition, create an additional symbolic function Dy = diff(y). (You also can use any valid function name instead of Dy.) Then Dy(0) = 0 specifies that Dy = 0 at x = 0.
syms y(x) Dy = diff(y); y(x) = dsolve(diff(y, 2) == cos(2*x) - y, y(0) == 1, Dy(0) == 0); y(x) = simplify(y)
y(x) = 1 - (8*(cos(x)/2 - 1/2)^2)/3
Solve this third-order ordinary differential equation:
![]()
![]()
Because the initial conditions contain the first- and the second-order derivatives, create two additional symbolic functions, Dy and D2y to specify these initial conditions:
syms u(x) Du = diff(u); D2u = diff(u, 2); u(x) = dsolve(diff(u, 3) == u, u(0) == 1, Du(0) == -1, D2u(0) == pi)
u(x) = (pi*exp(x))/3 - exp(-x/2)*cos((3^(1/2)*x)/2)*(pi/3 - 1) -... (3^(1/2)*exp(-x/2)*sin((3^(1/2)*x)/2)*(pi + 1))/3
This table shows examples of differential equations and their Symbolic Math Toolbox syntax. The last example is the Airy differential equation, whose solution is called the Airy function.
dsolve can handle several ordinary differential equations in several variables, with or without initial conditions. For example, solve these linear first-order equations. First, create the symbolic functions f(t) and g(t):
syms f(t) g(t)
Now use dsolve to solve the system. The toolbox returns the computed solutions as elements of the structure S:
S = dsolve(diff(f) == 3*f + 4*g, diff(g) == -4*f + 3*g)
S =
g: [1x1 sym]
f: [1x1 sym]To return the values of f(t) and g(t), enter these commands:
f(t) = S.f g(t) = S.g
f(t) = C2*cos(4*t)*exp(3*t) + C1*sin(4*t)*exp(3*t) g(t) = C1*cos(4*t)*exp(3*t) - C2*sin(4*t)*exp(3*t)
If you prefer to recover f(t) and g(t) directly, as well as include initial conditions, enter these commands:
syms f(t) g(t) [f(t), g(t)] = dsolve(diff(f) == 3*f + 4*g,... diff(g) == -4*f + 3*g, f(0) == 0, g(0) == 1)
f(t) = sin(4*t)*exp(3*t) g(t) = cos(4*t)*exp(3*t)
Suppose you want to solve a system of differential equations in a matrix form. For example, solve the system Y′ = AY + B, where A, B, and Y represent the following matrices:
syms x(t) y(t) A = [1 2; -1 1]; B = [1; t]; Y = [x; y];
Solve the system using dsolve:
S = dsolve(diff(Y) == A*Y + B); x = S.x y = S.y
x = 2^(1/2)*exp(t)*cos(2^(1/2)*t)*(C2 + (exp(-t)*(4*sin(2^(1/2)*t) +... 2^(1/2)*cos(2^(1/2)*t) + 6*t*sin(2^(1/2)*t) +... 6*2^(1/2)*t*cos(2^(1/2)*t)))/18) +... 2^(1/2)*exp(t)*sin(2^(1/2)*t)*(C1 - (exp(-t)*(4*cos(2^(1/2)*t) -... 2^(1/2)*sin(2^(1/2)*t) +... 6*t*cos(2^(1/2)*t) - 6*2^(1/2)*t*sin(2^(1/2)*t)))/18) y = exp(t)*cos(2^(1/2)*t)*(C1 - (exp(-t)*(4*cos(2^(1/2)*t) -... 2^(1/2)*sin(2^(1/2)*t) + 6*t*cos(2^(1/2)*t) -... 6*2^(1/2)*t*sin(2^(1/2)*t)))/18) - exp(t)*sin(2^(1/2)*t)*(C2 +... (exp(-t)*(4*sin(2^(1/2)*t) + 2^(1/2)*cos(2^(1/2)*t) +... 6*t*sin(2^(1/2)*t) + 6*2^(1/2)*t*cos(2^(1/2)*t)))/18)
![]() | Linear Algebra | Integral Transforms and Z-Transforms | ![]() |

See how symbolic computations can help you find analytical solutions to math and engineering problems.
Get free kit| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |