#include <stdio.h>
#include <math.h>
#define max_error 0.000125
double data[3][4] = {
{8,-3,2,20},
{4,11,-1,33},
{6,3,12,36}
};
double solu[3] = {3,2,1};
double x[3] = {0},x1[3];
int main() {
//雅可比迭代
int times = 0;
do {
for (char i = 0; i < 3; i++)
x1[i] = (data[i][3] - (data[i][0] * x[0] + data[i][1] * x[1] + data[i][2] * x[2]) + data[i][i] * x[i]) / data[i][i];
for (char j = 0; j < 3; j++)x[j] = x1[j];
times++;
printf("X%d=[%lf,%lf,%lf]\n", times, x[0], x[1], x[2]);
} while (fabs(solu[0] - x[0]) > max_error || fabs(solu[1] - x[1]) > max_error || fabs(solu[1] - x[1]) > max_error);
printf("雅可比迭代最终值为x1=%lf,x2=%lf,x3=%lf\n", x[0], x[1], x[2]);
//高斯-赛德尔迭代
times = 0;
x[0] = 0, x[1] = 0, x[2] = 0;
do{
for (char i = 0; i < 3; i++) {
x1[i] = (data[i][3]-(data[i][0]*x[0]+data[i][1]*x[1]+data[i][2]*x[2])+data[i][i]*x[i]) / data[i][i];
x[i] = x1[i];
}
times++;
printf("X%d=[%lf,%lf,%lf]\n", times, x[0], x[1], x[2]);
} while (fabs(solu[0]-x[0])>max_error|| fabs(solu[1] - x[1]) > max_error|| fabs(solu[1] - x[1]) > max_error);
printf("高斯-赛德尔迭代最终值为x1=%lf,x2=%lf,x3=%lf", x[0],x[1],x[2]);
return 0;
}
雅可比迭代,高斯-赛德尔迭代
最新推荐文章于 2023-10-20 17:02:50 发布