C# .Net Program to Solve Linear Equations

by Namir Shammas

The following program solves a system of linear equations:

A X = B

Where A is the matrix of coefficients, and B and X are the coefficient and solution vectors.

Click here to download a ZIP file containing the project files for this program.

The program is menu driven. The menu prompts which is:

   Solve a system of linear equations
   ==================================
0) Quit
1) New system
2) Edit matrix A
3) Edit vector B
4) View coefficients
7) Solve
Select choice by number: 
Asks you to enter one of the following choices:

1. A new set of equations. The program prompts you to enter the number of equations, the elements of matrix A, and the element of vector B.

2. Edit an element in the matrix A. This choice first prompts you to enter the row and column indices for the element in A. The program then displays the current value you are editing and prompts you to enter a new value.

3. Edit an element in the vector B. This choice first prompts you to enter index for the element in B. The program then displays the current value you are editing and prompts you to enter a new value.

4. View the coefficients. The program displays the elements of matrix A and vector B.

5. Solve the set of linear equations. The program displays the elements of solution vector X.

You select from the above menus by entering the menu number.

Here is an example to solve the following equations:
 

X1 + X2 + X3 = 6
X1 + 1.1 X2 + X3 = 6.2
X1 + X2 + 1.1 X3 = 6.3

Here is the first screen that shows the input for the above example:

The second screen shows the viewing of the coefficients, followed by solving the linear equations:

Here is the listing of the main module. The program also uses the matrix class library (MatrixLib.vb) which you can download from the VB.Net: Master Page:

using System.Diagnostics;
using System.Data;
using System.Collections;
using Microsoft.VisualBasic;
using System.Collections.Generic;
using System;

namespace SLE1
{
	sealed class Module1
	{
		
		static public void Main()
		{
			int n = 1;
			double[] X = new double[2];
			double[] vecB = new double[2];
			double[,] matA = new double[2, 2];
            double[,] matA2 = new double[2, 2];
			int i;
			int j;
			int nChoice;
			string sInput;
			
			do
			{
				Console.WriteLine("       Solve a system of linear equations");
				Console.WriteLine("       ==================================");
				Console.WriteLine("0) Quit");
				Console.WriteLine("1) New system");
				Console.WriteLine("2) Edit matrix A");
				Console.WriteLine("3) Edit vector B");
				Console.WriteLine("4) View coefficients");
				Console.WriteLine("5) Solve");
				Console.Write("Select choice bu number: ");
				sInput = Console.ReadLine();
				if (sInput.Length > 0)
				{
					nChoice = int.Parse(sInput);
				}
				else
				{
					nChoice = - 1;
				}
				Console.WriteLine();
				switch (nChoice)
				{
					case 0:
						
						Console.WriteLine("Bye!");
						break;
					case 1:
						
						Console.Write("Enter number of equations? ");
						n = int.Parse(Console.ReadLine());
						X = new double[n];
						vecB = new double[n];
						matA = new double[n, n];
                        matA2 = new double[n, n];
						for (i = 0; i < n; i++)
						{
							for (j = 0; j < n; j++)
							{
								Console.Write("Enter A({0},{1})? ", i + 1, j + 1);
								matA[i, j] = double.Parse(Console.ReadLine());
							}
							Console.Write("Enter B({0})? ", i + 1);
							vecB[i] = double.Parse(Console.ReadLine());
						}
						break;
					case 2:
						
						Console.Write("Enter value of index i? ");
						i = int.Parse(Console.ReadLine()) - 1;
						Console.Write("Enter value of index j? ");
						j = int.Parse(Console.ReadLine()) - 1;
						Console.Write("Enter new value of A({0},{1})? ({2}): ", i + 1, j + 1, matA[i, j]);
						sInput = Console.ReadLine();
						if (sInput.Length > 0)
						{
							matA[i, j] = double.Parse(sInput);
						}
						break;
					case 3:
						
						Console.Write("Enter value of index i? ");
						i = int.Parse(Console.ReadLine()) - 1;
						Console.Write("Enter new value of B({0})? ({1}): ", i + 1, vecB[i]);
						sInput = Console.ReadLine();
						if (sInput.Length > 0)
						{
							vecB[i] = double.Parse(sInput);
						}
						break;
					case 4:
						
						for (i = 0; i < n; i++)
						{
							for (j = 0; j < n; j++)
							{
								Console.WriteLine("A({0},{1}) = {2} ", i + 1, j + 1, matA[i, j]);
							}
							Console.WriteLine("B({0}) = {1} ", i + 1, vecB[i]);
						}
						break;
					case 5:
						
						for (i = 0; i < n; i++)
						{
                            for (j = 0; j < n; j++)
                            {
                                matA2[i, j] = matA[i, j];
                            }
							X[i] = vecB[i];
						}
						MatrixLibVb.SolveLU(ref matA2, ref X, n);
						Console.WriteLine("Solution is:");
						for (i = 0; i < n; i++)
						{
							Console.WriteLine("X({0}) = {1}", i + 1, X[i]);
						}
						break;
				}
				
				if (nChoice > 0)
				{
					Console.WriteLine();
					Console.Write("Press Enter to resume");
					Console.ReadLine();
					Console.WriteLine();
				}
				
			} while (!(nChoice == 0));
			
		}
		
	}
	
}

BACK

Copyright (c) Namir Shammas. All rights reserved.