我是靠谱客的博主 等待流沙,这篇文章主要介绍C#--解方程组之Seidel迭代法,现在分享给大家,希望可以做个参考。

简介
高斯-赛德尔迭代法是解 线性方程组的常用迭代法之一,设线性方程组为
高斯-赛德尔迭代法的迭代公式为
当然,此处假定
,在很多情况下,它比简单迭代法收敛快,它和简单迭代法的不同点在于计算
时,利用了刚刚迭代出的
的值,当系数矩阵 A 严格对角占优或对称正定时,高斯-赛德尔迭代法必收敛。


迭代公式的实现

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public void Calcu6() { int count1 = 0, count2 = 0; while (true) { for (int i = 0; i < n; i++) { double sum1 = 0,sum2=0; for (int j = i+1; j <n; j++) { sum1 += a[i, j] * x[j]; } for(int j=0;j<i-1;j++) { sum2 += a[i, j] * x2[j]; } x[i] = (a[i, n] - sum2-sum1) / a[i, i]; if (Math.Abs(x2[i] - x[i]) < e) count2++; } count1++; if (count1 > 10000) { Console.WriteLine("迭代发散!!!"); break; } if (count2 == n) { Console.WriteLine("迭代次数:{0}", count2); break; } } }

源程序:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Gauss_Seidel迭代 { class Seidel { int n; public int N { get { return n; } set { n = value; } } double[,] a; public double[,] A { get { return a; } set { a = value; } } double[] x; public double[] X { get { return x; } set { x = value; } } double e = 0.00001; public double E { get { return e; } set { e = value; } } private double[] x2; public double[] X2 { get { return x2; } set { x2 = value; } } public void Input() { Console.WriteLine("请输入阶数:"); n = Convert.ToInt32(Console.ReadLine()); a = new double[n, n + 1]; x = new double[n]; x2 = new double[N + 1]; for (int i = 0; i < N; i++) { x2[i]=x[i]; } Console.WriteLine("请输入各行系数(','或' '隔开):"); for (int i = 0; i < n; i++) { string s = Console.ReadLine(); string[] ss = s.Split(' ', ','); for (int j = 0; j < n + 1; j++) { a[i, j] = Convert.ToDouble(ss[j]); } } } public void Calcu6() { int count1 = 0, count2 = 0; while (true) { for (int i = 0; i < n; i++) { double sum1 = 0,sum2=0; for (int j = i+1; j <n; j++) { sum1 += a[i, j] * x[j]; } for(int j=0;j<i-1;j++) { sum2 += a[i, j] * x2[j]; } x[i] = (a[i, n] - sum2-sum1) / a[i, i]; if (Math.Abs(x2[i] - x[i]) < e) count2++; } count1++; if (count1 > 10000) { Console.WriteLine("迭代发散!!!"); break; } if (count2 == n) { Console.WriteLine("迭代次数:{0}", count2); break; } } } public void Output() { Console.WriteLine("方程系数为:"); for (int i = 0; i < n; i++) { string s = null; for (int j = 0; j < n + 1; j++) { s += string.Format("{0,8:f2}", a[i, j]); } Console.WriteLine(s); } } public void OutputX() { Console.WriteLine("n方程组的解是:"); for (int i = 0; i < n; i++) { Console.WriteLine("x{0}={1}", i + 1, x[i]); } } } class Program { static void Main(string[] args) { Seidel abc = new Seidel(); abc.Input(); abc.Output(); abc.Calcu6(); abc.OutputX(); } } }

运行结果:
谢谢!!!

最后

以上就是等待流沙最近收集整理的关于C#--解方程组之Seidel迭代法的全部内容,更多相关C#--解方程组之Seidel迭代法内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(72)

评论列表共有 0 条评论

立即
投稿
返回
顶部