genGaussSeidel
in genMath.lib
Solver Functions Function List by Category Alphabetical Function List
int genGaussSeidel(
float *a,
// pointer to A matrix data
float *t,
// pointer to initial guess t vector
float *b,
// pointer to 'right side,' b vector
int n,
// dimension of t and b vectors
float OptTol) //
convergence tolerance
This
function is used to solve a system of n linear equations in n unknowns.
The
Gauss-Seidel algorithm is used. Gauss-Seidel is a general procedure that
is useful
for many linear systems. However, other methods may be more efficient for
tridiagonal A matrices or other special cases. The general
Matrix-Vector equation
solved is of the form:
At = b
where
A is the coefficient matrix, t is the unknown vector and b
is the 'right side' vector.
That is, this Matrix-Vector equation is solved for the vector t.
The
user must initialize the vector t with a set of guess values. The
solver 'improves' upon
this guess for t, but not all starting values converge to a proper
solution. The solution vector
t is modified by the function.
The
return code is the number of iterations performed. The function is
hard-coded to exit
if not converged after 1000 iterations. Therefore, the user is encouraged
to test the return
for a value less than 1000 to determine that the routine has converged to a
solution. In
addition, the return value can be used to determine the Gauss-Seidel efficiency
for a
particular problem, as compared to another system solver. In other words,
you may compare
the number of iterations required by Gauss-Seidel to that required by another
method. The
routine achieving convergence in fewer iterations is more efficient for that
particular problem.
Sample Application:
//////////// splGS.c for genGaussSeidel Library function ////////// // // // As a sample, the 3-d linear system solved is: // // // // 6t1 + 1t2 + 1t3 = 10.7// // 1t1 + 9t2 - 2t3 = 3.6 // // 2t2 - 1t2 + 8t3 = 12.1 // // // // The initial guess for t = (1,1,1) and the convergence // // tolerance is 0.00001. // // // ///////////////////////////////////////////////////////////////////
////////////////////////// INCLUDES ///////////////////////////////
#include "stdio.h"
#include "genMath.h"
////////////////////////////// Main //////////////////////////////
void main()
{
//local declarations //integers int i; // dimension counter int retcode; // return code for call to genGausSeidel
//floats float a[3][3]; // matrix A float b[3]; // vector b float t[3]; // initial guess vector t; solution is // returned here as well
//character strings char ch; //input string to exit
//intialize the A matrix and b vector a[0][0] = (float)6; a[0][1] = (float)1; a[0][2] = (float)1;
a[1][0] = (float)1; a[1][1] = (float)9; a[1][2] = (float)-2;
a[2][0] = (float)2; a[2][1] = (float)-1; a[2][2] = (float)8;
b[0] = (float)10.7; b[1] = (float)3.6; b[2] = (float)12.1;
//initialize initial guess for t vector t[0] = (float)1; t[1] = (float)1; t[2] = (float)1;
//call the solver retcode = genGaussSeidel(&a[0][0],t,b,3, (float)0.00001);
//display the result printf("%s", "System Solved using Gauss Seidel: \n\n");
for(i=0;i<=2;++i){
printf("%s%d%s%f%s", "t[",i,"] = ", t[i],"\n");
}
printf("%s","\n"); printf("%s%d%s", "Iterations to converge: ", retcode, "\n\n");
//display closing message and wait for <enter> before exiting printf("Press <Enter> to end"); scanf("%c", &ch);
} // End Main ///////////////////////// End splGS.c ////////////////////////////
The exact solution for t is (1.5, 0.5, 1.2). The reader may compare the exact solution to the following numerical results.
Ouput from the test program splGS.c:
System Solved using Gauss Seidel
t[0] = 1.500001
t[1] = 0.500000
t[2] = 1.200000
Iterations to Converge: 7