Gauss-Jordan
This routine uses Gauss-Jordan elimination (full pivoting).
Here is a C# sample code:
Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Gaussian_elimination
{
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private Solve Linear_system; // Add reference for Solve.dll
public Form1()
{
InitializeComponent();
Linear_system=new Solve(); // Class initialization
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(248, 190);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
Application.EnableVisualStyles();
}
private void Form1_Load(object sender, System.EventArgs e)
{
double[] solutions=new double[4];
double[,] a=new double[4,5]; // Let's make a 3x3 linear system
a[1,1]=10; a[1,2]=22.5; a[1,3]=71.25; a[1,4]=-46.57;
a[2,1]=22.5; a[2,2]=71.25; a[2,3]=253.125; a[2,4]=-141.075;
a[3,1]=71.25; a[3,2]=253.125; a[3,3]=958.3125; a[3,4]=-487.4925;
solutions=Linear_system.Gauss_Jordan(a,3,4,5);
if (Linear_system.Gauss_Jordan_solutions() != 0)
{
for (int i=1; i<=3; i++)
solutions[i]=Math.Round(solutions[i],5);
MessageBox.Show("x1 = " + solutions[1].ToString() + "\nx2 = " + solutions[2].ToString()
+ "\nx3 = " + solutions[3].ToString(),"Solutions are");
}
else
MessageBox.Show("No solution can be found!","Ill conditioned problem!",
MessageBoxButtons.OK,MessageBoxIcon.Error);
this.Close();
}
}
}
Bookmarks