求解方程
?
#include<math.h>
#include<stdio.h>
#include <stdlib.h>
int main()
{
float e = 0.001, z, m, y[3];
float b[3] = {-12, 20, 3};
float x[3];
for(int i = 0;i < 3;i++){
x[i]=0;
}
float a[3][3] = {
{5, 2, 1},
{-1, 4, 2},
{2, -3, 10}
};
int n = 3, j, i, k = 1;
while(1)
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
m=m+a[i][j]*x[j];
m = m - x[i] * a[i][i];
y[i] = (b[i] - m) / a[i][i];
m = 0;
}
i = 0;
while(i < 3)
{
z = fabs(x[i] - y[i]);
if(z > e)
break;
i++;
}
if(i != 3)
{
for(i = 0; i < 3; i++)
x[i] = y[i];
k++;
}
else if(i == 3)
break;
}
printf("%f\n%f\n%f\n", y[0], y[1], y[2]);
return 0;
}
?Run(?精度保证小数点后两位):
下一步:提高精度
|