Introduction to ODEs in Maple

This is a short overview of the most relevant commands for differential equations, and some illustrations of what we have seen in sections 1.1, 2.1 and 2.2.

Maple can solve differential equations. Sometimes it can do them symbolically, finding the general solution. If the equation can not be solved symbolically, one can always find a numerical answer.


To Load the Differential equations package and the ODE plot package type

 > with(DEtools):

 > with(plots):

Analytical Solutions

The command to solve differential equations is dsolve: To solve the differential equation y'=x-y, we type

 > dsolve(D(y)(x)=x-y,y(x));


We can also solve Initial Value Problems. The commands are the same, but we have to specify initial conditions.

 > dsolve({D(y)(x)=x-y,y(0)=1},y(x));

The equations and/or the initial conditions are allowed to depend on parameters.

 > dsolve({D(y)(x)=a*x-y,y(0)=b},y(x));

The built-in solver DSolve is quite powerful. It can solve simple separable equations such as y'=y*y:

 > dsolve(D(y)(x)=y^2,y(x));

An interesting thing to try is to do the command "infolevel[dsolve]:=2;". This will give some information about how Maple goes about solving the equation. Try this and see if you would have used the same techniques.

It is amazing, but true, that a lot of differential equations cannot be solved. Here is an example: y'=x*x-siny

 > dsolve(D(y)(x)=x^2-sin(y),y(x));

When maple gives up, it produces no output.


Numerical Solutions

In cases like this, we can find a numerical solution. Now we have to specify the initial conditions and the x-range where we want the solution, and tell Maple to use the numeric package

 > sol :=dsolve({D(y)(x)=x^2-sin(y(x)),y(0)=1},y(x),type=numeric);
   
sol := proc(rkf45_x) ... end
Maple gives the solution as a procedure. The "rkf45" indicates that it will use the Runge-Kutta method to solve the equation.

We can evaluate the soultion at a particular point by doing

 > sol(2);


Plotting solutions

We can plot solutions, direction fields, we can even plot the general solution, by varying the free constant.

To plot the numerical solution we obtained above, we write

 > odeplot(sol,[x,y(x)],0..5);

To plot a solution, that was obtained from a symbolic computation, we use plot. We first must extract the function, and then if there are any parameters in the solution, we need to specify them, before plotting, as plot only deals with numbers.

 > sol := dsolve({x*D(y)(x)+2*y(x)=4*x^2,y(1)=c},y(x));

 > f :=  unapply(op(2,sol),(x,c));

 > plot(f(x,2),x=0.001..3,y=0..10);

More easily one can plot the solution directly using DEplot

 > DEplot(x*D(y)(x)+2*y(x)=4*x^2,y(x),x=0..3,{[1,2]},y=0..10);

 > flist:= seq(f(x,c),c=-5..5):

 > plot({flist},x=-3..3,y=-10..10);

Or for a specific list of c values, we can specify them

 > flist:= seq(f(x,c),c={-3,-1,1,Pi,2.45}):

 > plot({flist},x=-3..3,y=-10..10);

We can also plot Direction Fields. For this we can use the command dfieldplot(diffeq, vars, trange, <options>)--this is really a version of DEplot1--see below.

 > dfieldplot(D(y)(x) = (4*x^2-2*y)/x,y(x),x=-3..3,y=-10..10);

We can combine the direction field with the solutions if the equation can be solved. For this we use the slightly more general command DEplot1

 > DEplot1(D(y)(x) = (4*x^2-2*y)/x,y(x),x=-3..3,{[-1,2],[-1,3],[1,2],[1,3]},y=-10..10)
;

Another example where we combine the solutions with the direction field, is given below.

 > eq := D(y)(x)=-2*y+exp(-x);

 > DEplot1(eq,y(x),x=-2..4,{[1,-2],[1,-1],[1,0.1],[1,0.5],[1,2],[1,3],[1,4]},y=-5..5);


Exercises

Here are a few exercises to try out, using the above examples. 1) Look at x*y'+ y=exp(x), Examine the solutions of this equation. Graph the solutions for different values of the arbitrary constant. Notice that you cannot specify initial conditions at x=0, since p(x) and g(x) (What are they?) are not continuous at 0.

2) If you try to solve the Initial Value Problem consisting of the equation in exercise 1 and the initial condition y(0)=1, what does Maple reply? Make sense out of this, using the graphs from exercise 1 and the existence theorem we saw in class.

3) Solve the equation y'-2 x*y = x with the following initial conditions: a) y(0)=0, b) y(0)=-1/2 c) y(0)=-1

4) Do problem number 3, but use the numerical option, on the x-interval (-2,2). Make plots of the solutions in exercise 3 and of the corresponding numerical solutions. Compare.

5) Do an exact computation for y'=-y*y, y(0)=1/3 Now solve this problem numerically and compare the results.


James D. Meiss
Tue Aug 6 1996