Tutorial on Solving Ordinary Differential Equations
M.R.S. Kulenovic
University of Rhode Island
Getting Started
If you are on a PC, then you normally start Maple V by clicking on the maple leaf icon. This Maple V worksheet (m244nb1.mws) may be loaded by clicking on File and then Open which brings up the Open dialog from which you choose this file and click on OK . This Maple V Release 5 program may be run one Maple command at a time by putting the cursor on the command and pressing the Enter (or Return ) key. The entire program may be run by clicking on Edit and then Execute Worksheet . This worksheet may be sent to your printer by clicking on File and then Print , which brings up the Print dialog window.
You can learn more about the commands
in this tutorial or other Maple V commands by using the extensive
help that is available while in Maple. Simply click on Help , choose
Topic Search (or Full Text Search ), enter the command, and click OK .
You may also enter ?commandname at the Maple V prompt. It is useful
to note that the default plot size is larger than those in this tutorial.
In order to obtain the smaller graphs (and save paper) click on the plotand
the graph will be enclosed in a box. Youcan then resize the graph by dragging
one of the small dots at the corner of the box.
Goals
In this tutorial you will learn
some tools for solving and graphing first and second order differential
equations using Maple V. The methods will be illustrated by solving
five problems.
Problem 1.
Find the general solution of the
DE
'
=
,
and
then find the particular solution satisfying the initial condition x(0)
= 1. Verify that your particularsolution is a solution of the initial condition
and the differential equation. Plot a slope (direction) field and include
the trajectories for the solutions ic := x(0) = 1; passing through the
points (-2,3), (-2,-3), (0,1), (0,-1).
Solution:
> de1 := diff(x(t),t) = t^2 - x(t);
![]()
Here is the general solution:
gensoln1 := dsolve(de1,x(t));
![]()
Here is the initial condition:
> ic := x(0) = 1;
![]()
and the corresponding particular solution:
> partsoln1 := dsolve({de1,ic},x(t)):
in its simplified form:
> partsoln1 := simplify(%);
![]()
We now verify that this solution satisfies the initial condition and the differential equation.
> subs(partsoln1,ic);
![]()
> subs(partsoln1,de1);
![]()
> eval(%);
![]()
To verify that the general solution satisfies the differential equation we can execute the previous two steps as one:
> eval(subs(gensoln1,de1));
![]()
Finally, we plot the direction field and include the solution curves for the four initial conditions.
> with(DEtools):
> DEplot(de1,x(t), t=-4..4, {[-2,1],[-2,-3],[0,1],[0,-1]},
title=`Solution x(t) of x' = t^2 - x`, axesfont=[TIMES,BOLD,10],
titlefont=[TIMES,BOLD,12], labelfont=[HELVETICA,BOLD,10],
linecolor=green, arrows=THIN, x=-4..4);
![[Maple Plot]](mt244nb111.gif)
Problem 2.
Use Maple to find the general
solution of the following separable differential equation.
Graph the direction field for this differential equation and include in the plot the two solution curves passing through the points (0,0) and (1,1).
Solution:
> restart;
> de2 := diff(y(x),x)=(x+sin(x))/(y(x)-cos(y(x)));
![]()
> gensoln2 := dsolve(de2,y(x));
![]()
Thus, the solution is given in implicit form. In order to plot this solution, we need to first let Maple know that we want to think of the solution as a function. This can be done with the following command.
> f := unapply(lhs(gensoln2)-_C1,x,y);
![]()
The solution is given by f(x,y) = c , where c is determined by the initial condition. Let's see what the contours of this function look like.
> with(plots):
> contourplot(f(x,y),x=-3..3,y=-2..2,contours=12,titlefont=[TIMES,BOLD,12], title=`Contour Plot of f(x,y)`, coloring=[red,green]);
![[Maple Plot]](mt244nb116.gif)
Now let's plot the direction field of de2 together with the solution curves passing through the points (0,0) and (1,1).
> with(DEtools):
> DEplot(de2,y(x),x=-3..3,{[0,1.],[0,0]},
title=`Plot of Two Solutions of de2`, axesfont=[TIMES,BOLD,10], titlefont=[TIMES,BOLD,12],
labelfont=[HELVETICA,BOLD,10], color=magenta,
linecolor=yellow, arrows=THIN, y=-2..2);
![[Maple Plot]](mt244nb117.gif)
Problem 3.
The first example will be to solve a second order linear differential equation with constant coefficients. The steps used here are the same as one might use when working a problem by hand. In this example we solve the following differential equation:
Subsequently, we wish to find the solution of the differential equation which satisfies the initial values:
y(0) = 2, y'(0) = 0, y''(0) = 4.
Solution:
> de3 := diff(y(x),x$3)-3*diff(y(x),x$2)-6*diff(y(x),x)+8*y(x)=0;
![[Maple Math]](mt244nb119.gif)
The
characteristic equation is obtained by replacing the n th order derivative
by
.
> cheq3 := subs({diff(y(x),x$3)=r^3,diff(y(x),x$2)=r^2,diff(y(x),x)=r,y(x)=1},de3);
![]()
The first step in solving the problem is to find the roots of the characteristic equation.
> chroots:=solve(cheq3,r);
![]()
Once we know the characteristic roots we can obtain a basis for the general solution. When we enclose a sequence like 1, - 2, 4 with square brackets, [..], we create a Mapleobject known as a list.A Maplelist preserves order of data in the way you specify it to be. Next we apply a Maple function known as map to the list [1, - 2, 4]. This Maple function applies the same operation to each component in a data structure. We now apply maptwice to obtain a base of solutions for the homogeneous solution.
> map( (x,y) -> x*y,[chroots],x);
![]()
> base1 := map(exp,%);
![]()
We can now write the general solution:
> gensoln3 := sum(c[i]*base1[i],i=1..3);
![]()
In order to solve the initial value problem we need to solve for the constants by solving the equations:
y(0) = 2, y'(0) = 0, y''(0) = 4.
Maplefunctions that do this are as follows:
> y0 := subs(x=0,gensoln3);
![]()
> dy0 := subs(x=0,diff(gensoln3,x));
![]()
> ddy0 := subs(x=0,diff(gensoln3,x$2));
![]()
> partsoln3 := solve({y0=2,dy0=0,ddy0=4},{c[1],c[2],c[3]});
![]()
Using the above result we can now write the particular solution to the initial value problem.
> y1part := subs(partsoln3, gensoln3);
![]()
The easiest way to solve these kinds of problems is usually with function dsolve. For example the general solution is given by
> dsolve(de3,y(x));
![]()
The solution to that satisfies the initial conditions
y(0) = 2, y'(0) = 0, y''(0) = 4
can be obtained by
> y1part:=dsolve({de3,y(0)=2,D(y)(0)=0,(D@@2)(y)(0)=4},y(x));
![]()
Now, if we want to define the solution, which is given as a function, we use unapply and rhs.
> y1p := unapply(rhs(y1part),x);
![]()
This leads to the solution which can be evaluated or plotted. For example:
> y1p(2.0);
![]()
> plot(y1p(x),x=-2..2);
![[Maple Plot]](mt244nb135.gif)
Problem 4.
Consider the following second order Euler equation
Using the Maple command dsolve, find the general solution of this differential equation. Then find the solution satisfying the initial conditions y(1) = a and y'(1) = b, where a and b are constants. Finally, investigate how the solution y(t) varies for a in [0,6] and b in [1,7] by plotting the solution for y(1)= a, a = 0...6, and y'(1) = -1, and for y(1) = 2 and y'(1) = b, b = 1...7.
Solution:
> restart;
> de4 := t^2*diff(y(t),t$2) + t*diff(y(t),t) + y(t) = log(t) + t;
![[Maple Math]](mt244nb137.gif)
> dsolve(de4,y(t));
Let us define the initial condition:
> ic4 := y(1)=a, D(y)(1)=b;
![]()
> soln4 := dsolve({de4,ic4},y(t));
The following command converts soln4 to a function of (t,a,b).
> ysoln4 := unapply(rhs(soln4),t,a,b);
The next two plot commands use the sequence operator $ to plot seven curves on one graph.
> plot({ysoln4(t,a,-1)$a=0..6},t=0.1..2.5,
title=`y(t) for y(1) = a, a=0...6, and y'(1) = -1`, axesfont=[TIMES,BOLD,10],
titlefont=[TIMES,BOLD,12],
labelfont=[HELVETICA,BOLD,10]);
![[Maple Plot]](mt244nb145.gif)
> plot({ysoln4(t,2,b)$b=1..7},t=0.1..2.5,
title=`y(t) for y(1) = 2, and y'(1) = b, b= 1...7`, axesfont=[TIMES,BOLD,10],
titlefont=[TIMES,BOLD,12],
labelfont=[HELVETICA,BOLD,10]);
![[Maple Plot]](mt244nb146.gif)
Some of The Maple V Commands Used in This Section
List of Maple V commands, or options, or variables used in this section can be obtained by searching Help file:subs, diff, solve, mapsum, dsolve, unapply(rhs. The commands are listed in order of appearance.