Maple/Differential Equations/Old

From PrattWiki
Revision as of 21:00, 12 February 2012 by DukeEgr93 (talk | contribs)
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 (where the blanks in between lines indicate separate execution groups):

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 MySoln:= so it resembles:

MySoln:=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, MySoln)

The list in MySoln 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 MySoln. 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(MySoln, sqrt((diff(x(t), t))^2+(diff(y(t), t))^2))

To get Maple to take the derivatives, you can write

speed := expand(subs(MySoln, 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 MySoln and the values in Vals simultaneously, you would need to write:

subs(MySoln[], Vals, speed)

where the MySoln[] is used to take the two equations in MySoln 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:

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