Difference between revisions of "Maple/Differential Equations"
(→Define Equation) |
(→Very Basic Example) |
||
Line 23: | Line 23: | ||
Solving a system of differential equations is also similar to solving a system of linear algebra equations - the main differences are that you will use <code>dsolve</code> instead of <code>solve</code>, you must continue to use $$y(t)$$ instead of just $$y$$, and you may end up needing to add some initial conditions. The code | Solving a system of differential equations is also similar to solving a system of linear algebra equations - the main differences are that you will use <code>dsolve</code> instead of <code>solve</code>, you must continue to use $$y(t)$$ instead of just $$y$$, and you may end up needing to add some initial conditions. The code | ||
<syntaxhighlight lang=Maple> | <syntaxhighlight lang=Maple> | ||
− | + | soln1:=dsolve([eqn1], [y(t)]) | |
</syntaxhighlight> | </syntaxhighlight> | ||
− | will result in a solution of:<center><math>\mathit{ | + | will result in a solution of:<center><math>\mathit{soln1}:=\left\{y\! \left(t\right)=\frac{4}{3}+{\mathrm e}^{-\frac{3 t}{2}} \textit{_}\mathit{C1}\right\}</math></center> |
=== Initial Condition === | === Initial Condition === | ||
To incorporate initial conditions, you will give the <code>dsolve</code> command information about the value of the variable (or, for higher order differential equations, the value and values of the derivatives of the variable). For example, to solve our sample equation with $$y(0)=5$$, you will include the initial condition by adding <code>y(0)=5</code> to the equations: | To incorporate initial conditions, you will give the <code>dsolve</code> command information about the value of the variable (or, for higher order differential equations, the value and values of the derivatives of the variable). For example, to solve our sample equation with $$y(0)=5$$, you will include the initial condition by adding <code>y(0)=5</code> to the equations: | ||
<syntaxhighlight lang=Maple> | <syntaxhighlight lang=Maple> | ||
− | + | soln2:=dsolve([eqn1, y(0)=5], [y(t)]) | |
</syntaxhighlight> | </syntaxhighlight> | ||
will produce <center><math> | will produce <center><math> | ||
− | \mathit{ | + | \mathit{soln2}:=y\! \left(t\right)=\frac{4}{3}+\frac{11 {\mathrm e}^{-\frac{3 t}{2}}}{3} |
</math></center> | </math></center> | ||
Note that the initial condition does not have to be at time 0; if you know that $$y(6)=7$$, you can use that as well: | Note that the initial condition does not have to be at time 0; if you know that $$y(6)=7$$, you can use that as well: | ||
<syntaxhighlight lang=python> | <syntaxhighlight lang=python> | ||
− | + | soln3:=dsolve([eqn1, y(6)=7], [y(t)]) | |
</syntaxhighlight> | </syntaxhighlight> | ||
will produce<center><math> | will produce<center><math> | ||
− | \mathit{ | + | \mathit{soln3}:=y\! \left(t\right)=\frac{4}{3}+\frac{17 {\mathrm e}^{-\frac{3 t}{2}}}{3} |
</math></center> | </math></center> | ||
+ | |||
+ | == Making a Plot == | ||
+ | Once you have a solution or set of solutions, you can plot them using subs. For instance, to plot $$y(t)$$ in ''soln2'' above (where we set $$y(0)=5$$) you can use: | ||
+ | <syntaxhighlight lang=python> | ||
+ | plot(subs(soln2, y(t)), t = 0 .. 5) | ||
+ | </syntaxhighlight> | ||
+ | and can of course add other plotting options as needed. Note that ''soln2'' is a single equation, not a collection. If you end up getting a collection of results, you may need to de-bracket them. | ||
+ | |||
<!-- | <!-- |
Revision as of 22:10, 26 February 2024
Contents
Introduction
This page focuses on using Maple to find both the symbolic and the numeric solutions to differential equations obtained from electric circuits.
Note: There is a...decidedly more complicated explanation of these things at Maple/Differential Equations/Old; the goal for Spring 2024 and beyond is to keep things simpler.
Very Basic Example
Imagine you have the equation $$2\frac{dy(t)}{dt} + 3 y(t) = 4$$ with the initial condition $$y(0)=5$$, and you want to solve for $$y(t)$$. You can do this with Maple as follows:
Initialization
As always, it is a good idea to include the restart
command in your worksheet:
restart
Define Equation
In the same way that you were able to assign a linear algebra expression to a variable, you can do the same with a differential equation. The key is to note that the Maple diff
command can be used to calculate or represent a derivative. You will need to explicitly let Maple know that your variable is a function (in our case, a function of $$t$$) by including that parameter with the variable. Given that, you can store the differential equation in a variable called eqn
with:
eqn1:=2*diff(y(t), t)+3*y(t)=4
Solve Equation
Solving a system of differential equations is also similar to solving a system of linear algebra equations - the main differences are that you will use dsolve
instead of solve
, you must continue to use $$y(t)$$ instead of just $$y$$, and you may end up needing to add some initial conditions. The code
soln1:=dsolve([eqn1], [y(t)])
will result in a solution of:
Initial Condition
To incorporate initial conditions, you will give the dsolve
command information about the value of the variable (or, for higher order differential equations, the value and values of the derivatives of the variable). For example, to solve our sample equation with $$y(0)=5$$, you will include the initial condition by adding y(0)=5
to the equations:
soln2:=dsolve([eqn1, y(0)=5], [y(t)])
will produce
Note that the initial condition does not have to be at time 0; if you know that $$y(6)=7$$, you can use that as well:
soln3:=dsolve([eqn1, y(6)=7], [y(t)])
will produce
Making a Plot
Once you have a solution or set of solutions, you can plot them using subs. For instance, to plot $$y(t)$$ in soln2 above (where we set $$y(0)=5$$) you can use:
plot(subs(soln2, y(t)), t = 0 .. 5)
and can of course add other plotting options as needed. Note that soln2 is a single equation, not a collection. If you end up getting a collection of results, you may need to de-bracket them.