数值分析 迭代法 C++程序
- 格式:docx
- 大小:116.03 KB
- 文档页数:6
课题三 解线性方程组的迭代法
实验目标:
分别采用Jacobi迭代法,Gauss-Seidel迭代法和SOR迭代法求解线性方程组。
Jocabi迭代法:
#include
#include
using namespace std;
int i,j,k; //计数器
int M = 2000;
int Array(double ***Arr, int n){
double **p;
int i;
p=(double **)malloc(n*sizeof(double *));
if(!p)return 0;
for(i=0;i
{
p[i]=(double *)malloc(n*sizeof(double));
if(!p[i])return 0;
}
*Arr=p;
return 1;
}
void main()
{
double eps ;
cout<<"默认最多迭代次数为2000次"<
cin>>eps;
double **matrix;
int n;
cout<<"矩阵大小为:";
cin>>n;
double *X;
X= new double[n];
double *Y;
Y= new double[n];
double *G;
G= new double[n];
for(i=0;i
}
if(!Array(&matrix,n))
cout<<"内存分配失败!";
else
cout<<"请输入矩阵:"<
for( i=0;i
for( j=0;j
cin>>matrix[i][j];
}
}
cout<<"请输入右端项:"<
double *B;
B = new double[n];
for(i=0;i
cin>>B[i];
}
for (i = 0 ;i< n;i++)
{
if (fabs(matrix[i][i]) < eps)
{
cout <<"打印失败"<
return;
}
double T = matrix[i][i];
for ( j = 0 ; j< n;j++)
{
matrix[i][j] = -matrix[i][j]/T;
}
matrix[i][i] = 0;
G[i] = B[i]/T;
}
int counter = 0;
while (counter < M) {
for (i = 0;i < n; i++)
{
double temp = 0;
for (j = 0;j
{
temp = temp + matrix[i][j]*Y[j];
}
X[i] = G[i] + temp;
}
double temp = 0;
for (i = 0 ;i< n ; i++)
{
temp = temp + fabs(X[i] - Y[i]);
}
if (temp <= eps) break;
else
{
for( i = 0; i < n ;i++)
{
Y[i] = X[i];
}
}
counter++;
}
cout << "迭代次数为:"<
for( i = 0; i < n ;i++)
{
cout << X[i] <<" ";
}
}
Gauss-Seidel迭代法:
#include
#include
using namespace std;
int i,j,k; //计数器
int M = 2000;
int Array(double ***Arr, int n){
double **p;
int i; p=(double **)malloc(n*sizeof(double *));
if(!p)return 0;
for(i=0;i
{
p[i]=(double *)malloc(n*sizeof(double));
if(!p[i])return 0;
}
*Arr=p; return 1;
}
void main()
{
double eps ;
cout<<"默认最多迭代次数为2000次"<
cin>>eps;
double **matrix;
int n;
cout<<"矩阵大小为:";
cin>>n;
double *X;
X= new double[n];
double *Y;
Y= new double[n];
double *G;
G= new double[n];
for(i=0;i
Y[i]=0;
}
if(!Array(&matrix,n))
cout<<"内存分配失败!";
else
cout<<"请输入矩阵:"<
for( i=0;i
for( j=0;j
cin>>matrix[i][j];
}
}
cout<<"请输入右端项:"<
double *B;
B = new double[n];
for(i=0;i
cin>>B[i];
}
for (i = 0 ;i< n;i++)
{
if (fabs(matrix[i][i]) < eps)
{
cout <<"打印失败"<
return; }
double T = matrix[i][i];
for ( j = 0 ; j< n;j++)
{
matrix[i][j] = -matrix[i][j]/T;
}
matrix[i][i] = 0;
G[i] = B[i]/T;
}
int counter = 0;
while (counter < M)
{
//迭̨¹代䨲
for (i = 0;i < n; i++)
{
double temp = 0;
for (j = 0;j
{
temp = temp + matrix[i][j]*X[j];
}
X[i] = G[i] + temp;
}
double temp = 0;
for (i = 0 ;i< n ; i++)
{
temp = temp + fabs(X[i] - Y[i]);
}
if (temp <= eps)
break;
else
{
//交?换?X,ê?Y向¨°量¢?;ê?
for( i = 0; i < n ;i++)
{
Y[i] = X[i];
}
}
counter++;
}
cout << "迭代次数为:"<
for( i = 0; i < n ;i++)
{ cout << X[i] <<" ";
} }
SOR迭代法:
#include
#include
using namespace std;
int i,j,k; //计数器
int M = 2000;
int Array(double ***Arr, int n){
double **p;
int i;
p=(double **)malloc(n*sizeof(double *));
if(!p)return 0;
for(i=0;i
{
p[i]=(double *)malloc(n*sizeof(double));
if(!p[i])return 0;
}
*Arr=p;
return 1;
}
void main()
{
double eps ;
cout<<"默认最多迭代次数为2000次"<
cin>>eps;
double **matrix; int n;
cout<<"矩阵大小为:";
cin>>n;
double *X;
X= new double[n];
double *Y;
Y= new double[n];
double *G;
G= new double[n];
for(i=0;i
Y[i]=0;
}
if(!Array(&matrix,n))
cout<<"内存分配失败!";
else
cout<<"请输入矩阵:"<
for( i=0;i
for( j=0;j
cin>>matrix[i][j];
}
}
cout<<"请输入右端项:"<
double *B;