Circuit Solver: Specifying Systems of Linear Equations
1) Getting Started
Template code for this exercise (and those that follow) is available in zip format here. You should do your work in these files and save it on your computer, as some of the following exercises (both this week and next) will build on this work.
2) Interface
The interface to the gauss_solve function is a little bit obtuse, and
requires manually converting a set of linear equations to matrix form. We
would like to avoid this extra bit of complication and provide an easier
interface for setting up and solving systems of linear equations, which allows
us to think about individual variables, rather than the matrix representation.
Evantually, we will use this method to solve systems of equations as follows:
answer = solve_equations(eqnlist)
where eqnlist is a list of equations to be solved.
We can think of each equation as a list of terms that sum to zero. We will
represent an equation by a list of tuples of the form (coefficient, variable)
where coefficient is a number and variable is a string that represents the
name of a variable, so that each tuple represents a term of the form
coefficient*variable. If variable is None, then the associated
coefficient is taken to be a constant term.
In the previous exercise, we looked at the following system of equations:
The following code specifies the equations from this example, arguably in a more natural way than as a matrix:
eq1 = [(5,'x'), (-2,'y'), (-3,None)]
eq2 = [(3,'x'), (4,'y'), (-33,None)]
The following code solves eq1 and eq2:
answer = solve_equations([eq1, eq2])
Printing answer would show {'x': 3.0, 'y': 6.0}.
3) Implement
The solve_equations procedure should return a dictionary that maps variable
names to values. That is quite a task, so, you may find it helpful to break
this problem into a few pieces:
- convert the equation list into a matrix representation (as in the previous problem),
- invoke the
gauss_solvefunction to solve the system, and - convert the resulting list into a dictionary mapping variable names to values.
Implement the solve_equations function in le.py. Your definition will rely
on gauss_solve; if you have not yet finished the previous exercise, you can
import a working copy of gauss_solve to use for debugging purposes by adding
the following to the top of your file:
from lib601.le import gauss_solve
Once you are finished, upload your le.py file below.
le.py:
No file selected