Difference between revisions of "Maple/Simultaneous Equations"
Line 3: | Line 3: | ||
symbolic and the numeric solutions to equations obtained from electric circuits. | symbolic and the numeric solutions to equations obtained from electric circuits. | ||
− | + | ==General Process of Solving Simultaneous Equations== | |
− | ==Starting the Program== | + | ===Starting the Program=== |
Maple is free to Duke students and resides on the OIT system in the | Maple is free to Duke students and resides on the OIT system in the | ||
same way that MATLAB does. To start Maple, make sure your terminal is | same way that MATLAB does. To start Maple, make sure your terminal is | ||
Line 17: | Line 17: | ||
Then, open a new blank worksheet with '''File-New-Worksheet Mode'''. | Then, open a new blank worksheet with '''File-New-Worksheet Mode'''. | ||
− | ==Documenting Your Work== | + | ===Documenting Your Work=== |
When Maple starts a worksheet, it expects everything to be an input. | 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 | To document your work with the title of the assignment, your name and | ||
Line 28: | Line 28: | ||
put in today's date. | put in today's date. | ||
− | ==Clearing the Worksheet== | + | ===Clearing the Worksheet=== |
When Maple runs, it "remembers" everything that it has done in the | When Maple runs, it "remembers" everything that it has done in the | ||
worksheet, regardless of what order you ran lines of code. For that | worksheet, regardless of what order you ran lines of code. For that | ||
Line 42: | Line 42: | ||
make sure you have a "fresh start." | make sure you have a "fresh start." | ||
− | ==Defining Variables and Equations== | + | ===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 | ||
Line 75: | Line 75: | ||
items in the equations. | items in the equations. | ||
− | ==Solving Equations With Maple== | + | ===Solving Equations With Maple=== |
To solve the equations, all you need to do is use Maple's built in | To solve the equations, all you need to do is use Maple's built in | ||
'''solve''' function. One of the best ways to use the '''solve''' | '''solve''' function. One of the best ways to use the '''solve''' | ||
Line 106: | Line 106: | ||
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 | + | at the start of the '''solve''' line and pre-pend it with '''MySoln:=''' so it resembles: |
− | + | <source lang=text> | |
− | |||
MySoln:=solve({eqn1, eqn2, eqn3}, [x, y, z]) | MySoln:=solve({eqn1, eqn2, eqn3}, [x, y, z]) | ||
− | + | </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 | ||
later. | later. | ||
− | + | ===Substituting Values=== | |
− | Now that you have the symbolic answers to the variables | + | 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 | 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 | of the known values, then tell Maple to substitute in the numerical | ||
− | values by using the built-in | + | values by using the built-in '''subs''' command. Add the following |
lines of code: | lines of code: | ||
− | + | <source lang=text> | |
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} | 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) | subs(Vals, MySoln) | ||
− | + | </source> | |
− | The list in | + | The list in '''MySoln''' will now be shown with numerical values |
− | instead of symbols. Note that you have | + | instead of symbols. Note that you have ''not'' made any actual |
changes to any of the variables - you have just asked Maple to show | changes to any of the variables - you have just asked Maple to show | ||
you what they would look like given the particular substitutions | you what they would look like given the particular substitutions | ||
− | presented in | + | 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 | substitute in a variety of values to see how one or more parameters | ||
influence a particular variable or variables. | influence a particular variable or variables. | ||
− | + | ===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 | + | 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 | + | could use those expressions later. Maple has a command called '''assign''' that does exactly that. Add the commands: |
− | + | <source lang=text> | |
− | |||
assign(MySoln) | assign(MySoln) | ||
x | x | ||
y | y | ||
z | z | ||
− | + | </source> | |
− | You will see that while the | + | You will see that while the '''assign''' command does not report |
− | anything back to you, when you ask Maple to tell you what | + | anything back to you, when you ask Maple to tell you what ''x'', ''y'', |
− | and | + | and ''z'' are, it responds with the symbolic representation produced in |
− | + | '''MySoln'''. This is very useful if, for example, the answer you are | |
− | looking for is some function of the variables | + | 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 | |
− | \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 | + | symbolic representation for ''alpha'': |
− | + | <source lang=text> | |
alpha:=x+y+z | alpha:=x+y+z | ||
− | + | </source> | |
Note, among other things, that Maple represents the variable named | Note, among other things, that Maple represents the variable named | ||
− | + | '''alpha''' as its symbol, <math>\alpha</math>. If you want a numerical value, | |
− | you can again use the | + | you can again use the '''subs''' command and the value list from |
before: | before: | ||
− | + | <source lang=text> | |
subs(Vals, alpha) | subs(Vals, alpha) | ||
− | + | </source> | |
− | + | ===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 | + | 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 | |
− | simple terms as necessary while | + | 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 | ||
− | |||
decimal digits to use. For example, | decimal digits to use. For example, | ||
− | + | <source lang=text> | |
simplify(expand(alpha)) | simplify(expand(alpha)) | ||
− | + | </source> | |
− | will produce the most symbolically simplified version of | + | will produce the most symbolically simplified version of <math>\alpha</math> while |
− | + | <source lang=text> | |
evalf[8](subs(Vals, alpha)) | evalf[8](subs(Vals, alpha)) | ||
− | + | </source> | |
− | will produce a floating point result for | + | will produce a floating point result for <math>\alpha</math>. With practice, you |
will see how best to combine {\tt evalf}, {\tt simplify}, and {\tt | will see how best to combine {\tt evalf}, {\tt simplify}, and {\tt | ||
expand} to get the form of answer you want. | 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 | + | 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 | + | 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''' | |
− | down the worksheet are being used. This is why the | ||
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 !!! | + | worksheet is the !!! button at the top of the window. |
− | + | ==Sample Circuit== | |
+ | <!-- re-add when images ready | ||
Each of the samples presented below will involve solving for the power | Each of the samples presented below will involve solving for the power | ||
− | absorbed by resistor | + | absorbed by resistor <math>R_{4}</math> in the circuit: |
\begin{center} | \begin{center} | ||
\epsfig{file=./XMAPLE/SampleStarter.ps, scale=0.5} | \epsfig{file=./XMAPLE/SampleStarter.ps, scale=0.5} | ||
Line 242: | Line 240: | ||
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{ | + | \frac{v_{x}-v_{y}}{R_{1}}+ |
− | \frac{ | + | \frac{v_{x}-v_{s}}{R_{2}}+ |
− | \frac{ | + | \frac{v_{x}-v_{z}}{R_{3}}+ |
− | \frac{ | + | \frac{v_{x}-0}{R_{4}}&=0\\ |
\mbox{KCL, n}_3&: & | \mbox{KCL, n}_3&: & | ||
− | \frac{ | + | \frac{v_{y}-v_{x}}{R_{1}}- |
− | + | i_{t}&=0\\ | |
\mbox{KCL, n}_4&: & | \mbox{KCL, n}_4&: & | ||
− | \frac{ | + | \frac{v_{z}-v_{x}}{R_{3}}+ |
− | \frac{ | + | \frac{v_{z}-0}{R_{5}}+ |
− | + | i_{t}&=0 | |
− | \end{align | + | \end{align} |
− | These three equations can be put into Maple | + | </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 | + | solve for the three unknowns, and the power absorbed by <math>R_{4}</math> |
− | will be the voltage across the resistor ( | + | will be the voltage across the resistor (<math>R_{x}</math>) |
squared divided by the resistance. | squared divided by the resistance. | ||
− | |||
+ | <!-- add later | ||
\subsection{Branch Current Method} | \subsection{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 | ||
Line 380: | Line 382: | ||
% 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 | ||
+ | --> |
Revision as of 16:36, 2 April 2010
Contents
Introduction
This page focuses on using Maple to find both the symbolic and the numeric solutions to equations obtained from electric circuits.
General Process of Solving Simultaneous Equations
Starting the Program
Maple is free to Duke students and resides on the OIT system in the same way that MATLAB does. To start Maple, make sure your terminal is set up to receive graphics and type
xmaple &
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
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 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. Let us assume that we want to solve the following equations:
where x, y, and z are unknowns, a through i are known coefficients, and j through l are known variables. To teach Maple about these equations, you would create three variables, each holding on to one of the equations. 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. 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.
Solving Equations With Maple
To solve the equations, all you need to do is use Maple's built in solve function. One 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 solve. In 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:
solve({eqn1, eqn2, eqn3}, [x, y, z])
and note that the equations are bracketed with curly braces while the 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 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
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:
solve({eqn1, eqn2, eqn3}, [x])
the answer would come back as empty because no value of $x$ satisfies the three equations for arbitrary values of the other 14 variables.
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 MySoln:= so it resembles:
MySoln:=solve({eqn1, eqn2, eqn3}, [x, y, z])
This will assign the solution list to a variable that we can use later.
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 subs command. 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, MySoln)
The list in MySoln will now be shown with numerical values instead 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.
Assigning Representations
There will be many times you actually want to assign the solutions 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 could use those expressions later. Maple has a command called assign that does exactly that. Add the commands:
assign(MySoln)
x
y
z
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, and z are, it responds with the symbolic representation produced in MySoln. This is very useful if, for example, the answer you are looking for is some function of the variables x, y, and z. Assuming that you have determined the variable you are looking for, alpha, is
you can now use the symbolic representations in Maple to generate a symbolic representation for alpha:
alpha:=x+y+z
Note, among other things, that Maple represents the variable named alpha as its symbol, \(\alpha\). If you want a numerical value, you can again use the subs command and the value list from before:
subs(Vals, alpha)
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(alpha))
will produce the most symbolically simplified version of \(\alpha\) while
evalf[8](subs(Vals, alpha))
will produce a floating point result for \(\alpha\). With practice, you will see how best to combine {\tt evalf}, {\tt simplify}, and {\tt
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.
Sample Circuit
Assume you have a circuit for which the KCL equations are:
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 solve for the three unknowns, and the power absorbed by \(R_{4}\) will be the voltage across the resistor (\(R_{x}\)) squared divided by the resistance.