最近数值计算学了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. .NET平台开源项目速览(16)C#写PDF文件类库PDF File Writer介绍

    1年前,我在文章:这些.NET开源项目你知道吗?.NET平台开源文档与报表处理组件集合(三)中(第9个项目),给大家推荐了一个开源免费的PDF读写组件 PDFSharp,PDFSharp我2年前就看过 ...

  2. InstallShield 脚本语言学习笔记

    InstallShield脚本语言是类似C语言,利用InstallShield的向导或模板都可以生成基本的脚本程序框架,可以在此基础上按自己的意愿进行修改和添加.     一.基本语法规则      ...

  3. Coroutine in Java - Quasar Fiber实现--转载

    转自 https://segmentfault.com/a/1190000006079389?from=groupmessage&isappinstalled=0 简介 说到协程(Corout ...

  4. CSS 3学习——文本效果和@font-face

    文本效果 关于文本效果,这里仅仅记录得到大多数浏览器支持的几个属性,分别是: text-overflow text-shadow word-break word-wrap text-overflow ...

  5. MVC还是MVVM?或许VMVC更适合WinForm客户端

    最近开始重构一个稍嫌古老的C/S项目,原先采用的技术栈是『WinForm』+『WCF』+『EF』.相对于现在铺天盖地的B/S架构来说,看上去似乎和Win95一样古老,很多新入行的,可能就没有见过经典的 ...

  6. BPM配置故事之案例1-配置简单流程

    某天,Boss找到了信息部工程师小明. Boss:咱们新上了H3 BPM,你研究研究把现在的采购申请流程加上去吧,这是采购申请单. 小明:好嘞 采购申请单 小明回去后拿着表单想了想,开始着手配置. 他 ...

  7. Crystal Clear Applied: The Seven Properties of Running an Agile Project (转载)

    作者Alistair Cockburn, Crystal Clear的7个成功要素,写得挺好. 敏捷方法的关注点,大家可以参考,太激动所以转载了. 原文:http://www.informit.com ...

  8. 项目持续集成环境(jenkins + SVN + maven + tomcat)

    整体流程 每次SVN上代码有变动,触发自动构建动作,并部署到服务器的tomcat上,具体流程: 1.SVN上提交代码修改 2.maven执行Goals 3.将web工程打成war包 4.关闭服务器的t ...

  9. MySQL常见面试题

    1. 主键 超键 候选键 外键 主 键: 数据库表中对储存数据对象予以唯一和完整标识的数据列或属性的组合.一个数据列只能有一个主键,且主键的取值不能缺失,即不能为空值(Null). 超 键: 在关系中 ...

  10. 《高性能javascript》一书要点和延伸(上)

    前些天收到了HTML5中国送来的<高性能javascript>一书,便打算将其做为假期消遣,顺便也写篇文章记录下书中一些要点. 个人觉得本书很值得中低级别的前端朋友阅读,会有很多意想不到的 ...