Maple/Differential Equations/Old

From PrattWiki
Revision as of 22:00, 26 February 2024 by DukeEgr93 (talk | contribs) (Complicated Results)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Derivatives in Maple

Maple uses the diff command to calculate and represent derivatives. The first argument will be the variable or function of which you want the derivative, and the second and later arguments will be the differentiation variables. For example, to find:

\( \begin{align} a&=\frac{d}{dt}\left( e^{-t}\cos(\omega t-k x) \right)\\ b&=\frac{d}{dx}\left( e^{-t}\cos(\omega t-k x) \right) \end{align} \)

you could first define a variable to hold on to the function and then use the diff command to perform the required differentiation. Start a Maple worksheet with the following lines:

restart;
f:=exp(-t)*cos(omega*t-k*x);
a:=diff(f, t);
b:=diff(f, x);

Notice, among other things, that Maple properly renders the \(\omega\) and that it understands that \(f\) is a function of at least \(t\) and \(x\). You can take multiple derivatives of the same variable by appending a dollar-sign and the order of the derivative to the differentiation variable. For example, to complete:

\( \begin{align} c&=\frac{d^3}{dt^3}\left( e^{-t}\cos(\omega t-k x) \right) \end{align} \)

you should add:

c:=diff(f, t$3);

to the worksheet.

Ordinary Differential Equations in Maple

Since the diff function can be used to represent derivatives, it can also be used to define differential equations. For example, to solve the system:

\( \begin{align} \frac{dx}{dt}+x&=\cos(t)\\ x(0)&=1 \end{align} \)

you would start by defining an equation to represent the differential. Add the following to your Maple script:

deqn1:=diff(x(t), t)+x(t)=cos(t);

Note in this case that you must explicitly define the variable \(x\) to be a function of \(t\); otherwise, Maple will assume that the derivative of undefined variable \(x\) with respect to undefined variable \(t\) is simply 0!

Thus defined, you can solve for the system using Maple's dsolve function. This function takes two arguments - a set of equations (including initial conditions) to solve and a list of the variable(s) for which to solve. In this particular case, add the command:

dsolve({deqn1, x(0)=1}, [x(t)]);

and Maple will produce the answer:

\( \begin{align} x \left( t \right) &=1/2\,\cos \left( t \right) +1/2\,\sin \left( t \right) +1/2\,{e^{-t}} \end{align} \)

If you are solving second or higher order derivatives, or for a multiple variable system, you will need to provide initial values for the variables and some of their derivatives. For instance, to solve for the mathematical expression of a cannonball launched into a frictionless sky from some initial position (\(x_0\), \(y_0\)) at some initial velocity (\(u_0\), \(v_0\)), you can write:

acceqns := diff(x(t), t$2)=0, diff(y(t), t$2) = -g;

dsolve({acceqns, x(0)=x0, y(0)=y0, (D)(x)(0)=vx0, (D)(y)(0)=vy0}, [x(t), y(t)]);

which will produce the by-now very familiar answers:

\( \begin{align} x \left( t \right) &={\it vx0}\,t+{\it x0}\\ y \left( t \right) &=-1/2\,g{t}^{2}+{\it vy0}\,t+{\it y0} \end{align} \)

Using Solutions and Substituting Parameters for Differential Equations

In order to use these solutions, you should give them a name. Click at the start of the solve line and pre-pend it with soln:= so it resembles:

soln:=dsolve({acceqns, x(0)=x0, y(0)=y0, (D)(x)(0)=vx0, (D)(y)(0)=vy0}, [x(t), y(t)]);

This will assign the solution list to a variable that we can use later.

Now that you have the symbolic answers to the variables \(x(t)\) and \(y(t)\), you may want to substitute the actual coefficient values to obtain a numerical solution, though you will likely leave at least one variable alone. For example, in this case, you will not substitute anything in for \(t\).

In a similar fashion to the first lab, add the following lines of code:

Vals := x0=0, y0=5, vx0=5, vy0=5, g=9.8;

subs(Vals, soln);

The list in soln will now be shown with numerical values instead of symbols. Remember that you have {\it not} made any actual changes to any of the variables.

Using Representations of Differential Equations

Note that you can also use the subs command to replace variables contained in soln. This is very useful if, for example, the answer you are looking for is some function of the variables \(x(t)\) and \(y(t)\). Assuming that you have determined the variable you are looking for, speed, is

\( \begin{align} \mbox{speed}&=\sqrt{\left(\frac{dx}{dt}\right)^2+\left(\frac{dy}{dt}\right)^2} \end{align} \)

you can now use the symbolic representations in Maple to generate a symbolic representation for speed:

speed := subs(soln, sqrt((diff(x(t), t))^2+(diff(y(t), t))^2));

To get Maple to take the derivatives, you can write

speed := expand(subs(soln, sqrt((diff(x(t), t))^2+(diff(y(t), t))^2)));

If you want a numerical value, you can again use the subs command and the value list from before:

subs(Vals, speed);

To both substitute both equations in soln and the values in Vals simultaneously, you would need to write:

subs(soln[], Vals, speed);

where the soln[] is used to take the two equations in soln out of their brackets. Depending on the number and organization of solutions, the solution variable may be stored in different kinds of list. Unfortunately, Maple is somewhat picky about "unlisting" or "unset-ting" things. The following table shows how to make substitutions for different kinds of lists. Note that "row" refers to the row on which the specific substitutions to be used are:

soln Substitution format Comment
soln:= a=1 subs(soln, Other eqns., Target eqn.) Single solution
soln:=[[a=1, b=2]] subs(soln[1][], Other eqns., Target eqn.) Single solution list
soln:=[[a=1, b=2], [a=3, b=4]] subs(soln[row][], Other eqns., Target eqn.) Multiple solution list
soln:={a=1, b=2} subs(soln[], Other eqns., Target eqn.) Single solution set
soln:={{a=1, b=2}, {a=3, b=4}} subs(soln[row][], Other eqns., Target eqn.) Multiple solution set

Extra Information

Complicated Results

If your results look overly complicated - for example, there are several complicated exponentials (including complex exponentials) or there is a phrase "RootOf" and a bunch of Z's, there are a few things to try:

  • Use method=laplace in the <solve>line</solve>:
    soln:=dsolve({numeqn}, [var], method=laplace)
  • If that still produces something huge, you can look at a simplified and numerical version of the solution by using some Maple conversion and simplification commands:
    solnn := evalf[4](combine(expand(convert(soln, expsincos))))
    If this works, you may want to put a : after the soln line so you do not have to see the very complicated version.

Initial and Long-Term Behavior

Once you have your solutions, you can use the following to look at the initial values and the long-term values (for constant sources):

map(k -> limit(k, t = 0), soln)
map(k -> limit(k, t = infinity), soln)

If one or more of these produce complicated-looking results, you can use the numerical version of the solutions to hopefully get something clearer:

map(k -> limit(k, t = 0), solnn)
map(k -> limit(k, t = infinity), solnn)

Try the soln version first as the solnn version may have roudoff error.


Examples