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

From PrattWiki
Jump to navigation Jump to search
Line 103: Line 103:
 
== 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 <code>solve</code> line and pre-pend it with <code>MySoln:=</code> so it resembles:
+
at the start of the <code>solve</code> line and pre-pend it with <code>soln:=</code> so it resembles:
 
<source lang=text>
 
<source lang=text>
MySoln:=dsolve({acceqns, x(0)=x0, y(0)=y0, (D)(x)(0)=vx0, (D)(y)(0)=vy0}, [x(t), y(t)]);
+
soln:=dsolve({acceqns, x(0)=x0, y(0)=y0, (D)(x)(0)=vx0, (D)(y)(0)=vy0}, [x(t), y(t)]);
 
</source>
 
</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
Line 120: Line 120:
 
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, soln);
 
</source>
 
</source>
The list in <code>MySoln</code> will now be shown with numerical values
+
The list in <code>soln</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.
Line 128: Line 128:
 
==Using Representations of Differential Equations==
 
==Using Representations of Differential Equations==
 
Note that you can also use the <code>subs</code> command to replace variables
 
Note that you can also use the <code>subs</code> command to replace variables
contained in <code>MySoln</code>.  This is very useful if, for example, the
+
contained in <code>soln</code>.  This is very useful if, for example, the
 
answer you are  
 
answer you are  
 
looking for is some function of the variables <math>x(t)</math> and <math>y(t)</math>.
 
looking for is some function of the variables <math>x(t)</math> and <math>y(t)</math>.
Line 141: Line 141:
 
symbolic representation for <code>speed</code>:
 
symbolic representation for <code>speed</code>:
 
<source lang=text>
 
<source lang=text>
speed := subs(MySoln, sqrt((diff(x(t), t))^2+(diff(y(t), t))^2));
+
speed := subs(soln, sqrt((diff(x(t), t))^2+(diff(y(t), t))^2));
 
</source>
 
</source>
 
To get Maple to take the derivatives, you can write
 
To get Maple to take the derivatives, you can write
 
<source lang=text>
 
<source lang=text>
speed := expand(subs(MySoln, sqrt((diff(x(t), t))^2+(diff(y(t), t))^2)));
+
speed := expand(subs(soln, sqrt((diff(x(t), t))^2+(diff(y(t), t))^2)));
 
</source>
 
</source>
 
If you want a numerical value,
 
If you want a numerical value,
Line 154: Line 154:
 
</source>
 
</source>
  
To both substitute both equations in <code>MySoln</code> and the values in
+
To both substitute both equations in <code>soln</code> and the values in
 
<code>Vals</code> simultaneously, you would need to write:
 
<code>Vals</code> simultaneously, you would need to write:
 
<source lang=text>
 
<source lang=text>
subs(MySoln[], Vals, speed);
+
subs(soln[], Vals, speed);
 
</source>
 
</source>
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.
+
where the <code>soln[]</code> is used to take the two equations in <code> soln</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.
 
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
Line 166: Line 166:
 
{|style="border-collapse: separate; border-spacing: 0; border-width: 1px; border-style: solid; border-color: #000; padding: 0"
 
{|style="border-collapse: separate; border-spacing: 0; border-width: 1px; border-style: solid; border-color: #000; padding: 0"
 
|-
 
|-
!style="border-style: solid; border-width: 0 1px 1px 0"| MySoln
+
!style="border-style: solid; border-width: 0 1px 1px 0"| soln
 
!style="border-style: solid; border-width: 0 1px 1px 0"| Substitution format
 
!style="border-style: solid; border-width: 0 1px 1px 0"| Substitution format
 
!style="border-style: solid; border-width: 0 0 1px 0"| Comment
 
!style="border-style: solid; border-width: 0 0 1px 0"| Comment
 
|-
 
|-
|style="border-style: solid; border-width: 0 1px 0 0"| MySoln:= a=1
+
|style="border-style: solid; border-width: 0 1px 0 0"| soln:= a=1
|style="border-style: solid; border-width: 0 1px 0 0"| subs(MySoln, Other eqns., Target eqn.)
+
|style="border-style: solid; border-width: 0 1px 0 0"| subs(soln, Other eqns., Target eqn.)
 
|style="border-style: solid; border-width: 0"        | Single solution
 
|style="border-style: solid; border-width: 0"        | Single solution
 
|-
 
|-
|style="border-style: solid; border-width: 0 1px 0 0"| <nowiki>MySoln:=[[a=1, b=2]]</nowiki>
+
|style="border-style: solid; border-width: 0 1px 0 0"| <nowiki>soln:=[[a=1, b=2]]</nowiki>
|style="border-style: solid; border-width: 0 1px 0 0"| subs(MySoln[1][], Other eqns., Target eqn.)
+
|style="border-style: solid; border-width: 0 1px 0 0"| subs(soln[1][], Other eqns., Target eqn.)
 
|style="border-style: solid; border-width: 0"        | Single solution list
 
|style="border-style: solid; border-width: 0"        | Single solution list
 
|-
 
|-
|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"| soln:=[[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 1px 0 0"| subs(soln[row][], Other eqns., Target eqn.)
 
|style="border-style: solid; border-width: 0"        | Multiple solution list
 
|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"| soln:={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 1px 0 0"| subs(soln[], Other eqns., Target eqn.)  
 
|style="border-style: solid; border-width: 0"        | Single solution set
 
|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"| soln:={{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 1px 0 0"| subs(soln[row][], Other eqns., Target eqn.)
 
|style="border-style: solid; border-width: 0"        | Multiple solution set
 
|style="border-style: solid; border-width: 0"        | Multiple solution set
 
|}
 
|}
 
</center>
 
</center>
 +
 +
== 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 <code>method=laplace</code> in the <solve>line</solve>:<syntaxhighlight>soln:=dsolve){numeqn}, [var], method=laplace)</syntaxhighlight>
 +
* 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:<syntaxhighlight>solnn := evalf[4](combine(expand(convert(soln, expsincos))))</syntaxhighlight> If this works, you may want to put a : after the <code>soln</code> 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): <syntaxhighlight>map(k -> limit(k, t = 0), soln)
 +
map(k -> limit(k, t = infinity), soln)</syntaxhighlight> If one or more of these produce complicated-looking results, you can use the numerical version of the solutions to hopefully get something clearer: <syntaxhighlight>map(k -> limit(k, t = 0), solnn)
 +
map(k -> limit(k, t = infinity), solnn)</syntaxhighlight>  Try the <code>soln</code> version first as the <code>solnn</code> version may have roudoff error.
 +
  
 
== Examples ==
 
== Examples ==
 
* [[Maple/Differential Equations/RC Example]] - Example with a simple RC-Circuit using DC Steady State to determine initial conditions
 
* [[Maple/Differential Equations/RC Example]] - Example with a simple RC-Circuit using DC Steady State to determine initial conditions

Revision as of 16:29, 15 February 2022

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