next up previous
Next: Linear second order equations Up: Maple and differential equations Previous: Introduction

Linear first-order equations

Consider the following differential equation.

displaymath377

The solution, obtained by separation of variables (or otherwise) is

displaymath378

where C is a constant. The Maple command to solve this differential equation is given below, as well as the output.

  > dsolve(diff(x(t),t) = -k*x(t) ,x(t));

displaymath379

The first thing to notice is that the Maple command needs you to specify explicitly in a couple of places that x depends on t. We don't always do this when writing down differential equations by hand, but Maple often needs more guidance.

Actually Maple isn't quite that stupid, because the following shortcut does work.

  > dsolve(diff(x(t),t) = -k*x ,x(t));

displaymath379

However, if you replace either of the remaining occurrences of x(t) with x you will get an error. To avoid problems, I would stick with using x(t) all the time. You can never go wrong by underestimating the intelligence of Maple!

Another thing to notice is that Maple uses tex2html_wrap_inline409 to stand for the constant C. Putting the underscore in front is just Maple's way of minimizing the risk of a conflict with a variable you have set. You might try to see what Maple does if you give the variable _C1 a value.

The final thing to notice is that the output of the dsolve command is an equation, with the expression for the solution appearing on the right-hand side of the equation. If you need this expression for plotting or further manipulation, you will have to somehow extract it from the equation. If you are using dsolve to solve a single differential equation, then using the rhs command is the easiest way to accomplish this.

To use the rhs command, you have to be able to access the output of the dsolve command. The best way to do this is to give the dsolve command a label, as in the following example. It usually makes sense to also provide a label for the expression you've extracted, so it will be easy to plot it, differentiate it, or otherwise use it.

  > sol1 := dsolve(diff(x(t),t) = -k*x(t) ,x(t));

displaymath381

  > x1 := rhs(sol1);

displaymath382

You can also specify initial conditions in the dsolve command. For example, we could solve the IVP

displaymath383

with the following command.

  > sol2 := dsolve({diff(x(t),t) = -k*x(t),x(0) = 120},x(t));

displaymath384

Note that you have to put the differential equation and the initial condition together and enclose them in curly braces ({}).

The simple example we have been using includes a parameter k. Before we can plot the solution we have to set its value. If you are going to set one value for k and not change it, then you can just use a command like the first one below. The other command just shows that the value of k has been set.

  > k := 1/4;

displaymath385

  > x1;

displaymath386

However, sometimes you want to give a parameter several values, perhaps to plot the solution for different parameter values or because you need the general form of the solution for something else. In these cases, setting the values of parameters is best done with the subs command, as shown below. (The first command just unsets the value of k so it is treated as a variable again.)

  > k := 'k';

displaymath387

  > subs(k=1/4,x1);

displaymath386

The final example in this section demonstrates how to plot the solution to the IVP

displaymath383

which we labeled above with the name sol2. For the plot command to work, Maple has to be able to evaluate the expresssion being plotted to a number. This means that the value of k has to be set. The command below shows how to use the subs command to do this.

  > plot(subs(k=1/4,rhs(sol2)),t=0..5);

next up previous
Next: Linear second order equations Up: Maple and differential equations Previous: Introduction

William W. Farr
Thu Oct 24 13:25:12 EDT 1996