Difference between revisions of "Maple/Differential Equations/Old"

From PrattWiki
Jump to navigation Jump to search
 
Line 99: Line 99:
 
== Using Solutions and Substituting Parameters for Differential Equations ==
 
== Using Solutions and Substituting Parameters for Differential Equations ==
 
In order to use these solutions, you should give them a name.  Click
 
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
+
at the start of the <code>solve</code> line and pre-pend it with <code>MySoln:=</code> so it resembles:
  MySoln:=} so it resembles:
+
<source lang=text>
\begin{lstlisting}[frame=shadowbox]
 
 
MySoln:=dsolve({acceqns, x(0)=x0, y(0)=y0, (D)(x)(0)=vx0, (D)(y)(0)=vy0},  
 
MySoln:=dsolve({acceqns, x(0)=x0, y(0)=y0, (D)(x)(0)=vx0, (D)(y)(0)=vy0},  
 
     [x(t), y(t)])
 
     [x(t), y(t)])
\end{lstlisting}
+
</source>
 
This will assign the solution list to a variable that we can use
 
This will assign the solution list to a variable that we can use
 
later.
 
later.
  
Now that you have the symbolic answers to the variables $x(t)$ and $y(t)$,  
+
Now that you have the symbolic answers to the variables <math>x(t)</math> and <math>y(t)</math>,  
 
you may want to substitute the actual coefficient values to
 
you may want to substitute the actual coefficient values to
 
obtain a numerical solution, though you will likely leave at least one
 
obtain a numerical solution, though you will likely leave at least one
 
variable alone.  For example, in this case, you will not substitute
 
variable alone.  For example, in this case, you will not substitute
anything in for $t$.
+
anything in for <math>t</math>.
  
 
In a similar fashion to the first lab, add the following lines of code:
 
In a similar fashion to the first lab, add the following lines of code:
\begin{lstlisting}[frame=shadowbox]
+
<source lang=text>
 
Vals := x0=0, y0=5, vx0=5, vy0=5, g=9.8
 
Vals := x0=0, y0=5, vx0=5, vy0=5, g=9.8
 
subs(Vals, MySoln)
 
subs(Vals, MySoln)
\end{lstlisting}
+
</source>
The list in {\tt MySoln} will now be shown with numerical values
+
The list in <code>MySoln</code> will now be shown with numerical values
 
instead of symbols.  Remember that you have {\it not} made any actual
 
instead of symbols.  Remember that you have {\it not} made any actual
 
changes to any of the variables.
 
changes to any of the variables.
  
\subsection{Using Representations of Differential Equations}
+
==Using Representations of Differential Equations==
Note that you can also use the {\tt subs} command to replace variables
+
Note that you can also use the <code>subs</code> command to replace variables
contained in {\tt MySoln}.  This is very useful if, for example, the
+
contained in <code>MySoln</code>.  This is very useful if, for example, the
 
answer you are  
 
answer you are  
looking for is some function of the variables $x(t)$ and $y(t)$.
+
looking for is some function of the variables <math>x(t)</math> and <math>y(t)</math>.
 
Assuming that you have determined the variable you are looking for,
 
Assuming that you have determined the variable you are looking for,
{\tt speed}, is
+
<code>speed</code>, is
\begin{align*}
+
<center><math>
 +
\begin{align}
 
\mbox{speed}&=\sqrt{\left(\frac{dx}{dt}\right)^2+\left(\frac{dy}{dt}\right)^2}
 
\mbox{speed}&=\sqrt{\left(\frac{dx}{dt}\right)^2+\left(\frac{dy}{dt}\right)^2}
\end{align*}
+
\end{align}
 +
</math></center>
 
you can now use the symbolic representations in Maple to generate a
 
you can now use the symbolic representations in Maple to generate a
symbolic representation for {\tt speed}:
+
symbolic representation for <code>speed</code>:
\begin{lstlisting}[frame=shadowbox]
+
<source lang=text>
 
speed := subs(MySoln, sqrt((diff(x(t), t))^2+(diff(y(t), t))^2))
 
speed := subs(MySoln, sqrt((diff(x(t), t))^2+(diff(y(t), t))^2))
\end{lstlisting}
+
</source>
 
To get Maple to take the derivatives, you can write
 
To get Maple to take the derivatives, you can write
\begin{lstlisting}[frame=shadowbox]
+
<source lang=text>
 
speed := expand(subs(MySoln, sqrt((diff(x(t), t))^2+(diff(y(t), t))^2)))
 
speed := expand(subs(MySoln, sqrt((diff(x(t), t))^2+(diff(y(t), t))^2)))
\end{lstlisting}
+
</source>
 
If you want a numerical value,
 
If you want a numerical value,
you can again use the {\tt subs} command and the value list from
+
you can again use the <code>subs</code> command and the value list from
 
before:
 
before:
\begin{lstlisting}[frame=shadowbox]
+
<source lang=text>
 
subs(Vals, speed)
 
subs(Vals, speed)
\end{lstlisting}
+
</source>
  
To both substitute both equations in {\tt MySoln} and the values in
+
To both substitute both equations in <code>MySoln</code> and the values in
{\tt Vals} simultaneously, you would need to write:
+
<code>Vals</code> simultaneously, you would need to write:
\begin{lstlisting}[frame=shadowbox]
+
<source lang=text>
 
subs(MySoln[], Vals, speed)
 
subs(MySoln[], Vals, speed)
\end{lstlisting}
+
</source>
where the {\tt MySoln[]} is used to take the two equations in {\tt
+
where the <code>MySoln[]</code> is used to take the two equations in <code> MySoln</code> 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.
  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
 
The following table shows how to make substitutions for different
kinds of lists.  Note that ``row'' refers to the row on which the
+
kinds of lists.  Note that "row" refers to the row on which the
 
specific substitutions to be used are:
 
specific substitutions to be used are:
\renewcommand{\arraystretch}{1.5}
+
<center>
\small
+
{|style="border-collapse: separate; border-spacing: 0; border-width: 1px; border-style: solid; border-color: #000; padding: 0"
\begin{center}
+
|-
\begin{tabular}{l|l|l}
+
!style="border-style: solid; border-width: 0 1px 1px 0"| MySoln
MySoln & Substitution format &Comment\\ \hline
+
!style="border-style: solid; border-width: 0 1px 1px 0"| Substitution format
MySoln:= a=1 subs(MySoln, Other eqns., Target eqn.)
+
!style="border-style: solid; border-width: 0 0 1px 0"| Comment
& Single solution\\
+
|-
MySoln:=[[a=1, b=2]] & subs(MySoln[1][\,], Other eqns., Target eqn.)
+
|style="border-style: solid; border-width: 0 1px 0 0"| MySoln:= a=1
& Single solution list\\
+
|style="border-style: solid; border-width: 0 1px 0 0"| subs(MySoln, Other eqns., Target eqn.)
MySoln:=[[a=1, b=2], [a=3, b=4]] & subs(MySoln[row][\,], Other
+
|style="border-style: solid; border-width: 0"        | Single solution
eqns., Target eqn.) & Multiple solution list\\
+
|-
MySoln:=\{a=1, b=2\} & subs(MySoln[\,], Other eqns., Target eqn.)  
+
|style="border-style: solid; border-width: 0 1px 0 0"| <nowiki>MySoln:=[[a=1, b=2]]</nowiki>
& Single solution set\\
+
|style="border-style: solid; border-width: 0 1px 0 0"| subs(MySoln[1][], Other eqns., Target eqn.)
MySoln:=\{\{a=1, b=2\}, \{a=3, b=4\}\} & subs(MySoln[row][\,], Other eqns.,
+
|style="border-style: solid; border-width: 0"        | Single solution list
Target eqn.) & Multiple solution set
+
|-
\end{tabular}
+
|style="border-style: solid; border-width: 0 1px 0 0"| MySoln:=[[a=1, b=2], [a=3, b=4]]
\end{center}
+
|style="border-style: solid; border-width: 0 1px 0 0"| subs(MySoln[row][], Other eqns., Target eqn.)
\normalsize
+
|style="border-style: solid; border-width: 0"        | Multiple solution list
 +
|-
 +
|style="border-style: solid; border-width: 0 1px 0 0"| MySoln:={a=1, b=2}
 +
|style="border-style: solid; border-width: 0 1px 0 0"| subs(MySoln[], Other eqns., Target eqn.)  
 +
|style="border-style: solid; border-width: 0"        | Single solution set
 +
|-
 +
|style="border-style: solid; border-width: 0 1px 0 0"| MySoln:={{a=1, b=2}, {a=3, b=4}}
 +
|style="border-style: solid; border-width: 0 1px 0 0"| subs(MySoln[row][], Other eqns., Target eqn.)
 +
|style="border-style: solid; border-width: 0"        | Multiple solution set
 +
|}
 +
</center>

Revision as of 18:00, 12 February 2012

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:

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 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:

\( \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