C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-30-2009, 03:37 PM   #1
Registered User
 
Join Date: May 2009
Posts: 36
Help with output

Have the following Gauss Elim code from text:

Not printing out correct code:


SOLUTION OF SIMULTANEOUS LINEAR EQUATIONS
USING GAUSSIAN ELIMINATION

This program uses Gaussian Elimination to solve the
system Ax = B, where A is the matrix of known
coefficients, B is the vector of known constants
and x is the column matrix of the unknowns.
Number of equations: 3

Enter elements of matrix [A]
A(1,1) = 0
A(1,2) = -6
A(1,3) = 9
A(2,1) = 7
A(2,2) = 0
A(2,3) = -5
A(3,1) = 5
A(3,2) = -8
A(3,3) = 6

Enter elements of [b] vector
B(1) = -3
B(2) = 3
B(3) = -4

SOLUTION OF SIMULTANEOUS LINEAR EQUATIONS

The solution is
x(1) = 0.000000
x(2) = -1.#IND00
x(3) = -1.#IND00
Determinant = -1.#IND00
Press any key to continue . . .

Should be 1.058824, 1.823529, 0.882353, det= -102.0000

Any suggestions?- I tried to check- need fresh set of eyes

Code:
//Modified Code from C Numerical Methods Text

#include <stdio.h>
#include <math.h>
#define MAXSIZE 20

//function prototype
int gauss (double a[][MAXSIZE], double b[], int n, double *det);

int main(void)
{
    double a[MAXSIZE][MAXSIZE], b[MAXSIZE], det;
    int i, j, n, retval;
    
	printf("\n \t SOLUTION OF SIMULTANEOUS LINEAR EQUATIONS");
	printf("\n \t USING GAUSSIAN ELIMINATION \n");
	printf("\n This program uses Gaussian Elimination to solve the");
	printf("\n system Ax = B, where A is the matrix of known");
	printf("\n coefficients, B is the vector of known constants");
	printf("\n and x is the column matrix of the unknowns.");

	//get number of equations
	n = 0;
    while(n <= 0 || n > MAXSIZE)
	{
		printf("\n Number of equations: ");
		scanf ("%d", &n);
	}

	//read matrix A
	printf("\n Enter elements of matrix [A]\n");
	for (i = 0; i < n; i++)
		for (j = 0; j < n; j++)
		{
			printf(" A(%d,%d) = ", i + 1, j + 1);
			scanf("%lf", &a[i][j]);
		}
    //read {B} vector
    printf("\n Enter elements of [b] vector\n");
	for (i = 0; i < n; i++)
	{
		printf(" B(%d) = ", i + 1);
		scanf("%lf", &b[i]);
	}
    
	//call Gauss elimination function
	retval = gauss(a, b, n, &det);

	//print results
	if (retval == 0)
	{
		printf("\n\t SOLUTION OF SIMULTANEOUS LINEAR EQUATIONS\n");
		printf("\n\t The solution is");
		for (i = 0; i < n; i++)
			printf("\n \t x(%d) = %lf", i + 1, b[i]);
		printf("\n \t Determinant = %lf \n", det);
	}
	else
		printf("\n \t SINGULAR MATRIX \n");

    return 0;
 }

/* Solves the system of equations [A]{x} = {B} using       */
/* the Gaussian elimination method with partial pivoting.  */
/* Parameters:                                             */
/*      n         - number of equations                    */
/*      a[n][n]   - coefficient matrix                     */
/*      b[n]      - right-hand side vector                 */
/*      *det      - determinant of [A]                     */

int gauss (double a[][MAXSIZE], double b[], int n, double *det)
{
    double tol, temp, mult;
	int npivot, i, j, l, k, flag;

	//initialization
	*det = 1.0;
	tol = 1e-30;        //initial tolerance value
	npivot = 0;

	//forward elimination
	for (k = 0; k < n; k++)
	{
		//search for max coefficient in pivot row- a[k][k] pivot element
		for (i = k; i < n; i++)
		{
			if (fabs(a[i][k]) > fabs(a[k][k]))
			{
				//interchange row with maxium element with pivot row
				npivot++;
				for (l = 0; l < n; l++)
				{
				   temp = a[i][l];
				   a[i][l] = a[k][l];
				   a[k][l] = temp;
				}
				temp = b[i];
				b[i] = b[k];
				b[k] = temp;
			}
		}
		//test for singularity
		if (fabs(a[k][k]) < tol)
		{
           //matrix is singular- terminate
		   flag = 1;
		   return flag;
		}
        //compute determinant- the product of the pivot elements
		*det = *det * a[k][k];

		//eliminate the coefficients of X(I)
		for (i = k; i < n; i++)
		{
			mult = a[i][k] / a[k][k];
			b[i] = b[i] - b[k] * mult;  //compute constants
			for (j = k; j < n; j++)     //compute coefficients
				a[i][j] = a[i][j] - a[k][j] * mult;
		}
	}
	//adjust the sign of the determinant
	if(npivot % 2 == 1)
	  *det = *det * (-1.0);

	//backsubstitution
	b[n] = b[n] / a[n][n];
	for(i = n - 1; i > 1; i--)
	{
		for(j = n; j > i + 1; j--)
			b[i] = b[i] - a[i][j] * b[j];
		b[i] = b[i] / a[i - 1][i];
	}
	flag = 0;
	return flag;
}
ninety3gd is offline   Reply With Quote
Old 05-30-2009, 05:29 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
I'm thinking you should find the largest value in a given column first, then swap that with to the correct position -- as it is you're doing who knows how many swaps, which can affect the sign of your determinant. (I don't see how that fixes the problem, so there's bound to be something else as well.)
tabstop is offline   Reply With Quote
Old 05-30-2009, 05:49 PM   #3
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Your eliminate coefficients loop starts one row too early -- you always wipe out the row you're trying to keep before starting, meaning that every other entry becomes x/0.
tabstop is offline   Reply With Quote
Old 05-30-2009, 06:19 PM   #4
Registered User
 
Join Date: May 2009
Posts: 36
Code:
//eliminate the coefficients of X(I)
		for (i = k + 1; i < n; i++)
		{
			mult = a[i][k] / a[k][k];
			b[i] = b[i] - b[k] * mult;  //compute constants
			for (j = k; j < n; j++)     //compute coefficients
				a[i][j] = a[i][j] - a[k][j] * mult;
		}
	}


SOLUTION OF SIMULTANEOUS LINEAR EQUATIONS
USING GAUSSIAN ELIMINATION

This program uses Gaussian Elimination to solve the
system Ax = B, where A is the matrix of known
coefficients, B is the vector of known constants
and x is the column matrix of the unknowns.
Number of equations: 3

Enter elements of matrix [A]
A(1,1) = 0
A(1,2) = -6
A(1,3) = 9
A(2,1) = 7
A(2,2) = 0
A(2,3) = -5
A(3,1) = 5
A(3,2) = -8
A(3,3) = 6

Enter elements of [b] vector
B(1) = -3
B(2) = 3
B(3) = -4

SOLUTION OF SIMULTANEOUS LINEAR EQUATIONS

The solution is
x(1) = 3.000000
x(2) = -6.142857
x(3) = 0.167910
Determinant = -102.000000
Press any key to continue . . .

The determinate is correct now- but the solution does not match the one got be hand which is correct...what else needs to be changed elsewhere...I have tried...
ninety3gd is offline   Reply With Quote
Old 05-30-2009, 06:21 PM   #5
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
I suppose your backsubstitution starts in the wrong place as well. (There is no such thing as row n in a nxn matrix in C.)
tabstop is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
code output... roaan C Programming 6 07-03-2009 02:22 AM
Help for my output array qwertysingh C Programming 1 02-17-2009 03:08 PM
More digits in output, particular digits output, output in *.txt Ene Dene C++ Programming 4 11-30-2005 04:44 PM
Formatting output into even columns? Uncle Rico C Programming 2 08-16-2005 05:10 PM
Output problems with structures Gkitty C Programming 1 12-16-2002 05:27 AM


All times are GMT -6. The time now is 07:12 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22