Sign in to follow this  
FadedMaximus

help with solving matrices program

Recommended Posts

Hey I am trying to make a program to solve a system of 3 linear equations using matrices using rref. I can't seem to get the fourth matrix right and was wondering if somebody could point out what I was doing wrong. Thanks
#include <iostream>
#include <cctype>

using namespace std;

/* Function Prototypes */

void introduction();
void inputMatrix();
void outputMatrix();
void solveSystem();

/* Constants */

const int ROWS = 3;
const int COLUMNS = ROWS + 1;

/* Variables */

int matrix[ROWS][COLUMNS];

int main()
{
	char solveAgain;

	introduction();
	do {
		inputMatrix();
		outputMatrix();
		solveSystem();
		cout << "\nWould you like to solve another system? (y/n): ";
		cin >> solveAgain;
		solveAgain = toupper(solveAgain);
	} while (solveAgain == 'Y');

	return 0;
}

void introduction()
{
	cout << "\tEnter the Matrix!\n\n" << endl;
	cout << "This program will solve a system of " << ROWS << " equations with" << endl;
	cout << ROWS << " unknown varibles using row reduction echelon form." << endl;
	cout << endl;
	cout << "Enter the coefficients in this order:" << endl;
	cout << endl;
	cout << " 11 | 12 | 13 | 14 " << endl;
	cout << "-------------------" << endl;
	cout << " 21 | 22 | 23 | 24 " << endl;
	cout << "-------------------" << endl;
	cout << " 31 | 32 | 33 | 34 " << endl;
}

void inputMatrix()
{
	cout << endl;

	for (int i = 0; i < ROWS; ++i)
	{
		for (int j = 0; j < COLUMNS; ++j)
		{
			cout << "Matrix" << i + 1 << j + 1 << ": ";
			cin >> matrix[i][j];
		}
	}
}

void outputMatrix()
{
	cout << endl;

	for (int i = 0; i < ROWS; ++i)
	{
		for (int j = 0; j < COLUMNS; ++j)
		{
			cout << matrix[i][j] << "\t";
		}
		cout << endl;
	}
}

void solveSystem()
{
// first matrix
	for (int i = 1; i < ROWS; ++i)
	{
		double multiplier = matrix[i][0];
		for (int j = 0; j < COLUMNS; ++j)
		{
			matrix[i][j] = multiplier * matrix[0][j] - matrix[i][j] * matrix[0][0];
		}
	}
	outputMatrix();

// second matrix
	for (i = 2; i < ROWS; ++i)
	{
		double multiplier = matrix[i][1];
		for (int j = 1; j < COLUMNS; ++j)
		{
			matrix[i][j] = multiplier * matrix[1][j] - matrix[i][j] * matrix[1][1];
		}
	}
	outputMatrix();

// third matrix
	for (i = 2; i < ROWS; ++i)
	{
		double divider = matrix[i][2];
		for (int j = 2; j < COLUMNS; ++j)
		{
			matrix[i][j] = matrix[i][j] / divider;
		}
	}
	outputMatrix();

// fourth matrix
	for (i = 0; i < ROWS - 1; ++i)
	{
		double multiplier = matrix[2][2];
		for (int j = 0; j < COLUMNS; ++j)
		{
			matrix[i][j] = multiplier * matrix[i][j] - matrix[2][j] * matrix[i][2];
		}
	}
	outputMatrix();
}

Share this post


Link to post
Share on other sites
Hi FadedMaximus.

Your method is a little confusing: I'm not entirely sure what the four matrices are supposed to do. The first two matrices successfully reduce to upper-triangular form, but the second two are a mystery to me. It certainly doesn't look like they are moving towards echelon form. If you could explain the purposes of the four matrices then I may be able to produce something a bit closer to your original intention.

It's impossible to reduce the matrix to echelon form in general using integers, as the normalising divide would round everything to hell. Also, no attempts are made to ensure that this is possible, so divide-by-zero errors should be expected in the singular case.
Can you be sure that the division in 'third matrix' goes perfectly?

I suspect that your problem with the fourth matrix may be that matrix[2][2] and matrix[i][2] are the wrong way around:
// fourth matrix
for (i = 0; i < ROWS - 1; ++i)
{
double multiplier = matrix[i][2];
for (int j = 0; j < COLUMNS; ++j)
{
matrix[i][j] = multiplier * matrix[i][j] - matrix[2][j] * matrix[2][2];
}
}
but again, I'm not sure as I can't tell what it's supposed to do. In my test case, this brings the solution to very nearly echelon form, but I suspect that's only because I have a conveniently placed 1 at a[2][3].

Regards
Admiral

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this