GAUSS ELIMINATION
Gauss elimination is a procedure used to solve systems of linear equations.
The basic procedure involves using elementary row operations to transform the system of
equations into a "triangularized matrix, and then to solve for the unknowns using back
substitution.
Consider the following example:
Subroutines for Project
SUBROUTINE REDUCE (A,B,M)
****************************************
* A is the matrix of coefficients
* B is the right hand side vector
* M is the number of unknowns
***************************************
DIMENSION A(9,9), B(9)
J2=M-1
DO 1 J=1,J2
I1=J+1
Q=-1.0/A(J,J)
DO 1 I=I1,M
C=A(I,J)*Q
B(I)=B(I)+B(J)*C
DO 1 K=J,M
1 A(I,K)=A(I,K)+A(J,K)*C
RETURN
END
SUBROUTINE BAKSUB (A,B,X,M)
DIMENSION A(9,9),B(9),X(9)
****************************************
* A is the matrix of coefficients
* B is the right hand side vector
* X is the vector of unknowns
* M is the number of unknowns
***************************************
X(M)=B(M)/A(M,M)
DO 2 I1=2,M
I=M-I1+1
J1=I+1
DO 1 J=J1,M
1 B(I)=B(I)-A(I,J)*X(J)
2 X(I)=B(I)/A(I,I)
RETURN
END
SUBROUTINES
Last lecture we briefly discussed the use of subroutines in FORTRAN programming to simplify and
improve the organization of the program. The subroutine is valuable for problems in which a group
of statements are repeated in the program. Instead of repeating the set of instructions again and
again, the instructions can be placed in a subroutine and referenced by the main program when
needed. The subroutine is referenced from the main program with the following statement:
CALL subroutine name1 (argument list1)
This statement will reference the subroutine which has the first line consisting of :
SUBROUTINE subroutine name1 (argument list1)
There are a number of rules regarding writing and using subroutines:
1. The name of the subroutine should be chosen based on organizational criteria.
The name of the subroutine has nothing to do with the type of data (real or integer)
associated with the subroutine.
2. The first line of the subroutine identifies it as a subroutine, assigns a name
which will be used to reference the subroutine, and also contains a list of arguments.
3. The list of arguments are necessary to transfer data and variables (input) to
the subroutine as well as returning values to the calling program. The arguments which are used in the
CALL statement are the actual arguments while the arguments in the SUBROUTINE
statement are the dummy arguments. The arguments in the CALL statement must match in type,
number and order those used in the subroutine
definition. The actual variable names do not have to match, however if the first variable in the CALL
statement is a real array, the first dummy argument in the SUBROUTINE statement must also be a real array.
4. A subroutine may return one or more values, or no values.
5. The subroutine is a separate program from the main program and the only link between the two
programs is the list of arguments. Variables used within a subroutine are therefore independent of variables
used in the main program or in other subroutines. Any variables which are used in the subroutine that
are not arguments are local variables, and the values are not accessible from the main program. Therefore
if we have a variable named LENGTH in the main program, and the value of LENGTH is not transferred
in the list of arguments, we can also have a variable LENGTH in the subroutine which is independent of
the variable in the main program.
6. Be very careful with the dimension statements for multidimensional arrays in subroutines. You
should always pass the dimensional size [DIMENSION A(100,100)], and the size which actually used
[NROWS, NCOLS] as arguments.
For example: Say that we have an array A in the main program which has been
dimensioned:
DIMENSION A(10,10)
But we only have NCOLS=2, and NROWS=3 which are being used. If we have
a subroutine SORT which we are transferring the array A in:
CALL SORT (A,NCOLS,NROWS)
In the subroutine we have (note the dummy arguments in the subroutine use NC for
NCOLS and NR for NROWS)
SUBROUTINE SORT(A,NC,NR)
DIMENSION A(NR,NC)
.
.
RETURN
END
We will get very poor results from our subroutine operation. Values in an array are actually
stored in columns. When we dimension an array 10 x 10 we are reserving 100 locations in
memory for A. The dimension statement in the SUBROUTINE SORT is only obtaining the
first 6 (NC x NR) values from memory. Since the array is only stored in columns, we only be
getting the first 6 values from column one.
To avoid this you should always provide the same array dimensioning in the main program as well
as in subroutines.
7. Subroutines require a RETURN statement to return control to the main program or another subroutine
which calls it. An END statement is also required.
8. A subroutine may reference other subroutines, however it cannot reference itself.
FILE COMPRESSION
Throughout the semester we have been using "floppy" disks to save our files so that we can carry
the files with us. Transferring files to the 3.5" disks allows us to transfer files to other computers not
on the network, as well as archiving data or other work that we have completed. The amount of data
that we can store on a disk is approximately 1.44 Mb. In many situations we can actually get files
larger than 1.44 Mb, or we may have a couple of related files which easily exceed 1.44 Mb. In order
to archive the data on a diskette we typically use a software which compresses the files to minimize
the space on the disk that the file occupies.
PKZIP:
PKZIP is a "shareware" product that is used to compress files to minimize the diskspace required.
The actual program has extensive features that you can read about in the file named "manual.doc.
It is not necessary, however, to learn about all of the features to use PKZIP as a very helpful tool.
The files necessary for using pkzip are all in a subdirectory on FISH:
UHCOURSES\CIVE1331\FALL96\HELWIG\PKZIP. There are approximately 16 files in the pkzip
software. There are four executable files, however we will predominantly be using two of the
executable files: PKZIP.EXE and PKUNZIP.EXE. The easiest way to use pkzip and the related
executables is to place the path to the subdirectory pkzip in the autoexec.bat file on your computer.
For example if the software for pkzip is in a subdirectory with a path named C:\USER\APPS\PKZIP
we would put this path in our autoexec.bat file with a path option:
For example:
PATH=C:\;C:\WINNT35;C:\win32app;C:\USER\APPS\PKZIP
This is a line within the autoexec.bat file (which usually exists in the root directory C:\) that tells the
computer the name of directories to search for commands which are issued in DOS. For example, if
the command PKZIP was issued in any subdirectory within DOS, the computer will first try to
find PKZIP.EXE within the current subdirectory which is being used, it will then go to the
autoexec.bat file and start looking in the directories within PATH=. In the case with the above
PATH line in the autoexec.bat file will look first in C:\ then in C:\WINNT35, then in C:\win32app,
then in C:\USER\APPS\PKZIP until it finds the PKZIP.EXE file.
In the remainder of this file we will assume that you don't have the subdirectory which contains
PKZIP and so you will have to copy pkzip.exe or pkunzip.exe to the subdirectory that you are working in.
To compress our files we use PKZIP.EXE. If we are in a particular subdirectory and we wish to
compress the contents to a file named TODD.ZIP on the a: directory, we can copy PKZIP.EXE
to the particular subdirectory and type:
pkzip a:\todd.zip
If we later want to uncompress the files on another computer, we copy the file PKUNZIP.EXE to
the directory that we want to place the files, insert the disk into the a: drive and type:
PKUNZIP a:\todd.zip
The files will then be extracted and placed in the particular subdirectory.
In many situations we may wish to also compress and maintain a directory structure. For example
say we are in a subdirectory named ONEDIR that also contains a number of subdirectories. If we
wish to compress all of the contents of the directory ONEDIR as well as the contents in any subdirectories
we would first copy the file PKZIP.EXE to the subdirectory ONEDIR and type:
PKZIP -rp a:\todd.zip
this will zip all of the files in the current directory and the subdirectories into the file todd.zip on the a:\ drive.
This command will also maintain the directory structure when we unzip the files if we use a -d
option on PKUNZIP. For example if we wish to unzip the files on another computer and maintain
the directory structure. We first create the directory where we want to unzip the file, say that this
is in NEWDIR.
MD NEWDIR - makes a new directory named NEWDIR
CD NEWDIR - changes directories to the new directory
copy c:\users\apps\pkzip\pkunzip.exe - copies pkunzip.exe to the
new directory.
pkunzip -d a:\todd.zip this will unzip all of the files which were zipped into todd.zip and also maintain
the directory structure.
In many situations, we may have so many files to compress that the zipped file is too large to fit on a single
1.44 Mb diskette. In these situations we must tell the software pkzip to span disks if necessary. This is
done with the -& option. In the previous example if we wish to span disks we would just add the -& option.
For example:
PKZIP -& -rp a:\todd.zip
The computer will then prompt you for disks as each one gets full. Each disk that is used will have a file
named todd.zip on it. You should also number each disk on the label so that you know the order of the
disks. When you wish to unzip the files you will put the first disk in the a:\ drive and type:
pkunzip -d a:\todd.zip
The computer will recognize that this is a multiple disk file and ask you to put the last disk of the set in the
computer. It will then ask you to put the first disk in a:\ and hit any key when ready. After the computer has
extracted all of the files, it will ask you for the second disk……………
Summary:
pkzip -rp -& a:\filename.zip
-rp recurses subdirectories
-& spans disks if necessary
a:\filename.zip name of the file which will
contain the compressed files.
pkunzip -d a:\filename.zip
-d maintains the directory structure if the -rp
option was used on zipping the files.
OPTIMIZATION PROBLEMS
Pages 277-285, 291-297 in Gottfried
In many problems in engineering we try to find the optimum solution. The optimum solution may
maximize profits, maximize success, minimize cost, minimize the amount of material, etc.
In order to formulate an optimization problem we must first develop and objective function.
OBJECTIVE FUNCTION: An objective function establishes a relationship which is to be optimized
as a function of a series of independent variables. y = f(x1, x2, x3,….xi)
The independent variables xi are the selected parameters which will be varied to maximize or minimize
the objective function. For example, if profits are to be maximized for a company that makes two
products A and B, x1 may be set equal to the number of units of product A, and x2 may be set equal
to the number of units of product B. To maximize profits we may write the objective function y as a
function of the cost per unit of the two products. If A costs $5 a unit and B costs $10 a unit
y = 5 x1 + 10 x2
CONSTRAINTS:
In most problems the objective function must also satisfy some required constraints. The constraints
are applied on the independent variables. A constraint may be an equality or an inequality. For example,
perhaps there is a limit on the total number of hours to be worked.
g(x) < limiting number of hours
A constraint that will apply to many problems is that the independent variables be positive. xi > 0.
Example 10.1 from Gottfried:
Consider a company that makes Product A and Product B:
Product A - Sold for $120 per unit, requires 5 hours labor/unit.
Product B - Sold for $80 per unit, requires 3 hours labor/unit.
Labor costs $12/hour
Total available hours of labor = 8000 hours/month
Required that at least 1000 units/month be manufactured.
Determine a monthly production schedule that will maximize the companies profit.
x1 = units of product A manufactured each month
x2 = units of product B manufactured each month
We want to maximize the profits associated with these two
variables. We therefore need to develop an objective solution
which will represent the profit associated with these two products.
In order to do this we need to determine the net profit
associated with each product.
Product A sells for $120 but takes 5 hours to make.
The net profit per unit is therefore equal to
$120 - 5($12) = $60/unit
Product B sells for $80 but takes 3 hours to make.
The net profit per unit is therefore equal to
$80 - 3($12) = $44/unit
Our objective function for the profit associated with these
two products is therefore:
y = 60 x1 + 44x2 (1)
Apply the constraints:
·At least 1000 units/month must be manufactured.
x1 + x2 ³ 1000 (2)
·The total number of hours available each month is 8000.
In order to consider this restriction we must consider
how much time it takes to make each product.
Product A - 5 hours per unit
Product B - 3 hours per unit
5x1 + 3x2 £ 8000 (3)
·Both x1 and x2 must each be positive:
x1 ³ 0 (4a)
x2 ³ 0 (4b)
We now want to determine values of x1 and x2 that will maximize
our objective function (y) while also satisfying the constraints
of the problem.
GRAPHICAL SOLUTION :
One way to solve this problem is to solve it graphically. A
graphical solution is really only practical for a 2 dimensional
problem. Higher dimensional problems will usually require a
mathematical formulation.
· Graph the constraints Eqns. 2-4. The constraints represent the
feasible region for the solution by establishing some boundaries.
The boundaries may or may not totally bound the solution. For
example, the constraints may establish a closed polyhedron, or it
may result in an open-ended feasible region with infinite solutions.
The constraints for our problem our represented by the lines given
in Eqns. 2-4. In order to graph these lines on Excel we need to
establish two points on the line. This can be established by
setting one of the variables (x1 or x2 ) equal to zero and solving
for the value of the other variable. For example:
Constraint #1:
x1 + x2 ³ 1000
For x1 = 0, x2 = 1000
For x2 = 0, x1 = 1000
Constraint #2:
5x1 + 3x2 £ 8000
For x1 = 0, x2 = 8000/3 = 2667
For x2 = 0, x1 = 8000/5 = 1600
Constraint #3: x1 and x2 must be greater than zero. This is
established by the axes.
To solve our problem graphically, we must consider Equation 1, our
objective function.
y = 60 x1 + 44x2
Say for y = 10000
60x1 + 44x2 = 100000
For x1 = 0, x2 = 100000/44 = 2273
For x2 = 0, x1 = 100000/60 = 1667
We can set up our spreadsheet so that as we change our guess for "y"
we update the points for our graph for our optimum solution.
Excel Solver
While the graphical solution provides a good understanding of the
solution process for optimizing solutions, for problems with more
than two optimizing variables we must go to a mathematical solution.
Excel has an Add-on solver which can be used to solve optimization
problems.
Choose Tools, Add-ins, Solver Add-in.
This loads the Solver into the Tools menu Bar.
We then Set up the Sheet with the appropriate objective function, and constraints.
1. We designate cells which will be the independent variables
which the solver will vary.
2. We designate a cell which will contain the objective function.
3. We then put in the expressions involving the independent variables
(not the inequality, or equality)
into another cell.
4. We then select the Solver and it will walk us through the
remaining steps.
We want to graph this line for various values of y ( increasing profit).
Since we want to maximize the profit, we want to select the value of
the line for which is touches the highest point on our feasible region.
To find two points on this line we again evaluate it at a few locations:
solve our objective
A company manufactures two products, A and B, which can be sold for
$120 per unit and $80 per unit, respectively. It is required that
at least 1000 units be manufactured each month.