本文主要是介绍改进的平方根法_解线性方程组的直接解法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
改进的平方根法_解线性方程组的直接解法
标签:计算方法实验
#include <stdio.h>
#include <math.h>const int maxn = 15;int main(){double a[maxn][maxn], b[maxn], y[maxn], x[maxn], l[maxn][maxn], d[maxn];int n, sum;freopen("sqrt.txt", "r", stdin);scanf("%d", &n);for(int i = 1; i <= n; i++){for(int j = 1; j <= n; j++) scanf("%lf", &a[i][j]);scanf("%lf", &b[i]);}/*for(int i = 1; i <= n; i++){for(int j = 1; j <= n; j++) printf("%10f", a[i][j]);printf("%10f\n", b[i]);}*/for(int i = 1; i <= n; i++){ //分解: A = LDL^Tsum = 0;for(int j = 1; j <= i - 1; j++){for(int k = 1; k <= j - 1; k++) sum += (d[k] * l[i][k] * l[j][k]);l[i][j] = (a[i][j] - sum) / d[j];}sum = 0;for(int k = 1; k <= i - 1; k++) sum += (d[k] * l[i][k] * l[i][k]);d[i] = a[i][i] - sum;}for(int i = 1; i <= n; i++){ //求y: L(DL^Tx) = b即Ly = bsum = 0;for(int k = 1; k <= i - 1; k++) sum += (l[i][k] * y[k]);y[i] = b[i] - sum;}for(int i = n; i >= 1; i--){ //求x: L^Tx = D^-1bsum = 0;for(int k = i + 1; k <= n; k++) sum += (l[k][i] * x[k]);x[i] = y[i] / d[i] - sum;}for(int i = 1; i <= n; i++) printf("x%d = %10f\n", i, x[i]);return 0;
}
数据文件
实验结果
这篇关于改进的平方根法_解线性方程组的直接解法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!