最近数值计算学了Guass列主消元法和三角分解法解线性方程组,具体原理如下:

1、Guass列选主元消去法对于AX =B

1)、消元过程:将(A|B)进行变换为,其中是上三角矩阵。即:

k从1到n-1

a、 列选主元

选取第k列中绝对值最大元素作为主元。

b、 换行

c、 归一化

d、 消元

2)、回代过程:由解出。

2、三角分解法(Doolittle分解)

将A分解为如下形式

由矩阵乘法原理

a、计算U的第一行,再计算L的第一列

b、设已求出U的1至r-1行,L的1至r-1列。先计算U的第r行,再计算L的第r列。

a)计算U的r行

b)计算L的r列

C#代码:

  代码说明:Guass列主消元法部分将计算出来的根仍然储存在增广矩阵的最后一列,而Doolittle分解,将分解后的结果也储存至原来的数组中,这样可以节约空间。。

using System;
using System.Windows.Forms; namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Cannel_Button_Click(object sender, EventArgs e)
{
this.textBox1.Clear();
this.textBox2.Clear();
this.textBox3.Clear();
this.comboBox1.SelectedIndex = -1;
}
public double[,] GetNum(string str, int n)
{
string[] strnum = str.Split(' ');
double[,] a = new double[n, n + 1];
int k = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < strnum.Length / n; j++)
{
a[i, j] = double.Parse((strnum[k]).ToString());
k++;
}
}
return a;
}
public void Gauss(double[,] a, int n)
{
int i, j;
SelectColE(a, n);
for (i = n - 1; i >= 0; i--)
{
for (j = i + 1; j < n; j++)
a[i, n] -= a[i, j] * a[j, n];
a[i, n] /= a[i, i];
}
}
//选择列主元并进行消元
public void SelectColE(double[,] a, int n)
{
int i, j, k, maxRowE;
double temp; //用于记录消元时的因数
for (j = 0; j < n; j++)
{
maxRowE = j;
for (i = j; i < n; i++)
if (System.Math.Abs(a[i, j]) > System.Math.Abs(a[maxRowE, j]))
maxRowE = i;
if (maxRowE != j)
swapRow(a, j, maxRowE, n); //与最大主元所在行交换
//消元
for (i = j + 1; i < n; i++)
{
temp = a[i, j] / a[j, j];
for (k = j; k < n + 1; k++)
a[i, k] -= a[j, k] * temp;
}
}
return;
}
public void swapRow(double[,] a, int m, int maxRowE, int n)
{
int k;
double temp;
for (k = m; k < n + 1; k++)
{
temp = a[m, k];
a[m, k] = a[maxRowE, k];
a[maxRowE, k] = temp;
}
}
public void Doolittle(double[,] a, int n)
{
for (int i = 0; i < n; i++)
{
if (i == 0)
{
for (int j = i + 1; j < n; j++)
a[j, 0] = a[j, 0] / a[0, 0];
}
else
{
double temp = 0, s = 0;
for (int j = i; j < n; j++)
{
for (int k = 0; k < i; k++)
{
temp = temp + a[i, k] * a[k, j];
}
a[i, j] = a[i, j] - temp;
}
for (int j = i + 1; j < n; j++)
{
for (int k = 0; k < i; k++)
{
s = s + a[j, k] * a[k, i];
}
a[j, i] = (a[j, i] - s) / a[i, i];
}
}
}
}
private void Exit_Button_Click(object sender, EventArgs e)
{
this.Close();
} private void Confirm_Button_Click(object sender, EventArgs e)
{
if (this.textBox2.Text.Trim().ToString().Length == 0)
{
this.textBox2.Text = this.textBox1.Text.Trim();
}
else
{
this.textBox2.Text = this.textBox2.Text + "\r\n" + this.textBox1.Text.Trim();
}
this.textBox1.Clear();
} private void Calculate_Button_Click(object sender, EventArgs e)
{
string str = this.textBox2.Text.Trim().ToString();
string myString = str.Replace("\n", " ").Replace("\r", string.Empty);
double[,] a = new double[this.textBox2.Lines.GetUpperBound(0) + 1, this.textBox2.Lines.GetUpperBound(0) + 2];
a = GetNum(myString, this.textBox2.Lines.GetUpperBound(0) + 1);
if (this.comboBox1.Text == "Guass列主消元法")
{
Gauss(a, this.textBox2.Lines.GetUpperBound(0) + 1);
for (int i = 0; i < this.textBox2.Lines.GetUpperBound(0) + 1; i++)
{
this.textBox3.Text = this.textBox3.Text + "\r\nX" + (i + 1) + "=" + a[i, this.textBox2.Lines.GetUpperBound(0) + 1];
}
}
else if (this.comboBox1.Text == "Doolittle三角分解法")
{
this.textBox3.Enabled = true;
Doolittle(a, this.textBox2.Lines.GetUpperBound(0) + 1);
this.label3.Text = "分解后的结果:";
this.textBox3.Clear();
this.textBox3.Text += "L矩阵:\r\n";
for (int i = 0; i < this.textBox2.Lines.GetUpperBound(0) + 1; i++)
{
for (int j = 0; j < this.textBox2.Lines.GetUpperBound(0) + 1; j++)
{
if (j < i)
{
this.textBox3.Text += a[i, j].ToString() + "\t";
}
else if (i == j)
{
this.textBox3.Text += "1\t";
}
else
{
this.textBox3.Text += "0\t";
}
}
this.textBox3.Text += "\r\n";
}
this.textBox3.Text += "\r\nU矩阵:\r\n";
for (int i = 0; i < this.textBox2.Lines.GetUpperBound(0) + 1; i++)
{
for (int j = 0; j < this.textBox2.Lines.GetUpperBound(0) + 1; j++)
{
if (j >= i)
{
this.textBox3.Text += a[i, j].ToString() + "\t";
}
else
{
this.textBox3.Text += "0\t";
}
}
this.textBox3.Text += "\r\n";
}
} } private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (this.textBox1.Text.Trim().ToString().Length == 0)
{
Calculate_Button_Click(sender, e);
}
else
{
Confirm_Button_Click(sender, e);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
this.textBox2.Enabled = true;
}
}
}

  运行截图:

  至此完毕。。。。

Guass列选主元消去法和三角分解法的更多相关文章

  1. 大规模问题的分解法-D-W分解法

    大规模线性规划问题的求解极具挑战性,在效率.存储和数值稳定性等方面对算法都有很高的要求.但是这类问题常常非常稀疏且有特殊结构,能够分解为若干个较小规模问题求解. 线性规划问题的目标函数和非负约束都可分 ...

  2. Matlab数值计算示例: 牛顿插值法、LU分解法、拉格朗日插值法、牛顿插值法

    本文源于一次课题作业,部分自己写的,部分借用了网上的demo 牛顿迭代法(1) x=1:0.01:2; y=x.^3-x.^2+sin(x)-1; plot(x,y,'linewidth',2);gr ...

  3. [Architecture] 系统架构正交分解法

    [Architecture] 系统架构正交分解法 前言 随着企业成长,支持企业业务的软件,也会越来越庞大与复杂.当系统复杂到一定程度,开发人员会发现很多系统架构的设计细节,很难有条理.有组织的用一张大 ...

  4. 时间序列分解-STL分解法

    时间序列分解-STL分解法 [转载时请注明来源]:http://www.cnblogs.com/runner-ljt/ Ljt 作为一个初学者,水平有限,欢迎交流指正. STL(’Seasonal a ...

  5. 项目管理——WBS工作分解法

    首先我们要了解什么是WBS工作分解法 工作分解结构(Work Breakdown Structure,简称WBS)跟因数分解是一个原理,就是把一个项目,按一定的原则分解,项目分解成任务,任务再分解成一 ...

  6. Miiler-Robin素数测试与Pollard-Rho大数分解法

    板题 Miiler-Robin素数测试 目前已知分解质因数以及检测质数确定性方法就只能\(sqrt{n}\)试除 但是我们可以基于大量测试的随机算法而有大把握说明一个数是质数 Miler-Robin素 ...

  7. [原创]浅谈对任务分解法WBS应用

    [原创]浅谈对任务分解法WBS应用 1.WBS是什么? 即Work Breakdown Structure如何进行WBS分解:目标→任务→工作→活动 2.WBS分解的原则:将主体目标逐步细化分解,最底 ...

  8. Pollard_Rho 整数分解法【学习笔记】

    引文:如果要对比较大的整数分解,显然之前所学的筛选法和是试除法都将不再适用.所以我们需要学习速度更快的Pollard_Rho算法. 算法原理: 生成两个整数a和b,计算p=gcd(a-b, n),知道 ...

  9. url映射 ccf (Java正则表达式80分解法)

    问题描述 试题编号: 201803-3 试题名称: URL映射 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 URL 映射是诸如 Django.Ruby on Rails 等 ...

随机推荐

  1. C# DateTime日期格式化

    在C#中DateTime是一个包含日期.时间的类型,此类型通过ToString()转换为字符串时,可根据传入给Tostring()的参数转换为多种字符串格式. 目录 1. 分类 2. 制式类型 3. ...

  2. 【原】AFNetworking源码阅读(三)

    [原]AFNetworking源码阅读(三) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇的话,主要是讲了如何通过构建一个request来生成一个data tas ...

  3. 如何远程关闭一个ASP.NET Core应用?

    在<历数依赖注入的N种玩法>演示系统自动注册服务的实例中,我们会发现输出的列表包含两个特殊的服务,它们的对应的服务接口分别是IApplicationLifetime和IHostingEnv ...

  4. 将DataTable中的某列转换成数组或者List

    string[] arrRate = dtRate.AsEnumerable().Select(d => d.Field<string>("arry")).ToA ...

  5. css样式之超出隐藏

    文本超出部分隐藏,总结两种方法. 1.单行隐藏 html代码 <div class="mi">当文字超过范围的时候,超出部分会隐藏起来.</div> css ...

  6. Android中的多线程断点下载

    首先来看一下多线程下载的原理.多线程下载就是将同一个网络上的原始文件根据线程个数分成均等份,然后每个单独的线程下载对应的一部分,然后再将下载好的文件按照原始文件的顺序"拼接"起来就 ...

  7. Oracle创建表空间

    1.创建表空间 导出Oracle数据的指令:/orcl file=C:\jds.dmp owner=jds 导入Oracle数据的指令:imp zcl:/orcl file=C:\jds.dmp fu ...

  8. MongoDB学习笔记~对集合属性的操作

    回到目录 $unset清除元素 请注意在单个数组元素上使用$unset的结果可能与你设想的不一样.其结果只是将元素的值设置为null,而非删除整个元素.要想彻底删除某个数组元素,可以用$pull 和$ ...

  9. mono-3.4.0 源码安装时出现的问题 [do-install] Error 2 [install-pcl-targets] Error 1 解决方法

    Mono 3.4修复了很多bug,继续加强稳定性和性能(其实Mono 3.2.8 已经很稳定,性能也很好了),但是从http://download.mono-project.com/sources/m ...

  10. .NET - 基于事件的异步模型

    注:这是大概四年前写的文章了.而且我离开.net领域也有四年多了.本来不想再发表,但是这实际上是Active Object模式在.net中的一种重要实现方法,因此我把它掏出来发布一下.如果该模型有新的 ...