Differential Equations Matlab Help

Differential Equations
A first-order ordinary differential equation (ODE) can be written in the form dy dt = f(t, y)
where t is the independent variable and y is a function of t. A solution to such an equation is a function y = g(t) such that dgf dt = f(t, g), and the solution will contain one arbitrary constant. This constant becomes determined when we apply an additional condition of the solution by requiring that the solution have
a specified value y(t) when t = tl. The chosen value tl is often the smallest, or starting value, of t, and if so, the condition is called the initial condition (quite INITIAL’ often t) = 0). The general term for such a requirement is a boundary condition, CONDITION and MATLAB lets us specify conditions othe- than initial conditions. For example, we can specify the value of the dependent variable at t = tz. where ti > tl.
Methods for obtaining a numerical solution to differential equations were covered in Chapter 8. However, we prefer to obtain an analytical solution whenever possible because it is more general and thus more useful for designing engineering devices or processes .

A second-order ODE has the following form:
d 2 y ( dY dt ) 2 = f, t, y, dt
Its solution will have two arbitrary constants that can be determined once two additional conditions are specified. These conditions are often the specified values of y and’ d y / d t at t ::f O. The generalization to third-order and higher equations is straightforward  We will QCcasio’nally use the following abbreviations for the first- and second- ~rder derivatites  dy .. d2y Y = dt y = dt2
MATLAB provides the dsol ve function for solving ordinary differential equations. Its various forms differ according to whether they are used to solve single equations or sets of equations, whether or not boundary conditions are
specified; and whether or not the default independent variable t is acceptable. Note that t is the default independent variable and not x as with the other symbolic functions, The reason is that many ODE models of engineering applications have . time t as the independent variable, Solving a Single Differential Equation The dsol ve function’s syntax for solving a single equation is dsol ve ( I eqn ‘ ) . The function returns a symbolic solution of the ODE specified by the symbolic expression eqn. Use the uppercase letter D to represent the first
derivative, use D2to represent the second derivative, and so on. Any character immediately following the differentiation operator is taken to be the dependent variable. Thus Dw represents dw[dt , Because of this syntax, you cannot use , uppercase D as symbolic variable when using the dsol ve function. The arbitrary constants in the solution are denoted by Cl, C2, and so on. The number of such”coUstants is the same as the order of the ODE. For example, the equation . ‘,J . has the solution The solution can be found with the following session. Note that you-need not declare y to be symbolic prior to using dsol ve. »dsolve

(‘Dy+2*y=12’) ans = 6 +C 1 *exp (- 2 * t ).

There can be symbolic constants in the equation. For example,
dt = sin(at)
has the solution
y(t) = — + C) a
It can be found as follows:
»dsol ve (,Dye sin (a*t)·,)
ans = . .
\-c6s (a*t)+Cl*a) /a
Here is a second-order example:
~, d2y = 2
” dt? c y
The solution y(t) = Cse” +c-e:” can be found with the following session:
dsol ‘fe(,D2y=c”2 *y’ )
Cl*exp(-c*t) + C2*exp(c*t)
Solving ~ets of Equations
Sets of equations can be solved with dso 1ve.The appropriate syntax is dso 1ve (,eqnl’ ,,eqn2 ‘,… ).The function returns a symbolic solution of the set of equations specified by the symbolic expressions eqnl and eqn2.
For example, the set . .
dx dt =3x+4y
dy – = -4x+3y
dt has the solution x(t) = C)e3t cos4t +’C2e3t sin4t, y(t) = _C)e3t sin4t +
C2e3t cos 4t. The session is
»[x, y] = dsolve(‘Dx=3*x+4*y’, ‘Dy=-4*x+3*y’)
x = Cl*exp(3*t)*cos(4*t)+C2*exp(3*t)*sin(4*t)
y = -Cl*exp(3*t)*sin(4*t)+C2*exp(3*t)*cos(4*t)

Specifying Initial and Boundary Conditions’
Conditions on the solutions at specified values of the independent variable can be handled as follows. The form dsol ve (,eqn’, ,condl’, ,cond2  returns a symbolic solution of the ODE specified by the symbolic expression.
eqn, subject to the conditions specified in the expressions cond l,condz, and

y (a) = b, Dy (a) ~ c, D2y (a) = d, and so on. These correspond to y(a), y(a), ‘y(a), and so on. If the number of conditions is less than the order of the equation, the returned solution will contain arbitrary constants Cl, C2,and soon.

For example, the problem
-dy = sm. (b)t, Y(0)= 0
has the solution y(t) = [1 – cos(bt)]fb. It can be found as follows: »dsolve(‘Dy=sin(b*t) ‘,’y(O)=O’)
ans =
The problem
d2y 2
-2 = Cy, y(O) = 1, y(O) = 0
has the solution y(t) = (eCI +e-CI)f2. The session is
»dsolve(‘D2y=cA2*y’, ‘y(O)=l’, ‘Dy(O)=O’)
ans =
Arbitrary boundary conditions, such as y(O) = c, can be used. For example, the solution of the problem
dt +ay = b, y(O) = c
is b ( b) y(t) = ;;+ c -;; «:01
The session is
»dsolve(‘Dy+a*~=b’, ‘y(O)=c’)
Plotting the Solution
The ezplot function can be used to plot the solution, just as with any other symbolic expression, provided no undetermined constants such as Clare present.

For example, the problem
dy + lOy = 10+4 sin(4t), y(O) = 0
has the solution
y(t) = 1 – ~ cos(4t) + ~ sin(4t) – ~e-JOI

Plot of the solution of y + IOy = 10+4sin(4t), y(O) = o.

Plot of the solution of y + IOy = 10+4sin(4t), y(O) = o.

The session is
»y = dsolve(‘Dy+lO*y=lO+4*sin(4*t)’, ‘y(O)=O’)
y =
1-4/29*cos(4*t)+ 10/29*sin(4*t)-25/29*exp(-10*t)
»ezplot(y),axis([O 5 0 2))
The plot is shown in Figure 10.4-1.
Sometimes the ezplot function uses too few values of the independent variable and thus does not produce a smooth plot. To override the spacing chosen by the ezplot function, you can use the subs function to substitute an array of values for the independent variable. Note that you must define t to be a symbolic variable. For example, you can continue the previous session as follows:

»syrns t
»x = [0:0.05:5);
»P = subs(y,t,x);
»plot(x,P),axis([O 5 0 2)), xlabel(‘t’)
Equation Sets with Boundary Conditions Sets of equations with specified boundary conditions can be solved as follows. The function dsol ve (,eqnl ‘,,eqn2 ‘,… ,,condl ‘,,cond2

expressions eqnl, eqn2, and so on, subject to theinitial conditions specified in the expressions condl, cond2, and so on.

For example, the problem
– =3x+4y,
dy 4 “!II – =.- X +.Jy~
dt ‘
x(O) = 0
y(O) = 1
has the solution
x(t) =e” sin(4t), .y(t) =e3e cos(4i)
The session is
»dsolve(‘Dx=3*x+4*y’, ‘Dy=-4*x+3*y’, ‘x(O)=O’, ‘y(O)=l’)
[x,y) =. . ,
x = exp(3*t)*sin(4*t), y = exp(3*t)*coq(4*t) It is not necessary to specify only initial conditions. Theconditions can.be specified at different values of t. For example, to solve the problem
dt2 +’9y =0, y(O) = 1, Y(7r) = 2
the session is
»dsolve(‘D2y+9*y=O’, ‘y(O)=l’, ‘Dy(pi)=2’)
Using Other Independent Variables 

Although the default independent variable is t, you can use the following syntax to specify a different independent variable. The function dsol ve (,eqnl ‘, ,eqn2 ” … , ,condl ‘~,cond2 ” … , ,x “) returns a symbolic
solution of a set of equations where the independent variable is x. For example, the solution of the equation
‘-d.v + 2’v = 12 dx
is v(x) = 6 + C.e-2x• The session is
»dsolve(‘Dv+2*v=12’, ‘x’)
ans =

Test Your Understanding
110.4-1 Use MATLAB to solve the equation.
; +~2y = 0
Check the answer by hand or with MATLAB. (Answer: yet) = C] coS(bt) + C2 sin(~t).).
T10.4-2 Use MATLAB to solve the problem

-2 +b2y = 0, y(O) = 1. ycO) = ° dt
Check the answer by hand 0\ with MATLAB.
(Answer: yet) = cos(bt).)

Solving Nonlinear Equations
MATLAB can solve many nonlinear first-order differential equations. For example,
the problem ‘”
dy =4-l,
y(O) = 1 (10.4-1)
can be solved with the following session
»dsolve(‘Dy=4-yA2’, ‘y(O)=1’)
ans =
ans =
which is equivalent to
, ‘
3e4/.- 1
yet) = 21 + 3e41
Not all nonlinear equations can be solved in closed form. For example, the following equation is the equation of motion of a specific pendulum.
d2y . ‘
dt2 +9 SIn(Y) =0, y(O) = 1, .}o(O) =0
The following session generates a message that a solution could not be found.
»dsolve(‘D2y+9*sin(y)=O’, ‘y(O)=1’, ‘Dy(O)=O’)


Thus MATLAB was unable to find a closed-form solution. It is possible, however, that later versions of MATLAB will be able to solve this equation. Try it and see! Table 10.4-1 summarizes the functions for solving differential equations.

Posted on July 29, 2015 in Symbolic Processing with MATLAB

Share the Story

Back to Top
Share This