Maple/Differential Equations/Old
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:
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:
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:
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:
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 list of equations
(including initial conditions) to solve with and the name of the
variable for which to solve. In this particular case, add the
command:
dsolve({deqn1, x(0)=1}, [x(t)])
and Maple will produce the answer:
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:
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 {\tt solve} line and pre-pend it with {\tt
MySoln:=} so it resembles:
\begin{lstlisting}[frame=shadowbox] MySoln:=dsolve({acceqns, x(0)=x0, y(0)=y0, (D)(x)(0)=vx0, (D)(y)(0)=vy0}, [x(t), y(t)]) \end{lstlisting} 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: \begin{lstlisting}[frame=shadowbox] Vals := x0=0, y0=5, vx0=5, vy0=5, g=9.8 subs(Vals, MySoln) \end{lstlisting} The list in {\tt 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.
\subsection{Using Representations of Differential Equations} Note that you can also use the {\tt subs} command to replace variables contained in {\tt 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, {\tt 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 {\tt speed}: \begin{lstlisting}[frame=shadowbox] speed := subs(MySoln, sqrt((diff(x(t), t))^2+(diff(y(t), t))^2)) \end{lstlisting} To get Maple to take the derivatives, you can write \begin{lstlisting}[frame=shadowbox] speed := expand(subs(MySoln, sqrt((diff(x(t), t))^2+(diff(y(t), t))^2))) \end{lstlisting} If you want a numerical value, you can again use the {\tt subs} command and the value list from before: \begin{lstlisting}[frame=shadowbox] subs(Vals, speed) \end{lstlisting}
To both substitute both equations in {\tt MySoln} and the values in {\tt Vals} simultaneously, you would need to write: \begin{lstlisting}[frame=shadowbox] subs(MySoln[], Vals, speed) \end{lstlisting} where the {\tt MySoln[]} is used to take the two equations in {\tt
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: \renewcommand{\arraystretch}{1.5} \small \begin{center} \begin{tabular}{l|l|l} MySoln & Substitution format &Comment\\ \hline 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 \end{tabular} \end{center} \normalsize