Maple/Differential Equations

From PrattWiki
Revision as of 22:54, 26 February 2024 by DukeEgr93 (talk | contribs) (No Great Version 1 =)
Jump to navigation Jump to search

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:

\(\mathit{soln1}:=\left\{y\! \left(t\right)=\frac{4}{3}+{\mathrm e}^{-\frac{3 t}{2}} \textit{_}\mathit{C1}\right\}\)

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

\( \mathit{soln2}:=y\! \left(t\right)=\frac{4}{3}+\frac{11 {\mathrm e}^{-\frac{3 t}{2}}}{3} \)

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

\( \mathit{soln3}:=y\! \left(t\right)=\frac{4}{3}+\frac{17 {\mathrm e}^{-\frac{3 t}{2}}}{3} \)

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.

Second Order Example

Now imagine that you want to solve for $$y(t)$$ in

\( \frac{d^2y(t)}{dt^2}=-g\)

We can solve this using all symbols or numbers, and we can solve with or without initial conditions.

Initialization

restart

Define Equation

eqn1 := diff(y(t), t, t) = -g

Solve Equation / Initial Conditions

Without initial conditions, you can use

soln1 := dsolve([eqn1], [y(t)])

to get:

\(\mathit{soln1}:=\left\{y\! \left(t\right)=-\frac{1}{2} g \,t^{2}+\textit{_}\mathit{C1} t+\textit{_}\mathit{C2}\right\}\)

You can also put in symbolic initial conditions. To put in a derivative condition, use the format D^n(var)(time)=val to establish a condition for the nth derivative of var at time time. For instance, if you know some initial velocity and position, we can use:

soln2 := dsolve([eqn1, y(0) = y0, D(y)(0) = v0], [y(t)])

to get:

\(\mathit{soln2}:=y\! \left(t\right)=-\frac{1}{2} g \,t^{2}+\mathit{v0} t+\mathit{y0}\)


Making a Plot

Since your answer has symbols, you will need to replace them with numbers before plotting:

plot(subs(soln2, g = 9.81, y0 = 5, v0 = 10, y(t)), t = 0 .. 2)

and can of course add other plotting options as needed. Once again, note that soln2 is a single equation, not a collection, so there is no need to de-bracket.


More Complicated Coupled Example

For a series RLC circuit with an applied voltage $$v_S(t)$$ across the series network, you can find a coupled set of differential equations using KVL and the model equation for the capacitor, respectively, as:

\( \begin{align*} L\frac{iL(t)}{st}+R\,i_L(t)+v_C(t)&=0\\ i_L(t)&=C\frac{dv_C(t)}{dt} \end{align*}\)

We can try to solve these symbolically, but the results will not be particularly helpful.

Initialization

restart

Define Equations

eqn1 := -vs(t) + L*diff(iL(t), t) + R*iL(t) + vC(t) = 0;
eqn2 := iL(t) = C*diff(vC(t), t);
eqns := [eqn1, eqn2]

Solve Equation / Initial Conditions

No Great Version 1 =

If you try

soln1 := dsolve(eqns, [iL(t), vC(t)])

you will get:

\( \mathit{soln1}:= \left\{\mathit{iL}\! \left(t\right) = \left(\frac{{\mathrm e}^{-\frac{\left(R C+\sqrt{C \left(R^{2} C-4 L\right)}\right) t}{2 L C}} C R}{2 \sqrt{C \left(R^{2} C-4 L\right)}\, L}+\frac{{\mathrm e}^{-\frac{\left(R C+\sqrt{C \left(R^{2} C-4 L\right)}\right) t}{2 L C}}}{2 L}\right) \left({\int}\mathit{vs}\! \left(t\right) {\mathrm e}^{\frac{\left(R C+\sqrt{C \left(R^{2} C-4 L\right)}\right) t}{2 L C}}{d}t\right)+\left(-\frac{{\mathrm e}^{-\frac{\left(R C-\sqrt{C \left(R^{2} C-4 L\right)}\right) t}{2 L C}} C R}{2 \sqrt{C \left(R^{2} C-4 L\right)}\, L}+\frac{{\mathrm e}^{-\frac{\left(R C-\sqrt{C \left(R^{2} C-4 L\right)}\right) t}{2 L C}}}{2 L}\right) \left({\int}\mathit{vs}\! \left(t\right) {\mathrm e}^{\frac{\left(R C-\sqrt{C \left(R^{2} C-4 L\right)}\right) t}{2 L C}}{d}t\right)+\left(-\frac{C^{2} R^{2}}{2 \sqrt{C \left(R^{2} C-4 L\right)}\, L}-\frac{R C}{2 L}+\frac{2 C}{\sqrt{C \left(R^{2} C-4 L\right)}}\right) \textit{_}\mathit{C1} {\mathrm e}^{-\frac{\left(R C+\sqrt{C \left(R^{2} C-4 L\right)}\right) t}{2 L C}}+\left(\frac{C^{2} R^{2}}{2 \sqrt{C \left(R^{2} C-4 L\right)}\, L}-\frac{R C}{2 L}-\frac{2 C}{\sqrt{C \left(R^{2} C-4 L\right)}}\right) \textit{_}\mathit{C2} {\mathrm e}^{-\frac{\left(R C-\sqrt{C \left(R^{2} C-4 L\right)}\right) t}{2 L C}}, \\ \mathit{vC}\! \left(t\right)=-\frac{-{\mathrm e}^{-\frac{\left(R C-\sqrt{C \left(R^{2} C-4 L\right)}\right) t}{2 L C}} \textit{_}\mathit{C2} \sqrt{C \left(R^{2} C-4 L\right)}-{\mathrm e}^{-\frac{\left(R C+\sqrt{C \left(R^{2} C-4 L\right)}\right) t}{2 L C}} \textit{_}\mathit{C1} \sqrt{C \left(R^{2} C-4 L\right)}+\left({\int}\mathit{vs}\! \left(t\right) {\mathrm e}^{\frac{\left(R C+\sqrt{C \left(R^{2} C-4 L\right)}\right) t}{2 L C}}{d}t\right) {\mathrm e}^{-\frac{R t}{L}+\frac{\left(R C-\sqrt{C \left(R^{2} C-4 L\right)}\right) t}{2 L C}}-\left({\int}\mathit{vs}\! \left(t\right) {\mathrm e}^{\frac{\left(R C-\sqrt{C \left(R^{2} C-4 L\right)}\right) t}{2 L C}}{d}t\right) {\mathrm e}^{-\frac{R t}{L}+\frac{\left(R C+\sqrt{C \left(R^{2} C-4 L\right)}\right) t}{2 L C}}}{\sqrt{C \left(R^{2} C-4 L\right)}}\mathrm{ \\}\right\} \)

Without initial conditions, you can use

soln1 := dsolve([eqn1], [y(t)])

to get:

\(\mathit{soln1}:=\left\{y\! \left(t\right)=-\frac{1}{2} g \,t^{2}+\textit{_}\mathit{C1} t+\textit{_}\mathit{C2}\right\}\)

You can also put in symbolic initial conditions. To put in a derivative condition, use the format D^n(var)(time)=val to establish a condition for the nth derivative of var at time time. For instance, if you know some initial velocity and position, we can use:

soln2 := dsolve([eqn1, y(0) = y0, D(y)(0) = v0], [y(t)])

to get:

\(\mathit{soln2}:=y\! \left(t\right)=-\frac{1}{2} g \,t^{2}+\mathit{v0} t+\mathit{y0}\)

Making a Plot

Since your answer has symbols, you will need to replace them with numbers before plotting:

plot(subs(soln2, g = 9.81, y0 = 5, v0 = 10, y(t)), t = 0 .. 2)

and can of course add other plotting options as needed. Once again, note that soln2 is a single equation, not a collection, so there is no need to de-bracket.