Linear Differential Equations With Mathcad: Conference Paper
Linear Differential Equations With Mathcad: Conference Paper
net/publication/236029487
CITATIONS READS
0 4,321
1 author:
Nicolae Dăneţ
Technical University of Civil Engineering of Bucharest
48 PUBLICATIONS 78 CITATIONS
SEE PROFILE
All content following this page was uploaded by Nicolae Dăneţ on 22 May 2014.
NICOLAE DĂNEŢ
Technical University of Civil Engineering of Bucharest
Department of Mathematics and Computer Science
124, Lacul Tei Blvd., Bucharest, Romania
E-mail: [email protected]
Abstract. The paper shows how Mathcad software can be used for solving linear differential
equations symbolically and numerically.
Keywords: Ordinary differential equations, linear differential equations, Cauchy problem, initial
value problem, boundary value problem, Mathcad.
1. INTRODUCTION
Unlike other mathematical software like Maple, Mathematica and Mupad, Mathcad
does not have special functions to solve an ordinary differential equation (ODE for
short) symbolically. This does not mean that Mathcad cannot be used to solve ODEs.
In fact, Mathcad has an internal function called odesolve for solving ODEs, but this
function only works numerically.
If one wants to obtain a symbolic solution for an ODE with Mathcad, then they
must write their own Mathcad file for solving that type of ODE. Doing this work they
can understand better the method of solving instead of using a “black box” like in
other software.
In this paper we show how Mathcad can be used for solving linear differential
equations symbolically and numerically. Section 2 is devoted to linear differential
equations with constant coefficients. In Subsection 2.1, after we recall the algorithm
for determining the general solution of the homogeneous equation, we present a
Mathcad file which solves such an equation symbolically along with a given Cauchy
problem. In Subsection 2.2 we recall the method of variation of parameters for
determining a particular solution for the nonhomogeneous equation and present an
example in Mathcad. In Section 3 we show how the Mathcad function odesolve can
be used to solve numerically nonhomogeneous linear differential equations with
variable coefficients. Finally, an example of solving a system of linear differential
equations with odesolve is presented.
defined by
L( y ) = y ( n ) + a1 y ( n −1) + L + a n −1 y '+ a n y . (3)
Then the equation (2) can be written in the abridged form
L( y ) = f . (4)
If the right side f = 0 on I , then the equation is called homogeneous since it is
homogeneous in the unknown function y and its derivatives. For f ≠ 0 on I , the
equation is called nonhomogeneous.
In this section we recall the basic facts about the general solution of the
homogenous linear equation
L( y ) = y ( n ) + a1 y ( n −1) + L + an −1 y '+ a n y = 0 . (5)
If y1 , y 2 , K , y n are n solutions of the equations (5), they are said to form a
fundamental set of solutions for the homogeneous linear equation (5) if they are
linearly independent on the interval I . It is well known that the functions
y1 , y 2 ,K , y n ∈ C n ( I ) form a fundamental set of solutions for the equation (5) if and
only if the determinant
y1 ( x ) y2 ( x ) L yn ( x )
y1′ ( x ) y 2′ ( x ) L y n′ ( x )
W ( y1 , y 2 , K , y n )( x ) = , (6)
M M O M
y1( n −1) ( x ) y 2( n −1) ( x ) L y n( n −1) ( x )
called the Wronskian of the solutions y1 , y 2 , K , y n , is never zero on I .
The general solution of the homogeneous linear equation (5) is a linear
combination of a fundamental set of solution, that is
y ( x ) = C1 y1 ( x ) + C2 y 2 ( x ) + L + Cn y n ( x ) . (7)
λx
For the equation (5) we seek solutions of the form y = e λ is a real or
, where
λx
2 λx
complex number to be determined. Then it follows that y ' = λe , y ' ' = λ e ,…,
y ( n ) = λn e λx . By substituting these expressions in (5), we obtain
L( e λx ) = e λx (λn + a1λn −1 + L + a n −1λ + an ) = e λx P( λ ) , (8)
for all λ , where
P(λ ) = λn + a1λn −1 + L + an −1λ + an . (9)
78
The polynomial P( λ ) is called the characteristic polynomial, and the equation
P(λ ) = λn + a1λn −1 + L + an −1λ + a n = 0 (10)
is called the characteristic equation of the differential equation (5).
λx
The equality (8) shows that the function y = e is a solution of the differential
equation (5) if and only if P( λ ) = 0 , that is, λ is a root of the characteristic equation.
If all the roots λ1 , λ2 , K , λn of the characteristic equation (10) are real and
distinct, that is, the characteristic polynomial P( λ ) can be written in the form
P(λ ) = (λ − λ1 )(λ − λ2 )L (λ − λn ) ,
then we found n linearly independent solutions e
λ1 x
, e λ 2 x ,K , e λ n x of the equation
(5). Consequently,
y = C1e λ1 x + C2 e λ 2 x + L + Cn e λ n x , (11)
where Ci are arbitrary real constants, is the general solution of the equation (5).
If all the roots of the characteristic polynomial are real but there are multiple roots
among them, let us suppose that λ1 , λ2 , K , λm are the distinct roots, where m < n ,
and that each root λi has the multiplicity ki , i = 1,2, K , m , with k1 + L + k m = n .
Then the characteristic polynomial (9) has the form
P(λ ) = (λ − λ1 ) k1 ( λ − λ2 ) k 2 L (λ − λm ) k m .
In this case, to each root λi of multiplicity ki , correspond ki solutions of the equation
(5):
e λi x , xe λi x ,K , x k i −1e λi x .
All these solutions are linearly independent (see [2], p.102) and, consequently, the
general solution of equation (4) is of the form
m
y = ∑ (C0i + C1i x + L + Ck i −1.i x k i −1 ) e λi x , (12)
i =1
where C0i , C1i , K , C k i −1,i are arbitrary real constants.
Since the coefficients of the characteristic polynomial (9) are real, the complex
roots of the characteristic equation appear only as conjugate pairs λ1,2 = α ± iβ .
( α + iβ ) x (α − iβ ) x
The complex solutions e and e that correspond to these complex
roots can be replaced by two real solutions: the real and the imaginary part of one of
the solutions
e(α + iβ ) x = eα x (cos β x + i sin β x ) , e(α − iβ ) x = eα x (cos β x − i sin β x ) .
79
Thus, to the pair of complex conjugate roots λ1,2 = α ± iβ there correspond two
real solutions of the equation (5):
eα x cos β x , eα x sin β x .
If the characteristic equation has a multiple complex root λ1 = α + iβ of
multiplicity k , the complex conjugate λ2 = α − iβ is also a root with the same
multiplicity. The k complex solutions
e(α + iβ ) x , xe (α + iβ ) x , x 2 e(α + iβ ) x ,K , x k −1e(α + iβ ) x
that correspond to one of them may be replaced with 2 k real-valued solutions, the
real and the imaginary parts of the above complex solutions:
eα x cos β x , xeα x cos β x ,K , x k −1eα x cos β x ⎫
⎬ (13)
eα x sin β x , xeα x sin β x , K , x k −1eα x sin β x ⎭
Thus, to a pair of complex conjugate roots λ1, 2 = α ± iβ of multiplicity k there
correspond 2 k linearly independent solutions (13) ([2], p.116). Hence, the general
solution of the equation (5) can always be expressed as a linear combination of n
real-valued solutions.
2.1.4. The Cauchy Problem
The Cauchy problem for the equation (5) means to find the solution of this
equation which satisfies the given initial conditions
y ( x0 ) = y0 , y ' ( x0 ) = y '0 ,L , y ( n −1) ( x0 ) = y0( n −1) , (14)
( n −1)
where x0 is any point in the interval I and y0 , y '0 ,L , y0 are n arbitrary real
numbers.
If we demand that the general solution (7) of the equation (5) satisfies the initial
conditions (14), then we obtain an algebraic system of n linear equations
⎧ C1 y1 ( x0 ) + C2 y 2 ( x0 ) + L + Cn y n ( x0 ) = y0 ,
⎪ C y '(x ) + C y ' (x ) + L + C y ' (x ) = y ,
⎪ 1 1 0 2 2 0 n n 0 0
⎨ (15)
⎪ L L L L L L L L L L L L L L L M
L L L L
⎪⎩C1 y1( n −1) ( x0 ) + C2 y 2( n −1) ( x0 ) + L + Cn y n( n −1) ( x0 ) = y0 ,
with n unknowns C1 , C 2 , K , C n , and with a nonzero determinant, since the
determinant of this system is the Wronskian of a fundamental set of solutions of
equation (5). Thus, this system is solvable for Ci , for any given point x0 in the
interval I and for any kinds of right members.
2.1.5. Symbolic Solution with Mathcad
In this subsection we present a Mathcad file which determines the general solution
of a homogeneous linear differential equation with constant coefficients and solves
an associated Cauchy problem.
The presentation used the following conventions:
a) The paragraphs of text correspond to the text regions in Mathcad.
80
b) The yellow zones correspond to math regions in Mathcad used to write a
mathematical expression in order to make the text more clear. The equal sign
used in these expressions is the Boolean equal (CTRL + =).
c) The light blue zones indicate that the content of that math region did not
change automatically when the initial equation is changed and that these
changes must be made manually.
Example 2.1. a) Find the general solution of the following differential equation
( 4) ( 3) (16)
y +y − 7 ⋅ y'' − y' + 6 ⋅ y 0
b) Given the initial conditions
y (0) = 1, y ' (0) = 0, y ' ' (0) = −2, y ' ' ' (0) = −1 (17)
find the particular solution of the equation (16) which satisfies these conditions.
First we define the equation’s order, n , and the vector of the coefficients of the
equation denoted by a . This vector has the length n + 1 and the first component
must be the coefficient of the unknown function y .
T
n := 4 a := ( 6 −1 −7 1 1 )
The characteristic equation is:
n
P ( λ ) := ∑
j 2 3 4
aj⋅ λ → 6 − λ − 7 ⋅ λ + λ + λ
j =0
The roots of the characteristic equation are the components of the vector r
obtained as follows:
⎛ 1 ⎞
⎜ 2
r := P ( λ ) solve , λ → ⎜ ⎟
⎜ −3
⎝ −1 ⎠
Now we compute the multiplicity of each root. In the same time we obtain the
exponents for the powers of x which appear in the fundamental set of solutions.
k( i , r) := k←0
j←0
while j ≤ i
k ← k + 1 if r j − ri 0
j← j+1
return k
i := 0 .. n − 1
i= ri = k( i , r) =
0 1 1
1 2 1
2 -3 1
3 -1 1
81
y( x) := for i ∈ 0 .. n − 1
k ( i , r ) −1 Re( r i) ⋅x
yi ← x ⋅e ⋅ sin ( Im ( ri) ⋅ x) if Im ( ri) > 0
k ( i , r ) −1 Re( r i) ⋅x
yi ← x ⋅e ⋅ cos ( Im ( ri) ⋅ x) if Im ( ri) < 0
k ( i , r ) − 1 r i ⋅x
yi ← x ⋅e if Im ( ri) 0
y
This set is:
⎛ exp( x) ⎞
⎜
exp( 2 ⋅ x) ⎟
y( x) → ⎜
⎜ exp( −3 ⋅ x) ⎟
⎜
⎝ exp( −x) ⎠
The general solution of the equation is a linear combination of the above
fundamental set of solutions
n −1
Y( x , C) := ∑ Ci+ 1⋅ y( x) i
i =0
That is
Y( x , C) → C1⋅ exp ( x) + C2⋅ exp ( 2 ⋅ x) + C3⋅ exp ( −3 ⋅ x) + C4⋅ exp ( −x) (18)
Now we want to find the particular solution yp(x ) of the equation which at the
point
x0 := 0
satisfies the initial conditions
yp( x0) y0 yp' ( x0) y'0 yp'' ( x0) y''0 yp''' ( x0) y'''0
where
y0 := 1 y'0 := 0 y''0 := −2 y'''0 := −1
We introduce these conditions in a vector denoted ic, from “initial conditions”.
⎛ y0 ⎞ ⎛ 1 ⎞
⎜
⎜ 0
ic := ⎜
y'0 ⎟
ic = ⎜ ⎟
⎜ y''0 ⎟
⎜ ⎜ −2
⎝ y'''0 ⎠ ⎝ −1 ⎠
The Wronski matrix corresponding to the fundamental set of solutions is
⎡ y1( x) y2( x) y3( x) y4( x) ⎤
⎢ ⎥
y1 ' ( x) y2 ' ( x) y3 ' ( x) y4 ' ( x)
MW( x) ⎢ ⎥
⎢ y1 '' ( x) y2 '' ( x) y3 '' (x) y4 '' ( x) ⎥
⎢ ⎥
⎣ y1 ''' ( x) y2 ''' ( x) y3 ''' (x) y4 ''' ( x) ⎦
To obtain this matrix we define the fundamental set of solutions and its derivatives:
82
⎛ exp( x) ⎞ y'( x) := for j ∈ 0 .. n − 1
⎜
exp( 2 ⋅ x) ⎟
y( x) := ⎜ y'j ←
d1
⎜ exp(−3 ⋅ x) ⎟ 1
y( x) j
⎜ dx
⎝ exp( −x) ⎠ y'
y'' ( x) := for j ∈ 0 .. n − 1 y''' ( x) := for j ∈ 0 .. n − 1
d2 d3
y'' j ← y( x) j y''' j ← y( x) j
2 3
dx dx
y'' y'''
Then the Wronski matrix is
T
MW( x) := augment( y( x) , y'( x) , y'' ( x) , y''' ( x) )
⎛ exp( x) exp( 2 ⋅ x) exp( −3 ⋅ x) exp( −x) ⎞
⎜
exp ( x) 2 ⋅ exp ( 2 ⋅ x) −3 ⋅ exp( −3 ⋅ x) −exp( −x) ⎟
MW( x) → ⎜
⎜ exp( x) 4 ⋅ exp(2 ⋅ x) 9 ⋅ exp( −3 ⋅ x) exp( −x) ⎟
⎜ exp( x)
⎝ 8 ⋅ exp ( 2 ⋅ x) −27 ⋅ exp ( −3 ⋅ x) −exp ( −x) ⎠
The determinant of this matrix is called the Wronskian of the fundamental set of
solutions and is denoted by W (x ) .
W ( x) := MW( x)
The value of this determinant at any point is nonzero
W ( x) simplify → 240 ⋅ exp( −x)
In the given point x0, the Wronskian is
⎛1 1 1 1 ⎞
⎜1 2 −3 −1
MW( 0) → ⎜ ⎟
⎜1 4 9 1
⎝1 8 −27 −1 ⎠
and it has the value
W ( x0) → 240
The constants C1 , C2 , C3 , C 4 , for which the general solution (18) satisfies the
given initial conditions (17), are obtained from the linear system
⎛ C1 ⎞ ⎛ C1 + C2 + C3 + C4 ⎞ ⎛ 1 ⎞
⎜ ⎜ ⎜0
C2 C 1 + 2 ⋅ C 2 − 3 ⋅ C3 − C4
MW( 0) ⋅ ⎜ ⎟ ic → ⎜ ⎟
⎜ ⎟
⎜ C3 ⎟ ⎜ C 1 + 4 ⋅ C 2 + 9 ⋅ C3 + C4 ⎟
⎜ −2
⎜ ⎜ ⎝ −1 ⎠
⎝ C4 ⎠ ⎝ C1 + 8 ⋅ C2 − 27 ⋅ C3 − C4 ⎠
This system is solved using Mathcad function lsolve.
C := lsolve( MW( x0) , ic)
The solution is:
83
−2 −1
C →⎛ ⎞
T 11 5
⎝ 8 3 8 12 ⎠
Now we define the particular solution which satisfies the initial conditions (17).
n −1
yp( x) := ∑ Ci⋅ y( x) i
i =0
This solution is:
11 2 1 5
yp( x) → ⋅ exp ( x) − ⋅ exp ( 2 ⋅ x) − ⋅ exp( −3 ⋅ x) + ⋅ exp ( −x)
8 3 8 12
Finally, we plot this solution (see Figure 1).
yp( x)
0 0.5 1 1.5
1
x
FIGURE 1. The particular solution which satisfies the Cauchy problem in Example 2.1.
y0 ( x ) = C1 y1 ( x ) + C2 y 2 ( x ) + L + Cn y n ( x ) . (21)
y p ( x ) = C1 ( x ) y1 ( x ) + C2 ( x ) y 2 ( x ) + L + Cn ( x ) y n ( x ) , (22)
⎧ C1′ ( x ) y1 ( x ) + C 2′ ( x ) y 2 ( x ) + L + C n′ ( x ) y n ( x ) = 0,
⎪ C ′ ( x ) y ′ ( x ) + C ′ ( x ) y ′ ( x ) + L + C ′ ( x ) y ′ ( x ) = 0,
⎪ 1 1 2 2 n n
⎨ (23)
⎪ LLLLLLLLLLLLLLLLLLLLLL
⎪⎩C1′ ( x ) y1( n −1) ( x ) + C 2′ ( x ) y 2( n −1) ( x ) + L + Cn′ ( x ) y n( n −1) ( x ) = f ( x ).
The determinant of this system is precisely the Wronskian (6) of the fundamental set
of solutions y1 , y 2 , K , y n of the corresponding homogeneous equation (5), and this
determinant is nonzero for each value of x . Hence it is possible to determine the
functions C1′ ( x ), C 2′ ( x ),K , C n′ ( x ) . Then, by integrating, we find
Ci ( x ) = ∫ Ci′( x )dx + Ci , i = 1,2,K , n, (24)
where Ci are the new constants of integration.
r := P ( λ ) solve , λ → ⎜
⎛ i⎞
⎝ −i ⎠
85
k( i , r) := k←0
j←0
while j ≤ i
k ← k + 1 if r j − ri 0
j← j+1
return k
i := 0 .. n − 1 i = ri = k( i , r) =
0 i 1
1 -i 1
y( x) := for i ∈ 0 .. n − 1
Re( r i) ⋅x
⋅ sin ( Im ( ri) ⋅ x) if Im ( ri) > 0
k ( i , r ) −1
yi ← x ⋅e
Re( r i) ⋅x
⋅ cos ( Im ( ri) ⋅ x) if Im ( ri) < 0
k ( i , r ) −1
yi ← x ⋅e
r i ⋅x
if Im ( ri)
k ( i , r ) −1
yi ← x ⋅e 0
y
For this equation the fundamental set of solutions is:
y( x) → ⎜
⎛ sin ( x) ⎞
⎝ cos ( x) ⎠
Then, the general solution of the homogeneous linear equation (26) is:
n −1
y0( x , C) := ∑ Ci+ 1 ⋅ y( x) i
i =0
y0( x , C) → C1 ⋅ sin ( x) + C2 ⋅ cos ( x) (27)
⎡ y1( x) y2( x) ⎤
MW( x) ⎢ ⎥
⎣ y1 ' ( x) y2 ' ( x) ⎦
To obtain this matrix we define the fundamental set of solutions and its derivatives.
86
⎛ sin ( x) ⎞ y'( x) := for j ∈ 0 .. n − 1
y( x) := ⎜
⎝ cos ( x) ⎠
d1
y'j ← y( x) j
1
dx
y'
The matrix and its determinant are:
MW( x) := augment( y( x) , y'( x) )
T ⎛ sin ( x) cos ( x) ⎞
MW( x) → ⎜
⎝ cos ( x) −sin ( x) ⎠
W ( x) := MW( x) W ( x) simplify → −1
The particular solution of the nonhomogeneous equation (25) is sought under the
form
yp( x) C1 ( x) ⋅ y( x) 0 + C2 ( x) ⋅ y( x) 1
The derivatives of the functions C1(x) and C2(x) are obtained from the linear
algebraic system
C1' ( x) ⋅ y( x) 0 + C2' ( x) ⋅ y( x) 1 0,
C1' ( x) ⋅ y'( x) 0 + C2' ( x) ⋅ y'( x) 1 f ( x)
We resolve this system using Cramer rule and symbolic computation in Mathcad. We
have:
⎛ 0 y( x) 1 ⎞
⎜
C1' ( x) :=
⎝ f( x) y'( x) 1 ⎠ simplify → 1
W ( x)
and
⎛ y( x) 0 0 ⎞
⎜
C2' ( x) :=
⎝ y'( x) 0 f ( x) ⎠ simplify → −sin (x)
W ( x) cos ( x)
By integrating these derivatives we obtain the desired functions C1(x) and C2(x).
⌠ ⌠
C1 ( x) := ⎮ C1' ( x) dx → x C2 ( x) := ⎮ C2' ( x) dx → ln( cos ( x) )
⌡ ⌡
Now we can define the particular solution of the nonhomogeneous equation:
yp( x) := C1 ( x) ⋅ y( x) 0 + C2 ( x) ⋅ y( x) 1
This solution is:
yp( x) → x⋅ sin ( x) + ln( cos ( x) ) ⋅ cos ( x) (28)
Then the general solution of the nonhomogeneous equation (25) is the sum of the
general solution (27) of the corresponding homogeneous equation (26) and the
particular solution (28) of the nonhomogeneous equation (25):
Y( x , C) := y0( x , C) + yp( x).
Hence, we have
Y( x , C) → C1 ⋅ sin ( x) + C2 ⋅ cos ( x) + x⋅ sin ( x) + ln( cos ( x) ) ⋅ cos ( x) (29)
87
Third step. We now solve the following Cauchy problem: given the point
π
x0 :=
4
find the particular solution of the equation (25) which satisfies the initial conditions
yp( x0) y0 yp'( x0) y'0
where
π⋅ 2 π⋅ 2
y0 := y'0 := (30)
16 16
We define the initial conditions in a vector denoted by ic:
⎛ 1 ⎞
⎜ 1 2
⎜ ⋅π ⋅2 ⎟
ic := ⎜
⎛ y0 ⎞ 16 ⎛ 0.278 ⎞
ic → ⎜ ⎟ =⎜
⎝ y'0 ⎠ ⎜ 1
⎟ ⎝ 0.278 ⎠
1
⎜ ⋅π ⋅2
2
⎝ 16 ⎠
The particular solution (28) has the derivative
d
yp'( x) := yp( x)
dx
yp'( x) → x⋅ cos ( x) − ln( cos ( x) ) ⋅ sin ( x)
The values of the solution yp(x ) and its derivative yp ' ( x ) calculated at the given
point x0 are defined in a vector denoted by b:
⎛ 1 ⎛ 1⎞ 1 ⎞
⎜ 1 2 1 ⎜1 2 2
⎜ ⋅ π ⋅ 2 + ⋅ ln⎜ ⋅ 2 ⋅ 2 ⎟
⎛ yp(x0) ⎞ 8 2 ⎝2 ⎠ ⎛ 0.310 ⎞
b := ⎜ b→⎜ ⎟ =⎜
⎝ yp'(x0) ⎠ ⎜ 1 ⎛ 1⎞ 1
⎟ ⎝ 0.800 ⎠
1 1 ⎜ 1 2
⎜ 2
⋅ π ⋅ 2 − ⋅ ln⎜ ⋅ 2 ⋅ 2
2
⎝ 8 2 ⎝2 ⎠ ⎠
The constants C1 , C2 are obtained from the algebraic linear system
⎛ 1 1 ⎞ ⎛ 1 ⎛ 1⎞ 1 ⎞
⎜ 1 2 1 2 ⎜ −1 2 1 ⎜1 2 2
⎜ ⋅ 2 ⋅ C1 + ⋅ 2 ⋅ C2 ⎟ ⎜ ⋅ π ⋅ 2 − ⋅ ln⎜ ⋅ 2 ⋅ 2 ⎟
⎛ C1 ⎞ 2 2 16 2 ⎝2 ⎠
MW( x0) ⋅ ⎜ ic − b → ⎜ ⎟ ⎜ ⎟
⎝ C2 ⎠ ⎜ 1 1
⎟ ⎜ 1 ⎛⎜ 1⎞ 1
⎟
1 2 1 2 −1 1 1 2
⎜
⋅ 2 ⋅ C1 − ⋅ 2 ⋅ C2 ⎜ 2
⋅ π ⋅ 2 + ⋅ ln⎜ ⋅ 2 ⋅ 2
2
2 ⎝ 2 ⎠ ⎝ 16 2 ⎝2 ⎠ ⎠
which has a non zero determinant
W ( x0) → −1
The solution of this system is
C := lsolve( MW( x0) , ic − b)
⎛ ⎛ 1⎞ ⎞
T
⎜ −1 ⎜ 1 2
C →⎜ ⋅ π −ln⎜ ⋅ 2 = ( −0.393 0.347 )
⎝ 8 ⎝2 ⎠⎠
88
Then, the particular solution of the equation (25) which satisfies the given initial
conditions (30) is:
n −1
ypc( x) := ∑ Ci⋅ y( x) i + yp( x)
i =0
⎛ 1⎞
−1 ⎜1 2
ypc( x) → ⋅ π ⋅ sin ( x) − ln⎜ ⋅ 2 ⋅ cos ( x) + x⋅ sin ( x) + ln( cos ( x) ) ⋅ cos ( x)
8 ⎝2 ⎠
In order to avoid some mistakes in the above computations we check with
Mathcad if the obtained solution ypc (x ) satisfies the equation (25) and the initial
conditions (30).
d2 1
ypc( x) + ypc ( x) simplify →
2 cos ( x)
dx
1
1 2
ypc( x0) → ⋅π⋅2
16
d
ypc' ( x) := ypc ( x)
dx
1
1 2
ypc' ( x0) simplify → ⋅π ⋅2
16
Now we plot this solution. First the graph is plotted on the interval [-10,10], which is
the default plot interval for X-Y Plot (see Figure 2).
10
8
6
4
2
ypc( x)
10 8 6 4 2 0 2 4 6 8 10
2
4
6
8
10
x
FIGURE 2. The graph on the interval [-10,10 ] of the particular solution which
satisfies the Cauchy problem in Example 2.2.
The lack of the curve on some intervals is due to the fact that the solution exists
only on the intervals where cos( x ) > 0 .
The graph of this solution on the interval ( −π / 2, π / 2 ) , which contains the initial
point x0 = π / 4 , is shown in Figure 3.
89
3
2.5
ypc( x) 1.5
0.5
FIGURE 3. The graph on the interval (-π/2, π/2) of the particular solution which
satisfies the Cauchy problem in Example 2.2.
In this section we show how the Mathcad function odesolve can be used to obtain
a solution for a linear differential equation when its coefficients are functions and not
numbers, that is, if the equation has the form
It is well known that in this case there is not a general theory for determining the
general solution for the homogeneous equation
90
Example 3.1. Find the solution of the equation
1
x ( x + 1) y ' '+( x + 2) y '− y = x + , (33)
x
which satisfies the initial conditions
y (1) = 5 / 2 , y ' (1) = 9 / 2 . (34)
d d2
dx 2
dx
or using prime notation similar to y ' ( x ) . The keystroke for the prime character is
CTRL + F7.
Even if Mathcad computes y (x ) numerically, we can use the result like a “true”
function of variable x , for x between the initial value a = 1 and the final value
b = 4 . This function has a derivative at every point in the interval [a, b] and can be
integrated on every subinterval [c, d ] ⊆ [a, b] . For example
3
d ⌠
y( 3.5) = 9.874 y'( x) := y( x) y'( 3.5) = 2.575 ⎮ y( x) dx = 7.252
dx ⌡2
Figure 4 shows the graph of the function y (x ) and its derivative y ' ( x ) .
15
y( x) 10
y'( x)
5
1 2 3 4
x
FIGURE 4. The graphs of the solution and its derivative obtained in Example 3.1.
The analytical solution of the equation (33) which satisfies the initial conditions (34)
is (see [4], Problem 702, p.58):
91
2 ⎛x ⎞ 3
ye( x) := x + 2 − + ⎜ + 1 ⋅ ln( x )+
x ⎝2 ⎠ 2
Then we can graphically compare the numeric solution y (x ) obtained using Mathcad
function odesolve and the exact solution ye(x ) . The Figure 5 shows that for this
equation the graphs of the two functions almost coincide.
15
y( x) 10
ye( x)
5
y( x) 1.5
s ( x)
1
0.5
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x, x
FIGURE 6. The graph of the numerical solution obtained in Example 3.2 with odesolve
function and the graph of the exact solution.
The Mathcad function odesolve can be used not only to solve differential
equations but also for systems of differential equations. The following example shows
how odesolve can be used to compute the solution of a system of two linear
differential equations.
Example 3.3. Find the solution of the nonhomogeneous linear system of
differential equations
⎧⎪ x ' (t ) = y (t ) + 2 e t ,
⎨ (37)
⎪⎩ y ' (t ) = x (t ) + t2,
([4], Problem 826, p.73), which satisfies the initial conditions
x (0) = 0, y ( 0 ) = 1. (38)
In Mathcad we have:
Given
d t
x( t) y( t) + 2⋅ e
dt
d 2
y( t) x( t) + t
dt
x( 0) 0 y( 0) 1
⎛ x ⎞ := Odesolve⎡⎛ x ⎞ , t , 2 , 200⎤
⎜ ⎢⎜ ⎥
⎝ y⎠ ⎣⎝ y ⎠ ⎦
The solution is computed on the interval [1, 2] divided in 200 subintervals. Figure 7
shows the graphs of the functions x (t ) and y (t ) which form the solution of this
system.
93
25
20
x( t) 15
y( t) 10
5
30 20
x( t) 20 y( t)
10
xx( t) yy( t)
10
4. REFERENCES
94