Difference between revisions of "Maple/Simultaneous Equations"

From PrattWiki
Jump to navigation Jump to search
 
(Solve Equations)
 
(20 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
==Introduction==
 
==Introduction==
This page focuses on using Maple to find both the
+
This page focuses on using Maple to find both the symbolic and the numeric solutions to equations obtained from electric circuits.  It assumes that you have already taken the steps in [[Maple/Initialization and Documentation]] to start Maple and begin documenting your work.
symbolic and the numeric solutions to equations obtained from electric circuits.
 
  
 +
== Very Basic Example ==
 +
The example code below assumes you are running a worksheet in Maple. There is a finished example at the end of this section. Imagine you have the equation $$ax=d$$ and you want to solve for $$x$$. You can do this in Maple as follows:
  
==Starting the Program==
+
=== Initialization ===
Maple is free to Duke students and resides on the OIT system in the
+
You are not explicitly required to include the <code>restart</code> command in a worksheet, but it does help if you end up making edits later and need to re-run everything from scratchGo ahead and put
same way that MATLAB does.  To start Maple, make sure your terminal is
+
<syntaxhighlight>
set up to receive graphics and type
+
restart
<source lang=bash>
+
</syntaxhighlight>
xmaple &
+
as the first executable in your worksheet.
</source>
 
at the prompt.  Maple will start up.  It may have a window at startup
 
containing hints or tips - go ahead and close that window.  There will
 
most likely be some initial blank document in the main window - go
 
ahead and close it as well by selecting '''File-Close Document'''.
 
Then, open a new blank worksheet with '''File-New-Worksheet Mode'''.
 
  
==Documenting Your Work==
+
=== Define Equations ===
When Maple starts a worksheet, it expects everything to be an input.
 
To document your work with the title of the assignment, your name and
 
NET ID, and any kind of explanation you would like to add, you need to
 
tell Maple to switch to paragraph mode.  Go to '''Insert-Paragraph-Before Cursor''' and you will notice that a blank
 
line opens up above the red cursor mark.  You can type text in here
 
and Maple will know ''not'' to try to process it.  Go ahead and call
 
this assignment '''Introductory Maple Assignment''', hit return, put
 
in your name followed by your NET ID in parenthesis, hit return, and
 
put in today's date. 
 
 
 
==Clearing the Worksheet==
 
When Maple runs, it "remembers" everything that it has done in the
 
worksheet, regardless of what order you ran lines of code.  For that
 
reason, it is good programming practice to have Maple "restart"
 
itself at the beginning of each worksheet.  To give Maple a command,
 
first tell Maple you are ready to issue commands by selecting '''Insert-Execution Group-After Cursor'''. This will start a new bracket
 
(black lines at the left of the worksheet) and give you a prompt (red
 
>).  At the prompt, type '''restart'''.  When you hit return, if you
 
quickly look at the bottom left of the Maple window, you will see that
 
Maple evaluates the command then then tells you that it is Ready.  The
 
'''restart''' command clears out any variables Maple was taught and
 
also clears out any packages that were loaded.  It is a good way to
 
make sure you have a "fresh start."
 
 
 
==Defining Variables and Equations==
 
 
In Maple, the way you define a variable is by typing the name of the
 
In Maple, the way you define a variable is by typing the name of the
 
variable, followed by the symbols ''':=''', followed by whatever items
 
variable, followed by the symbols ''':=''', followed by whatever items
 
you want to store in the variable.  Note the importance of the colon
 
you want to store in the variable.  Note the importance of the colon
directly in front of the equals sign - without it, Maple will ''not'' assign a value to a variable but will merely print out the
+
directly in front of the equals sign - without it, Maple will ''not'' assign a value to a variable but will merely print out the equation you typed.
equation you typed.
 
 
One benefit of this is you can define variables to hold on to
 
One benefit of this is you can define variables to hold on to
 
equations and then use those variables later, in concert with Maple's
 
equations and then use those variables later, in concert with Maple's
solver, to get answers for the unknowns.  Let us assume that we want to
+
solver, to get answers for the unknowns.  Given that, we will define a variable <code>eqn1</code> to store the equation $$ax=d$$:
 +
<syntaxhighlight>
 +
eqn1:=a*x=d
 +
</syntaxhighlight>
 +
 
 +
=== Solve Equations ===
 +
The easiest way to solve an equation (or a system of equations) is to use the <code>solve</code> command.  The most formal, and flexible, way to use this command is to give it a set of equations (surrounded by curly brackets) and a list of variables (surrounded by square brackets).  The result will be an expression, set of expressions, list of expressions, or list of list of expressions depending on the nature and number of the equations and the solutions.  For example, if you add the code:
 +
<syntaxhighlight>
 +
soln1 := solve({eqn1}, [x])
 +
</syntaxhighlight>
 +
then Maple will produce a variable called <code>soln1</code> that has a list with a list with an expression; specifically, $$soln1 := [[x = \frac{d}{a}]]$$
 +
 
 +
=== Make Substitutions ===
 +
Now that you have symbolic answers, you can make numerical substitutions for those symbols using the <code>subs</code> command.  The subs command expects a series of equalities to define the substitutions followed by a single item into which to make those substitutions.  For example, to see what x is when d is 10, you can write:
 +
<syntaxhighlight>
 +
subs(d = 10, soln1)
 +
</syntaxhighlight>
 +
and you will get the new list of lists $$[[x = \frac{10}{a}]]$$.  If you want to see multiple substitutions, you can put them all at the start of the soln command:
 +
<syntaxhighlight>
 +
subs(a=3, d = 10, soln1)
 +
</syntaxhighlight>
 +
will give $$[[x = \frac{10}{3}]]$$
 +
 
 +
=== Full Example ===
 +
[https://maple.cloud/app/6117497218662400/VeryBasicLA VeryBasicLA.mw] on MapleCloud
 +
 
 +
== More Complicated Example ==
 +
Let us now assume that we want to
 
solve the following equations:
 
solve the following equations:
 
<center><math>
 
<center><math>
Line 60: Line 56:
 
</math></center>
 
</math></center>
 
where ''x'', ''y'', and ''z'' are unknowns, ''a'' through ''i'' are known
 
where ''x'', ''y'', and ''z'' are unknowns, ''a'' through ''i'' are known
coefficients, and ''j'' through ''l'' are known variables.  To teach
+
coefficients, and ''j'' through ''l'' are known variables.  There is a finished example at the end of this section.
Maple about these equations, you would create three variables, each
+
=== Initialization ===
holding on to one of the equationsAt the prompt, type:
+
Go ahead and put
 +
<syntaxhighlight>
 +
restart
 +
</syntaxhighlight>
 +
as the first executable in your worksheet.
 +
 
 +
=== Define Equations ===
 +
Now there are three different equations to store; you could store all three in a single variable containing a set or you could define three variables and then include them in a set in a solve commandWe will do the latter, so at the prompt, type:
 
<source lang=text>
 
<source lang=text>
 
eqn1:=a*x+b*y+c*z=j
 
eqn1:=a*x+b*y+c*z=j
Line 70: Line 73:
 
Note that each time you hit return to go to the next line, Maple
 
Note that each time you hit return to go to the next line, Maple
 
processes your input and reports back what it has done.  It will also
 
processes your input and reports back what it has done.  It will also
number the outputs for you so you can refer to them later.
+
number the outputs for you so you can refer to them later. As an aside, if you try to copy and paste this whole block, when you hit return, you will get a parsing error.  Maple is trying to interpret the whole group as one long string of code and gets confused at the end of the first equation.  To fix this, if you copy and paste multiple lines of codes, you can end them with a semi-colon:
At this point, Maple now has three variables, each of
+
<source lang=text>
which defined as an equation.  It is perfectly happy having undefined
+
eqn1:=a*x+b*y+c*z=j;
items in the equations.
+
eqn2:=d*x+e*y+f*z=k;
 +
eqn3:=g*x+h*y+i*z=l;
 +
</source>
 +
The three lines will now be in a single execution group and all three will run when you hit return at the end of that line.
 +
 
 +
Whichever way you entered and ran the code, at this point, Maple now has three variables, each of which defined as an equation.  It is perfectly happy having undefined items in the equations.  Note that in Maple the complex number $$\sqrt{-1}$$ is given with a capital $$I$$; you must avoid using $$I$$ as a variable in Maple.  We are using lower case $$i$$, which is fine.
  
==Solving Equations With Maple==
+
===Solve Equations With Maple===
To solve the equations, all you need to do is use Maple's built in
+
Now we just need to give the <code>solve</code> command a set of equations and a list of variablesNote that you need to give a complete list of the unknowns for a system even if yuo are only looking for the value of one of themThat is to say, even if we are just trying to solve $$x$$, we still need to let Maple know that $$x$$, $$y$$, and $$z$$ are the unknowns -- anything '''not''' listed as an unknown variable is considered known, and thus a system can become overconstrained and not have a solution.  Add the line:
'''solve''' functionOne of the best ways to use the '''solve'''
 
function is to give it a list of the equations and an array of items
 
for which to solveIn the equations above, for example, there are
 
three equations with a total of fifteen symbols - we need to tell
 
Maple which ones are unknown and it will assume that the others are
 
known.  Add the line:
 
 
<source lang=text>
 
<source lang=text>
solve({eqn1, eqn2, eqn3}, [x, y, z])
+
soln1:=solve({eqn1, eqn2, eqn3}, [x, y, z])
 
</source>
 
</source>
and note that the equations are bracketed with curly braces while the
+
Hit return, and you will once again note that Maple produces a list of lists of expressions for the solution.  
unknowns are in a list set off with square brackets.  Hit return, and
+
 
you will note that Maple produces a list - set off with double
+
As an aside, if we had not included the variable list and
brackets - containing the answers for ''x'', ''y'', and ''z'' in terms of
 
the other variables. If we had not included the variable list and
 
 
instead had asked
 
instead had asked
 
<source lang=text>
 
<source lang=text>
solve({eqn1, eqn2, eqn3})
+
soln2:=solve({eqn1, eqn2, eqn3})
 
</source>
 
</source>
 
Maple would have given all possible combinations of all 15 symbols that
 
Maple would have given all possible combinations of all 15 symbols that
 
would satisfy the equations.  Conversely, if we had given Maple only
 
would satisfy the equations.  Conversely, if we had given Maple only
$x$ to work with as an unknown by typing:
+
''x'' to work with as an unknown by typing:
 
<source lang=text>
 
<source lang=text>
solve({eqn1, eqn2, eqn3}, [x])
+
soln3:=solve({eqn1, eqn2, eqn3}, [x])
 
</source>
 
</source>
the answer would come back as empty because no value
+
the answer would come back as an empty list empty because no value
of $x$ satisfies the three equations for arbitrary values of the other
+
of ''x'' satisfies the three equations for arbitrary values of the other 14 variables.
14 variables.
 
  
In order to use these solutions, you should give them a nameClick
+
=== Make Substitutions ===
at the start of the {\tt solve} line and pre-pend it with {\tt
+
Now that you have the symbolic answers to the variables ''x'', ''y'', and ''z'', you may want to substitute the actual coefficient values to
  MySoln:=} so it resembles:
+
obtain a numerical solutionIn the simple example above we put the substitution expressions directly into the <code>subs</code>command.  Given that we may want to re-use those expressions, or that we may have multiple sets of substitutions, another way to use the <code>subs</code> command is to generate a list
\begin{lstlisting}[frame=shadowbox]
+
of the known values then tell Maple to substitute in the numerical
MySoln:=solve({eqn1, eqn2, eqn3}, [x, y, z])
+
values.  Add the following
\end{lstlisting}
+
lines of code:
This will assign the solution list to a variable that we can use
+
<source lang=text>
later.
+
Vals := a=-1, b=2, c=3, d=4, e=5, f=6, g=7, h=8, i=9, j=10, k=11, l=12
 +
subs(Vals, soln1)
 +
</source>
 +
Remember if you cut and paste these into a single group you will need to add a semi-colon after the first line or else you will get a parsing error.
 +
The list of lists in <code>soln1</code> will now be shown with numerical valuesinstead of symbols.  Note that you have ''not'' made any actual changes to any of the variables - you have just asked Maple to show you what they would look like given the particular substitutions presented in '''Vals'''.  This is a very powerful tool, since you can substitute in a variety of values to see how one or more parameters influence a particular variable or variables.
  
\subsection{Substituting Values}
 
Now that you have the symbolic answers to the variables $x$, $y$, and
 
$z$, you may want to substitute the actual coefficient values to
 
obtain a numerical solution.  One way to do this is to generate a list
 
of the known values, then tell Maple to substitute in the numerical
 
values by using the built-in {\tt subs} command.  Add the following
 
lines of code:
 
\begin{lstlisting}[frame=shadowbox]
 
Vals := {a=1, b=2, c=3, d=4, e=5, f=6, g=7, h=8, i=9, j=10, k=11, l=12}
 
subs(Vals, MySoln)
 
\end{lstlisting}
 
The list in {\tt MySoln} will now be shown with numerical values
 
instead of symbols.  Note that you have {\it not} made any actual
 
changes to any of the variables - you have just asked Maple to show
 
you what they would look like given the particular substitutions
 
presented in {\tt Vals}.  This is a very powerful tool, since you can
 
substitute in a variety of values to see how one or more parameters
 
influence a particular variable or variables.
 
  
\subsection{Assigning Representations}
+
<!--
 +
===Assigning Representations===
 
There will be many times you actually want to assign the solutions
 
There will be many times you actually want to assign the solutions
found by {\tt solve} - that is, you want to take the equations out of
+
found by '''solve''' - that is, you want to take the equations out of
 
the list and have Maple process them as if the = were := so that Maple
 
the list and have Maple process them as if the = were := so that Maple
could use those expressions later.  Maple has a command called {\tt
+
could use those expressions later.  Maple has a command called '''assign''' that does exactly that.  Add the commands:
  assign} that does exactly that.  Add the commands:
+
<source lang=text>
\begin{lstlisting}[frame=shadowbox]
 
 
assign(MySoln)
 
assign(MySoln)
 
x
 
x
 
y
 
y
 
z
 
z
\end{lstlisting}
+
</source>
You will see that while the {\tt assign} command does not report
+
You will see that while the '''assign''' command does not report
anything back to you, when you ask Maple to tell you what $x$, $y$,
+
anything back to you, when you ask Maple to tell you what ''x'', ''y'',
and $z$ are, it responds with the symbolic representation produced in
+
and ''z'' are, it responds with the symbolic representation produced in
{\tt MySoln}.  This is very useful if, for example, the answer you are
+
'''MySoln'''.  This is very useful if, for example, the answer you are
looking for is some function of the variables $x$, $y$, and $z$.
+
looking for is some function of the variables ''x'', ''y'', and ''z''.
 
Assuming that you have determined the variable you are looking for,
 
Assuming that you have determined the variable you are looking for,
$alpha$, is
+
''alpha'', is
\begin{align*}
+
<center><math>
 +
\begin{align}
 
\alpha&=x+y+z
 
\alpha&=x+y+z
\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 $\alpha$:
+
symbolic representation for ''alpha'':
\begin{lstlisting}[frame=shadowbox]
+
<source lang=text>
 
alpha:=x+y+z
 
alpha:=x+y+z
\end{lstlisting}
+
</source>
 
Note, among other things, that Maple represents the variable named
 
Note, among other things, that Maple represents the variable named
{\tt alpha} as its symbol, $\alpha$.  If you want a numerical value,
+
'''alpha''' as its symbol, <math>\alpha</math>.  If you want a numerical value,
you can again use the {\tt subs} command and the value list from
+
you can again use the '''subs''' command and the value list from
 
before:
 
before:
\begin{lstlisting}[frame=shadowbox]
+
<source lang=text>
 
subs(Vals, alpha)
 
subs(Vals, alpha)
\end{lstlisting}
+
</source>
 +
-->
 +
=== Full Example ===
 +
[https://maple.cloud/app/4991629096255488/More+Complicated+LA MoreComplicatedLA.mw] on MapleCloud; note this includes code from the following two sections as well.
  
\subsection{Cleaning Things Up}
+
==Multiple and Dependent Substitution Lists==
 +
If you have several sets of equations you want to use for substitution
 +
- including some which are dependent on values set in other equations,
 +
you can still use <code>subs</code> -- you just need to be careful about the
 +
order of the substitutions.  As an example, imagine some variable:
 +
<center><math>
 +
\begin{align}
 +
m&=p+q
 +
\end{align}
 +
</math></center>
 +
where
 +
<center><math>
 +
\begin{align}
 +
p&=r*s\\
 +
q&=t-u
 +
\end{align}
 +
</math></center>
 +
and
 +
<center><math>
 +
\begin{align}
 +
r&=1 & t&=3\\
 +
s&=2 & u&=4
 +
\end{align}
 +
</math></center>
 +
To get ''m'' in terms of ''r'', ''s'', ''t'', and ''u'', you could write:
 +
<source lang=text>
 +
eqn1:=m=p+q;
 +
sublist1:=p=r*s, q=t-u;
 +
subs(sublist1, eqn1);
 +
</source>
 +
If you want to get ''m'''s numerical value, you must ''first'' get ''m''
 +
in terms of ''r'', ''s'', ''t'', and ''u'', and ''then'' you can substitute
 +
in the numbers for those variables.  Specifically:
 +
<source lang=text>
 +
eqn1:=m=p+q;
 +
sublist1:=p=r*s, q=t-u;
 +
sublist2:=r=1, s=2, t=3, u=4;
 +
subs(sublist1, sublist2, eqn1);
 +
</source>
 +
This will show $$m=1$$; again, Maple has not made the variable $$m$$ equal to the number 1, it is just showing what things would be ilke if the substitutions were made.
 +
 
 +
Putting the equations in the wrong order will end up yielding an
 +
answer that is still in terms of  ''r'', ''s'', ''t'', and ''u''.  The reason is that <code>subs</code> ''only'' makes substitutions into the last entry in the argument list. <code>
 +
subs(sublist2, sublist2, eqn1);</code> will yield $$m=rs+t-u$$.
 +
 
 +
== Extracting Expressions from Lists (or Lists of Lists) ==
 +
Sometimes, you will need to take equations out of a set of brackets to
 +
use them.  For example, assume that you are looking back at the worksheet where you solved three linear algebra equations for $$x$$, $$y$$, and $$z$$.  Imagine you have some
 +
variables you want to calculate called </code>alpha</code> and <code>beta</code>, which has formulas
 +
of:
 +
<center><math>
 +
\begin{align}
 +
\alpha&=x+y+z\\
 +
\beta&=y*z
 +
\end{align}
 +
</math></center>
 +
You can put in the solutions for ''x'', ''y'', and ''z'' to get <code>alpha</code> and <code>beta</code>
 +
in terms of those characters.  What makes
 +
this a bit difficult is that <code>MySoln</code> is given as a
 +
single-row matrix and <code>subs</code> just wants the equations
 +
themselves.  If you just type <code>soln1</code> in the worksheet, you will get a list of lists.  To extract only the expressions, you can write:
 +
<source lang=text>
 +
soln1[][]
 +
</source>
 +
and you will instead see a comma-separated collecting of expressions without any brackets.  The default index for a list is '''everything''' - you could also write
 +
<source lang=text>
 +
soln1[1][]
 +
</source>
 +
here and that would grab the first inner list and then all the items from that inner list (Maple is 1-indexed, similar to MATLAB and different from Python).
 +
 
 +
In the worksheet where you solved for the three equations, go ahead and add the lines
 +
<source lang=text>
 +
aux1:=alpha=x+y+z
 +
aux2:=beta=y*x
 +
subs(soln1[1][], {aux1, aux2})
 +
</source>
 +
to the end of your worksheet.  Note that since we want all the substitutions to go into two different items, those items are collected in a set with curly brackets; you can also collect them in a list if you would like.  The rest of this command will be a set (or list) showing your auxiliary equations using the symbols solved for in <code>soln1</code>.
 +
 
 +
Now imagine you want the numerical values for $$\alpha$$ and $$\beta$$ based on the symbolic solutions in <code>soln1</code> and the numerical values in <code>vals</code>. 
 +
Again - the order is important - you need to first substitute in the
 +
equations for the variables higher in the dependency list, ''then'' give values to the
 +
known quantities, then substitute all that into whatever is in the
 +
final argument of <code>subs</code>. If you try <code>subs(vals, soln1[1][], {aux1, aux2})</code> you will not see any changes - the <code>vals</code> list will not find any of $$a$$ through $$l$$ in the auxiliary equations because those substitutions have not happened yet.  On the other hand, if you run <code>subs(vals, soln1[1][], {aux1, aux2})</code> you will get numerical values for $$\alpha$$ and $$\beta$$.
 +
 
 +
==Cleaning Things Up==
 
Many times, Maple will produce an expression that is more complicated
 
Many times, Maple will produce an expression that is more complicated
 
than it needs to be.  To get what it considers to be the simplest
 
than it needs to be.  To get what it considers to be the simplest
form, use the {\tt simplify(expand( ))} compound function.  The {\tt
+
form, use the '''simplify(expand( ))''' compound function.  The '''expand''' will take the expression and represent it using as many
  expand} will take the expression and represent it using as many
+
simple terms as necessary while '''simplify''' will recombine them in
simple terms as necessary while {\tt simplify} will recombine them in
+
the most compact form.  Finally, to get a decimal value, use the '''evalf[N]( )''' function, where '''N''' represents the number of
the most compact form.  Finally, to get a decimal value, use the {\tt
 
  evalf[N]( )} function, where {\tt N} represents the number of
 
 
decimal digits to use.  For example,   
 
decimal digits to use.  For example,   
\begin{lstlisting}[frame=shadowbox]
+
<source lang=text>
simplify(expand(alpha))
+
simplify(expand(thing))
\end{lstlisting}
+
</source>
will produce the most symbolically simplified version of $\alpha$ while
+
will produce the most symbolically simplified version of a thing while
\begin{lstlisting}[frame=shadowbox]
+
<source lang=text>
evalf[8](subs(Vals, alpha))
+
evalf[8](thing)
\end{lstlisting}
+
</source>
will produce a floating point result for $\alpha$.  With practice, you
+
will produce a floating point result for thing.  With practice, you
will see how best to combine {\tt evalf}, {\tt simplify}, and {\tt
+
will see how best to combine <code>evalf</code>, <code>simplify</code>, and <code>expand</code> to get the form of answer you want.
  expand} to get the form of answer you want.
 
  
\section{Memory Issues}
+
==Memory Issues==
A major issue to consider with Maple is its memory.  At the end of this worksheet,
+
A major issue to consider with Maple is its memory.  At the end of the worksheet above,
there are several variables that are defined, including $x$, $y$, and
+
there are several variables that are defined, including ''x'', ''y'', and
$z$.  If you go back near the beginning, click in the line where {\tt
+
''z''.  If you go back near the beginning, click in the line where '''eqn1''' is defined, and hit return, you will notice that where ''x'',
  eqn1} is defined, and hit return, you will notice that where $x$,
+
''y'', and ''z'' were before, their symbolic solutions from much further
$y$, and $z$ were before, their symbolic solutions from much further
+
down the worksheet are being used.  This is why the '''restart'''
down the worksheet are being used.  This is why the {\tt restart}
 
 
command is so helpful - if you need to to run a worksheet again, it is
 
command is so helpful - if you need to to run a worksheet again, it is
 
best to always start from scratch.  A shortcut for running an entire
 
best to always start from scratch.  A shortcut for running an entire
worksheet is the !!!~button at the top of the window.
+
worksheet is the !!! button at the top of the window.
  
\section{Sample Circuit}
+
<!--
Each of the samples presented below will involve solving for the power
+
== Examples ==
absorbed by resistor $\E{R}{4}$ in the circuit:
+
===Solving Circuit Variables===
\begin{center}
+
[[File:SampleStarter.png|thumb]]
\epsfig{file=./XMAPLE/SampleStarter.ps, scale=0.5}
+
Each of the samples in this section will involve solving for the power
\end{center}
+
absorbed by resistor <math>R_{4}</math> in the circuit at right
 
by using a different method.  The circuit will be presented several times
 
by using a different method.  The circuit will be presented several times
 
along the way to demonstrate how to label it properly for the given
 
along the way to demonstrate how to label it properly for the given
 
method.
 
method.
 +
<br clear=all>
  
\subsection{Node Voltage Method}
+
====Node Voltage Method====
 +
=====Choose Ground Node =====
 +
[[file:SampleNVM1.png|thumbs|Circuit now shown with ground node.]]
 
First, choose a ground node.  In this case, the large bottom node is
 
First, choose a ground node.  In this case, the large bottom node is
 
as good as any:
 
as good as any:
\begin{center}
+
=====Label Each Node=====
\epsfig{file=./XMAPLE/SampleNVM1.ps, scale=0.5}
 
\end{center}
 
  
 
Next, circle or otherwise mark each node:
 
Next, circle or otherwise mark each node:
Line 232: Line 303:
 
   Note that other unknowns could have been selected so long as they
 
   Note that other unknowns could have been selected so long as they
 
   gave equations for each of the nodes.
 
   gave equations for each of the nodes.
 
+
=====Determine Equations=====
 
Finally, you can write the KCL equations.  In this case, there are
 
Finally, you can write the KCL equations.  In this case, there are
 
three unknowns so you need to pick three of the nodes.  To help
 
three unknowns so you need to pick three of the nodes.  To help
Line 242: Line 313:
 
   leaving} the node through each branch that passes through the node
 
   leaving} the node through each branch that passes through the node
 
boundary, are:
 
boundary, are:
\begin{align*}
+
 
 +
Assume you have a circuit for which the KCL equations are:
 +
<center><math>
 +
\begin{align}
 
\mbox{KCL, n}_2&: &
 
\mbox{KCL, n}_2&: &
\frac{\E{v}{x}-\E{v}{y}}{\E{R}{1}}+
+
\frac{v_{x}-v_{y}}{R_{1}}+
\frac{\E{v}{x}-\E{v}{s}}{\E{R}{2}}+
+
\frac{v_{x}-v_{s}}{R_{2}}+
\frac{\E{v}{x}-\E{v}{z}}{\E{R}{3}}+
+
\frac{v_{x}-v_{z}}{R_{3}}+
\frac{\E{v}{x}-0}{\E{R}{4}}&=0\\
+
\frac{v_{x}-0}{R_{4}}&=0\\
 
\mbox{KCL, n}_3&: &
 
\mbox{KCL, n}_3&: &
\frac{\E{v}{y}-\E{v}{x}}{\E{R}{1}}-
+
\frac{v_{y}-v_{x}}{R_{1}}-
\E{i}{t}&=0\\
+
i_{t}&=0\\
 
\mbox{KCL, n}_4&: &
 
\mbox{KCL, n}_4&: &
\frac{\E{v}{z}-\E{v}{x}}{\E{R}{3}}+
+
\frac{v_{z}-v_{x}}{R_{3}}+
\frac{\E{v}{z}-0}{\E{R}{5}}+
+
\frac{v_{z}-0}{R_{5}}+
\E{i}{t}&=0
+
i_{t}&=0
\end{align*}
+
\end{align}
These three equations can be put into Maple {\it as is} - again, no
+
</math></center>
 +
These three equations can be put into Maple ''as is'' - again, no
 
need to set them up as a matrix if you are using Maple.  Maple can
 
need to set them up as a matrix if you are using Maple.  Maple can
solve for the three unknowns, and the power absorbed by $\E{R}{4}$
+
solve for the three unknowns, and the power absorbed by <math>R_{4}</math>
will be the voltage across the resistor ($\E{v}{x}-0$)
+
will be the voltage across the resistor (<math>R_{x}</math>)
 
squared divided by the resistance.
 
squared divided by the resistance.
\pagebreak
 
  
\subsection{Branch Current Method}
+
 
 +
====Branch Current Method====
 
For the BCM, start be determining the number of branches.  In this
 
For the BCM, start be determining the number of branches.  In this
 
particular case, there are five branches.  One of them - the very top
 
particular case, there are five branches.  One of them - the very top
Line 323: Line 398:
 
\pagebreak
 
\pagebreak
  
\subsection{Mesh Current Method}
+
====Mesh Current Method====
 
For the MCM, start by labeling the mesh currents in the primary loops.   
 
For the MCM, start by labeling the mesh currents in the primary loops.   
 
In this particular case, there are three primary loops.  As to avoid
 
In this particular case, there are three primary loops.  As to avoid
Line 361: Line 436:
 
squared multiplied by the resistance.
 
squared multiplied by the resistance.
  
 +
<!--
 
\section{Assignment}
 
\section{Assignment}
 
Create three worksheets - one each to solve the equations generated by
 
Create three worksheets - one each to solve the equations generated by
Line 380: Line 456:
 
% LocalWords:  NVM BCM MCM KCL KVL knowns supernode supernodes Vals MySoln OIT
 
% LocalWords:  NVM BCM MCM KCL KVL knowns supernode supernodes Vals MySoln OIT
 
% LocalWords:  evalf superloop Cramer's Maplesoft xmaple startup eqn
 
% LocalWords:  evalf superloop Cramer's Maplesoft xmaple startup eqn
 +
-->

Latest revision as of 23:32, 13 January 2024

Introduction

This page focuses on using Maple to find both the symbolic and the numeric solutions to equations obtained from electric circuits. It assumes that you have already taken the steps in Maple/Initialization and Documentation to start Maple and begin documenting your work.

Very Basic Example

The example code below assumes you are running a worksheet in Maple. There is a finished example at the end of this section. Imagine you have the equation $$ax=d$$ and you want to solve for $$x$$. You can do this in Maple as follows:

Initialization

You are not explicitly required to include the restart command in a worksheet, but it does help if you end up making edits later and need to re-run everything from scratch. Go ahead and put

restart

as the first executable in your worksheet.

Define Equations

In Maple, the way you define a variable is by typing the name of the variable, followed by the symbols :=, followed by whatever items you want to store in the variable. Note the importance of the colon directly in front of the equals sign - without it, Maple will not assign a value to a variable but will merely print out the equation you typed. One benefit of this is you can define variables to hold on to equations and then use those variables later, in concert with Maple's solver, to get answers for the unknowns. Given that, we will define a variable eqn1 to store the equation $$ax=d$$:

eqn1:=a*x=d

Solve Equations

The easiest way to solve an equation (or a system of equations) is to use the solve command. The most formal, and flexible, way to use this command is to give it a set of equations (surrounded by curly brackets) and a list of variables (surrounded by square brackets). The result will be an expression, set of expressions, list of expressions, or list of list of expressions depending on the nature and number of the equations and the solutions. For example, if you add the code:

soln1 := solve({eqn1}, [x])

then Maple will produce a variable called soln1 that has a list with a list with an expression; specifically, $$soln1 := [[x = \frac{d}{a}]]$$

Make Substitutions

Now that you have symbolic answers, you can make numerical substitutions for those symbols using the subs command. The subs command expects a series of equalities to define the substitutions followed by a single item into which to make those substitutions. For example, to see what x is when d is 10, you can write:

subs(d = 10, soln1)

and you will get the new list of lists $$[[x = \frac{10}{a}]]$$. If you want to see multiple substitutions, you can put them all at the start of the soln command:

subs(a=3, d = 10, soln1)

will give $$[[x = \frac{10}{3}]]$$

Full Example

VeryBasicLA.mw on MapleCloud

More Complicated Example

Let us now assume that we want to solve the following equations:

\( \begin{align} ax+by+cz&=j\\ dx+ey+fz&=k\\ gx+hy+iz&=l \end{align} \)

where x, y, and z are unknowns, a through i are known coefficients, and j through l are known variables. There is a finished example at the end of this section.

Initialization

Go ahead and put

restart

as the first executable in your worksheet.

Define Equations

Now there are three different equations to store; you could store all three in a single variable containing a set or you could define three variables and then include them in a set in a solve command. We will do the latter, so at the prompt, type:

eqn1:=a*x+b*y+c*z=j
eqn2:=d*x+e*y+f*z=k
eqn3:=g*x+h*y+i*z=l

Note that each time you hit return to go to the next line, Maple processes your input and reports back what it has done. It will also number the outputs for you so you can refer to them later. As an aside, if you try to copy and paste this whole block, when you hit return, you will get a parsing error. Maple is trying to interpret the whole group as one long string of code and gets confused at the end of the first equation. To fix this, if you copy and paste multiple lines of codes, you can end them with a semi-colon:

eqn1:=a*x+b*y+c*z=j;
eqn2:=d*x+e*y+f*z=k;
eqn3:=g*x+h*y+i*z=l;

The three lines will now be in a single execution group and all three will run when you hit return at the end of that line.

Whichever way you entered and ran the code, at this point, Maple now has three variables, each of which defined as an equation. It is perfectly happy having undefined items in the equations. Note that in Maple the complex number $$\sqrt{-1}$$ is given with a capital $$I$$; you must avoid using $$I$$ as a variable in Maple. We are using lower case $$i$$, which is fine.

Solve Equations With Maple

Now we just need to give the solve command a set of equations and a list of variables. Note that you need to give a complete list of the unknowns for a system even if yuo are only looking for the value of one of them. That is to say, even if we are just trying to solve $$x$$, we still need to let Maple know that $$x$$, $$y$$, and $$z$$ are the unknowns -- anything not listed as an unknown variable is considered known, and thus a system can become overconstrained and not have a solution. Add the line:

soln1:=solve({eqn1, eqn2, eqn3}, [x, y, z])

Hit return, and you will once again note that Maple produces a list of lists of expressions for the solution.

As an aside, if we had not included the variable list and instead had asked

soln2:=solve({eqn1, eqn2, eqn3})

Maple would have given all possible combinations of all 15 symbols that would satisfy the equations. Conversely, if we had given Maple only x to work with as an unknown by typing:

soln3:=solve({eqn1, eqn2, eqn3}, [x])

the answer would come back as an empty list empty because no value of x satisfies the three equations for arbitrary values of the other 14 variables.

Make Substitutions

Now that you have the symbolic answers to the variables x, y, and z, you may want to substitute the actual coefficient values to obtain a numerical solution. In the simple example above we put the substitution expressions directly into the subscommand. Given that we may want to re-use those expressions, or that we may have multiple sets of substitutions, another way to use the subs command is to generate a list of the known values then tell Maple to substitute in the numerical values. Add the following lines of code:

Vals := a=-1, b=2, c=3, d=4, e=5, f=6, g=7, h=8, i=9, j=10, k=11, l=12
subs(Vals, soln1)

Remember if you cut and paste these into a single group you will need to add a semi-colon after the first line or else you will get a parsing error. The list of lists in soln1 will now be shown with numerical valuesinstead of symbols. Note that you have not made any actual changes to any of the variables - you have just asked Maple to show you what they would look like given the particular substitutions presented in Vals. This is a very powerful tool, since you can substitute in a variety of values to see how one or more parameters influence a particular variable or variables.


Full Example

MoreComplicatedLA.mw on MapleCloud; note this includes code from the following two sections as well.

Multiple and Dependent Substitution Lists

If you have several sets of equations you want to use for substitution - including some which are dependent on values set in other equations, you can still use subs -- you just need to be careful about the order of the substitutions. As an example, imagine some variable:

\( \begin{align} m&=p+q \end{align} \)

where

\( \begin{align} p&=r*s\\ q&=t-u \end{align} \)

and

\( \begin{align} r&=1 & t&=3\\ s&=2 & u&=4 \end{align} \)

To get m in terms of r, s, t, and u, you could write:

eqn1:=m=p+q;
sublist1:=p=r*s, q=t-u;
subs(sublist1, eqn1);

If you want to get m's numerical value, you must first get m in terms of r, s, t, and u, and then you can substitute in the numbers for those variables. Specifically:

eqn1:=m=p+q;
sublist1:=p=r*s, q=t-u;
sublist2:=r=1, s=2, t=3, u=4;
subs(sublist1, sublist2, eqn1);

This will show $$m=1$$; again, Maple has not made the variable $$m$$ equal to the number 1, it is just showing what things would be ilke if the substitutions were made.

Putting the equations in the wrong order will end up yielding an answer that is still in terms of r, s, t, and u. The reason is that subs only makes substitutions into the last entry in the argument list. subs(sublist2, sublist2, eqn1); will yield $$m=rs+t-u$$.

Extracting Expressions from Lists (or Lists of Lists)

Sometimes, you will need to take equations out of a set of brackets to use them. For example, assume that you are looking back at the worksheet where you solved three linear algebra equations for $$x$$, $$y$$, and $$z$$. Imagine you have some variables you want to calculate called alpha and beta, which has formulas of:

\( \begin{align} \alpha&=x+y+z\\ \beta&=y*z \end{align} \)

You can put in the solutions for x, y, and z to get alpha and beta in terms of those characters. What makes this a bit difficult is that MySoln is given as a single-row matrix and subs just wants the equations themselves. If you just type soln1 in the worksheet, you will get a list of lists. To extract only the expressions, you can write:

soln1[][]

and you will instead see a comma-separated collecting of expressions without any brackets. The default index for a list is everything - you could also write

soln1[1][]

here and that would grab the first inner list and then all the items from that inner list (Maple is 1-indexed, similar to MATLAB and different from Python).

In the worksheet where you solved for the three equations, go ahead and add the lines

aux1:=alpha=x+y+z
aux2:=beta=y*x
subs(soln1[1][], {aux1, aux2})

to the end of your worksheet. Note that since we want all the substitutions to go into two different items, those items are collected in a set with curly brackets; you can also collect them in a list if you would like. The rest of this command will be a set (or list) showing your auxiliary equations using the symbols solved for in soln1.

Now imagine you want the numerical values for $$\alpha$$ and $$\beta$$ based on the symbolic solutions in soln1 and the numerical values in vals. Again - the order is important - you need to first substitute in the equations for the variables higher in the dependency list, then give values to the known quantities, then substitute all that into whatever is in the final argument of subs. If you try subs(vals, soln1[1][], {aux1, aux2}) you will not see any changes - the vals list will not find any of $$a$$ through $$l$$ in the auxiliary equations because those substitutions have not happened yet. On the other hand, if you run subs(vals, soln1[1][], {aux1, aux2}) you will get numerical values for $$\alpha$$ and $$\beta$$.

Cleaning Things Up

Many times, Maple will produce an expression that is more complicated than it needs to be. To get what it considers to be the simplest form, use the simplify(expand( )) compound function. The expand will take the expression and represent it using as many simple terms as necessary while simplify will recombine them in the most compact form. Finally, to get a decimal value, use the evalf[N]( ) function, where N represents the number of decimal digits to use. For example,

simplify(expand(thing))

will produce the most symbolically simplified version of a thing while

evalf[8](thing)

will produce a floating point result for thing. With practice, you will see how best to combine evalf, simplify, and expand to get the form of answer you want.

Memory Issues

A major issue to consider with Maple is its memory. At the end of the worksheet above, there are several variables that are defined, including x, y, and z. If you go back near the beginning, click in the line where eqn1 is defined, and hit return, you will notice that where x, y, and z were before, their symbolic solutions from much further down the worksheet are being used. This is why the restart command is so helpful - if you need to to run a worksheet again, it is best to always start from scratch. A shortcut for running an entire worksheet is the !!! button at the top of the window.