Difference between revisions of "Maple/Differential Equations/RC Example"

From PrattWiki
Jump to navigation Jump to search
(Further Examples)
 
(21 intermediate revisions by the same user not shown)
Line 25: Line 25:
 
Using KCL at the top right node gives:
 
Using KCL at the top right node gives:
 
<center><math>
 
<center><math>
\frac{v_C(0^-)-v_s}{R_1}+
+
\frac{v_C(0^-)-v_S}{R_1}+
 
\frac{v_C(0^-)}{R_2}=0\!
 
\frac{v_C(0^-)}{R_2}=0\!
 
</math></center>
 
</math></center>
which can be solved to find the capacitor voltage at the time just before the source <math>v_s(t)</math> changes.   
+
which can be solved to find the capacitor voltage at the time just before the source <math>v_S(t)</math> changes.   
  
 
== Model Equations for t>0 ==
 
== Model Equations for t>0 ==
Line 37: Line 37:
 
Using KCL at the top right node again gives:
 
Using KCL at the top right node again gives:
 
<center><math>
 
<center><math>
\frac{v_C-v_s}{R_1}+
+
\frac{v_C(t)-v_S(t)}{R_1}+
\frac{v_C}{R_2}+C\frac{dv_C}{dt}=0\!
+
\frac{v_C(t)}{R_2}+C\frac{dv_C(t)}{dt}=0\!
 
</math></center>
 
</math></center>
 
  
 
== Code ==
 
== Code ==
Now that the equations for DC steady state and for the differential model are known, you can write Maple code to solve for everything.
+
Now that the equations for DC steady state and for the differential model are known, you can write Maple code to solve for everything. A complete example of the code below is at the [https://maple.cloud/app/5138367069159424/RRCexample Maple Cloud]
  
 
=== Preparing the Worksheet ===
 
=== Preparing the Worksheet ===
Line 49: Line 48:
 
of the page.  Also be sure that the first Maple command is <code>restart</code>.
 
of the page.  Also be sure that the first Maple command is <code>restart</code>.
  
=== Element and Source Values ===
+
=== Values ===
Next, generate lists for the element values and for the source
+
Create a list of values for substitutions laterNote that there should probably be one variable for the DCSS steady-state analysis and another for the differential equations.  In this case, <code>vsDC</code> will be the $$t<0$$ value and <code>vs(t)</code> will be for $$t\geq 0$$.
valuesFor the element values, you can write
 
ElementVals:= R1=20e3, R2=30e3, C=50e-6
 
 
 
For the source values, there will be two different sets - one before
 
the transition and one after.  The initial conditions will be based on
 
the old set while the differential equations will be based on the
 
newFor that reason, there need to be two different lists:
 
OldSourceVals:= vs(t)=5
 
NewSourceVals:= vs(t)=10*cos(8*t)
 
Note that the lists have the source values as functions of <code>t</code> - this
 
will come in handy in solving circuits with time-varying inputs.
 
  
 
=== Initial Conditions From Steady-State ===
 
=== Initial Conditions From Steady-State ===
While Maple can solve differential equations with symbolic initial
+
While Maple can solve differential equations with symbolic initial conditions and coefficients, most of the time this will result in a very unwieldy and unhelpful
conditions and coefficients, most of the
 
time this will result in a very unwieldy and unhelpful
 
 
representation.  For this assignment, you will be providing Maple with
 
representation.  For this assignment, you will be providing Maple with
numerical values for the initial conditions.  The three step process
+
numerical values for the initial conditions.  The four-step process
for this is to set up equations for the DC steady-state values in terms
+
for this is:
of the sources and elements:
+
* Set up equations for the DC steady-state values in terms of the sources and elements <syntaxhighlight>eqn1 := (vC(0)-vsDC)/R1 + vC(0)/R2 = 0</syntaxhighlight>
ss1 := (vcss-vs(t))/R1 + vcss/R2 = 0
+
* Put those equations in a list:<syntaxhighlight>eqns := [eqn1]</syntaxhighlight>
solve those equations,
+
* Solve those equations:<syntaxhighlight>sssoln := solve(eqns, [vC(0)])</syntaxhighlight>
ICsoln := solve({ss1}, [vcss])
+
* Substitute numbers into those solutions and de-bracket the list for use later:<syntaxhighlight>numsssoln := subs(vals, sssoln)[][]</syntaxhighlight>
and then
+
At the end of this step, there will be an un-bracketed set of equations storing the values of the variables at time 0.
substitute in the proper element and source values. 
 
ICnum := subs(ICsoln[], OldSourceVals, ElementVals, {vc(0) = vcss})
 
This latter part
 
will also be where you explicitly tell Maple that the steady state
 
values are the initial conditions by stating that <code>vc(0)</code> is equal
 
to the numerical value of <code>vcss</code>.  The output of this should be a
 
set of equations, where sets are surrounded by curly brackets.
 
  
 
=== Differential Equations ===
 
=== Differential Equations ===
Next set up the differential equations, generate a single list of
+
For this assignment, you will also be providing substituting values into the differential equations - unlike the linear algebra equations above, however, you really need to make the substitutions before solvingThe four-step process for this is:
the differential equations with numerical values substituted in for
+
* Set up differential equations for $$t\geq 0$$: <syntaxhighlight>deqn1 := (vC(t) - vs(t))/R1 + (vC(t) + 0)/R2 + C*diff(vC(t), t) = 0</syntaxhighlight>
the elements and sources, and solveThis is a three step process: define the
+
* Put those equations in a list:<syntaxhighlight>deqns := [deqn1]</syntaxhighlight>
equations:
+
* Substitute numbers into those equations and de-bracket for later:<syntaxhighlight>numdeqns := subs(vals, deqns)[]</syntaxhighlight>
eqn1 := (vc(t)-vs(t))/R1+vc(t)/R2+C*(diff(vc(t), t)) = 0
+
* Solve those equations:<syntaxhighlight>dsoln := dsolve([numsssoln, numdeqns], [vC(t)])</syntaxhighlight>
and then substitute in the element values and the ''new'' source values:
+
At the end of this step, there will be a solution or a collection of solutions for the variable or variablesIf you want to see a version where numbers are converted to floating-point notation and rounded, include the code <syntaxhighlight>evalf[4](dsoln)</syntaxhighlight>
EQnum := subs(NewSourceVals, ElementVals, {eqn1})
+
 
The end result will be a set of equationsNow solve the equations:
+
=== Plotting ===
MySoln := dsolve({EQnum[], ICnum[]}, [vc(t)])
+
 
You may want to look at a simplified version of the solution by first converting it to  
+
To make a plot, just substitute in your solution (or de-bracketed collection of solutions) into your variable of interest::
 +
<syntaxhighlight>plot(subs(dsoln, [vC(t)]), t = 0 .. 10, labels = [typeset(t, ", s"), typeset(v__C(t), ", V")]);
 +
</syntaxhighlight>
 +
 
 +
== Extra Steps ==
 +
Though not necessary for this particular example, there may be times when you may want to look at a simplified version of the solution by first converting it to  
 
cos, sin, and exponentials (in case there are hyperbolic trig functions) and allowing  
 
cos, sin, and exponentials (in case there are hyperbolic trig functions) and allowing  
Maple to expand and then combine terms:
+
Maple to expand and combine terms, then round off to four significant figures:<syntaxhighlight> evalf[4](combine(expand(convert(dsoln, expsincos))))</syntaxhighlight>
evalf[4](combine(expand(MySoln, expsincos)))
 
  
Note that in many cases the results of the differential equation are,
+
Note that in some cases the results of the differential equation are,
 
frankly, ugly.  Sometimes, telling Maple to solve using the Laplace
 
frankly, ugly.  Sometimes, telling Maple to solve using the Laplace
 
method comes up with a more compact answer:
 
method comes up with a more compact answer:
MySoln := dsolve({EQnum[], ICnum[]}, [vc(t)], method=laplace):
+
<syntaxhighlight>dsoln := dsolve([numsssoln, numdeqns], [vC(t)], method=laplace)
evalf[4](combine(expand(MySoln, expsincos)))
+
evalf[4](combine(expand(convert(dsoln, expsincos))))</syntaxhighlight>
 +
 
 
Other times, neither Maple's default method nor Laplace have a
 
Other times, neither Maple's default method nor Laplace have a
 
``nice'' answer; in those cases, simply put a colon at the end of the
 
``nice'' answer; in those cases, simply put a colon at the end of the
line to suppress the output and forget about the simplify line:
+
line to suppress the output and forget about the simplify line:<syntaxhighlight>dsoln := dsolve([numsssoln, numdeqns], [vC(t)], method=laplace)</syntaxhighlight>
MySoln := dsolve({EQnum[], ICnum[]}, [vc(t)]):
 
 
In those cases, you will want to focus more on the plot than the
 
In those cases, you will want to focus more on the plot than the
 
analytical solution.
 
analytical solution.
  
=== Plotting ===
 
 
Plotting can sometimes be a little more complicated than it seems - much of the
 
Plotting can sometimes be a little more complicated than it seems - much of the
 
time, round-off errors will cause solutions that have tiny vestigial
 
time, round-off errors will cause solutions that have tiny vestigial
 
imaginary values. To eliminate this, you can have Maple <code>map</code> the
 
imaginary values. To eliminate this, you can have Maple <code>map</code> the
 
real part of the solution vector.  That is:
 
real part of the solution vector.  That is:
plot(map(Re, subs(MySoln, [vc(t)])), t = 0 .. 5,
 
labels = ["Time (t)", "Voltage (V)"],
 
labeldirections = [horizontal, vertical],
 
title = "Capacitor Voltage", legend = ["vc"]);
 
 
== Complete Example ==
 
For the Spring 2018 semester, students in EGR 224 get the code using:
 
wget people.duke.edu/~mrg/EGR224/lab3files.tar
 
from Linux or from a browser at
 
http://people.duke.edu/~mrg/EGR224/
 
in the lab3files.tar file.
 
<source lang=text>
 
# Demonstration Code - Lab 3 - EGR 224
 
 
# Prepare Worksheet
 
restart;
 
 
# Element and Source Values
 
ElementVals := R1 = 0.20e5, R2 = 0.30e5, C = 0.50e-4;
 
OldSourceVals := vs(t) = 5;
 
NewSourceVals := vs(t) = 10*cos(8*t);
 
 
# Initial Conditions
 
ss1 := (vcss-vs(t))/R1+vcss/R2 = 0;
 
ICsoln := solve({ss1}, [vcss]);
 
ICnum := subs(ICsoln[], ElementVals, OldSourceVals, {vc(0) = vcss});
 
  
# Differential Equations
+
<syntaxhighlight>plot(map(Re, subs(dsoln, [vC(t)])), t = 0 .. 10,
eqn1 := (vc(t)-vs(t))/R1+vc(t)/R2+C*(diff(vc(t), t)) = 0;
+
labels = [typeset(t, ", s"), typeset(vC(t), ", V")])</syntaxhighlight>
EQnum := subs(ElementVals, NewSourceVals, {eqn1});
 
MySoln := dsolve({EQnum[], ICnum[]}, [vc(t)], method = laplace);
 
evalf[4](combine(expand(MySoln, expsincos)));
 
  
# Plotting
+
== Further Examples ==
plot(subs(MySoln, map(Re, [vc(t)])), t = 0 .. 5, labels = ["Time (t)", "Voltage (V)"], labeldirections = [horizontal, vertical], title = "Voltage Out", legend = ["vc"]);
+
The following examples are from "Circuit Analysis and Design" by Ulaby, Maharbiz, and Furse - free download at: https://services.publishing.umich.edu/publications/ee/
</source>
+
* CAD Example 0608 Underdamped - [https://maple.cloud/app/6507127311040512/CAD+Example+0608+Underdamped?key=EEA7F050169E4F8DAB8997F3C7ADFB66302825487348498ABD2BF536042C93C0 Maple Cloud Link]
Note - you may want to copy each line into its own execution group. There does not seem to be a simple way to copy and paste a file like this and have each command land in its own execution group. If anyone figures out a good way to solve this problem, please add it here.
+
* CAD Example 0610 Overdamped - [https://maple.cloud/app/5024665695289344/CAD+Example+0610+Overdamped?key=3347BAEC53D04D068EAB572DBA7E5425911676E5E122483984EFDBDBABB06EB8 Maple Cloud Link]

Latest revision as of 14:09, 27 February 2024

Description

The following page will go through an example of using Maple's ability to work with differential equations to analyze a circuit that undergoes a change in source values. In this particular case, the independent source is given as a constant for all times before 0 sec, at which time it changes to a non-constant source. The source is connected to a network containing both resistors and a capacitor. While there are several ways to put all this work together in a Maple script, the following will provide a flexible framework for solving the equations and using substitution to determine a numerical list of initial conditions, substituting element values into the differential equations and putting them into a list, solving the differential equations, and finally plotting the results.

Circuit

For this demonstration code, the following circuit is used:

RCD DemoCircuit.png

where \(R_1\)=20 k\(\Omega\), \(R_2\)=30 k\(\Omega\), \(C\)=50 \(\mu\)F, and \(v_s(t)\) changes from 5 V to 10 cos(8\(t\)) V when \(t=0\) s.

DC Steady-State Analysis

Assuming the circuit has been in place for a "long time" before \(t\)=0 sec, and given the topology of the circuit and the fact that the independent source is a constant for all times before 0 sec, you can use the DC steady-state equivalent for the circuit at \(t=0^-\) sec:

RCD DemoCircuitSS.png

Using KCL at the top right node gives:

\( \frac{v_C(0^-)-v_S}{R_1}+ \frac{v_C(0^-)}{R_2}=0\! \)

which can be solved to find the capacitor voltage at the time just before the source \(v_S(t)\) changes.

Model Equations for t>0

In general, after \(t\)=0 sec you can label the circuit as:

RCD DemoCircuitDE.png

Using KCL at the top right node again gives:

\( \frac{v_C(t)-v_S(t)}{R_1}+ \frac{v_C(t)}{R_2}+C\frac{dv_C(t)}{dt}=0\! \)

Code

Now that the equations for DC steady state and for the differential model are known, you can write Maple code to solve for everything. A complete example of the code below is at the Maple Cloud

Preparing the Worksheet

Be sure that your name and the assignment show up as text at the top of the page. Also be sure that the first Maple command is restart.

Values

Create a list of values for substitutions later. Note that there should probably be one variable for the DCSS steady-state analysis and another for the differential equations. In this case, vsDC will be the $$t<0$$ value and vs(t) will be for $$t\geq 0$$.

Initial Conditions From Steady-State

While Maple can solve differential equations with symbolic initial conditions and coefficients, most of the time this will result in a very unwieldy and unhelpful representation. For this assignment, you will be providing Maple with numerical values for the initial conditions. The four-step process for this is:

  • Set up equations for the DC steady-state values in terms of the sources and elements
    eqn1 := (vC(0)-vsDC)/R1 + vC(0)/R2 = 0
  • Put those equations in a list:
    eqns := [eqn1]
  • Solve those equations:
    sssoln := solve(eqns, [vC(0)])
  • Substitute numbers into those solutions and de-bracket the list for use later:
    numsssoln := subs(vals, sssoln)[][]

At the end of this step, there will be an un-bracketed set of equations storing the values of the variables at time 0.

Differential Equations

For this assignment, you will also be providing substituting values into the differential equations - unlike the linear algebra equations above, however, you really need to make the substitutions before solving. The four-step process for this is:

  • Set up differential equations for $$t\geq 0$$:
    deqn1 := (vC(t) - vs(t))/R1 + (vC(t) + 0)/R2 + C*diff(vC(t), t) = 0
  • Put those equations in a list:
    deqns := [deqn1]
  • Substitute numbers into those equations and de-bracket for later:
    numdeqns := subs(vals, deqns)[]
  • Solve those equations:
    dsoln := dsolve([numsssoln, numdeqns], [vC(t)])

At the end of this step, there will be a solution or a collection of solutions for the variable or variables. If you want to see a version where numbers are converted to floating-point notation and rounded, include the code

evalf[4](dsoln)

Plotting

To make a plot, just substitute in your solution (or de-bracketed collection of solutions) into your variable of interest::

plot(subs(dsoln, [vC(t)]), t = 0 .. 10, labels = [typeset(t, ", s"), typeset(v__C(t), ", V")]);

Extra Steps

Though not necessary for this particular example, there may be times when you may want to look at a simplified version of the solution by first converting it to cos, sin, and exponentials (in case there are hyperbolic trig functions) and allowing

Maple to expand and combine terms, then round off to four significant figures:

 evalf[4](combine(expand(convert(dsoln, expsincos))))

Note that in some cases the results of the differential equation are, frankly, ugly. Sometimes, telling Maple to solve using the Laplace method comes up with a more compact answer:

dsoln := dsolve([numsssoln, numdeqns], [vC(t)], method=laplace)
evalf[4](combine(expand(convert(dsoln, expsincos))))

Other times, neither Maple's default method nor Laplace have a ``nice answer; in those cases, simply put a colon at the end of the

line to suppress the output and forget about the simplify line:

dsoln := dsolve([numsssoln, numdeqns], [vC(t)], method=laplace)

In those cases, you will want to focus more on the plot than the analytical solution.

Plotting can sometimes be a little more complicated than it seems - much of the time, round-off errors will cause solutions that have tiny vestigial imaginary values. To eliminate this, you can have Maple map the real part of the solution vector. That is:

plot(map(Re, subs(dsoln, [vC(t)])), t = 0 .. 10, 
 labels = [typeset(t, ", s"), typeset(vC(t), ", V")])

Further Examples

The following examples are from "Circuit Analysis and Design" by Ulaby, Maharbiz, and Furse - free download at: https://services.publishing.umich.edu/publications/ee/