Products & Services Solutions Academia Support User Community Company

Learn more about Symbolic Math Toolbox   

Solving Equations

Solving Algebraic Equations

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. If you need to solve equations of the form f(x)  =  q(x), you must use quoted strings. In particular, the command

s = solve('cos(2*x) + sin(x) = 1')

returns a vector with three solutions.

s =
         0
     pi/6
 (5*pi)/6

There are also solutions at each of these results plus for integer k, as you can see in the MuPAD solution:

Several Algebraic Equations

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 = sym('alpha');

There are several ways to address the output of solve. One is to use a two-output call

[x, y] = solve(x^2*y^2, x-y/2 - alpha)

which returns

x = 
 alpha
     0
 
y = 
          0
 (-2)*alpha

Modify the first equation to x2y2 = 1 and there are more solutions.

eqs1 = 'x^2*y^2=1, x-y/2-alpha';
[x,y] = solve(eqs1)

produces four distinct solutions:

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
 
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. In particular, consider the system u^2 - v^2 = a^2, u + v = 1, a^2 - 2*a = 3. The command

S = solve('u^2 - v^2 = a^2', 'u + v = 1', 'a^2 - 2*a = 3')

returns

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 simultaneous equations can also be solved using matrix division. For example,

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

results in

sol =
 (2*v)/3 - (5*u)/3
     (4*u)/3 - v/3

z =
 (2*v)/3 - (5*u)/3
     (4*u)/3 - v/3

Thus s and z produce the same solution, although the results are assigned to different variables.

Single Differential Equation

The function dsolve computes symbolic solutions to ordinary differential equations. The equations are specified by symbolic expressions containing the letter D to denote differentiation. The symbols D2, D3, ... DN, correspond to the second, third, ..., Nth derivative, respectively. Thus, D2y is the toolbox equivalent of d2y/dt2. The dependent variables are those preceded by D and the default independent variable is t. Note that names of symbolic variables should not contain D. The independent variable can be changed from t to some other symbolic variable by including that variable as the last input argument.

Initial conditions can be specified by additional equations. If initial conditions are not specified, the solutions contain constants of integration, C1, C2, etc.

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 or place the output in a structure whose fields contain the solutions of the differential equations.

Example 1

The following call to dsolve

dsolve('Dy=t*y')

uses y as the dependent variable and t as the default independent variable.

The output of this command is

ans =
C5*exp(t^2/2)

y = C*exp(t^2/2) is a solution to the equation for any constant C.

To specify an initial condition, use

y = dsolve('Dy=t*y', 'y(0)=2')

This produces

y = 
2*exp(t^2/2)

Notice that y is in the MATLAB workspace, but the independent variable t is not. Thus, the command diff(y,t) returns an error. To place t in the workspace, enter syms t.

Example 2

Nonlinear equations may have multiple solutions, even when initial conditions are given:

x = dsolve('(Dx+x)^2=1','x(0)=0')

results in

x = 
 1/exp(t) - 1
 1 - 1/exp(t)

Example 3

Here is a second-order differential equation with two initial conditions, and the default independent variable changed to x. The commands

y = dsolve('D2y=cos(2*x)-y','y(0)=1','Dy(0)=0', 'x');
simplify(y, 400)

produce

ans =
1 - (2*(cos(x) - 1)^2)/3

Example 4

The key issues in this example are the order of the equation and the initial conditions. To solve the ordinary differential equation

with x as the independent variable, type

u = dsolve('D3u=u','u(0)=1','Du(0)=-1','D2u(0) = pi','x')

Use D3u to represent d3u/dx3 and D2u(0) for .

u =
(pi*exp(x))/3 - (cos((3^(1/2)*x)/2)*(pi/3 - 1))/exp(x/2) ...
- (3^(1/2)*sin((3^(1/2)*x)/2)*(pi + 1))/(3*exp(x/2))

Further ODE Examples

This table shows a few more examples of differential equations and their Symbolic Math Toolbox syntax. The final entry in the table is the Airy differential equation, whose solution is referred to as the Airy function.

Differential Equation

MATLAB Command

y(0) = 1

y = dsolve('Dy+4*y = exp(-t)', 'y(0) = 1')

2x2y′′ + 3xy′ – y = 0
( ′ = d/dx)

y = dsolve('2*x^2*D2y + 3*x*Dy - y = 0','x')

(The Airy equation)

y = dsolve('D2y = x*y','y(0) = 0', 'y(3) = besselk(1/3, 2*sqrt(3))/pi', 'x')

Several Differential Equations

The function dsolve can also handle several ordinary differential equations in several variables, with or without initial conditions. For example, here is a pair of linear, first-order equations.

S = dsolve('Df = 3*f + 4*g', 'Dg = -4*f + 3*g')

The computed solutions are returned in the structure S. You can determine the values of f and g by typing

f = S.f
g = S.g
f =
C5*cos(4*t)*exp(3*t) + C1*sin(4*t)*exp(3*t)
 
g =
C1*cos(4*t)*exp(3*t) - C5*sin(4*t)*exp(3*t)

If you prefer to recover f and g directly as well as include initial conditions, type

[f,g] = dsolve('Df=3*f+4*g, Dg =-4*f+3*g', 'f(0) = 0, g(0) = 1')
f =
sin(4*t)*exp(3*t)
 
g =
cos(4*t)*exp(3*t)
  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS