数值分析 迭代法 C 程序

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
for(i=0;i<n;i++){ Y[i]=0;
} if(!Array(&matrix,n))
cout<<"内存分配失败!"; else cout<<"请输入矩阵:"<<endl;
for( i=0;i<n;i++){ for( j=0;j<n;j++){ cin>>matrix[i][j]; }
}
代精度为:";
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<n;i++){ Y[i]=0; } if(!Array(&matrix,n)) cout<<"内存分配失败!"; else cout<<"请输入矩阵:"<<endl; for( i=0;i<n;i++){
p[i]=(double *)malloc(n*sizeof(double)); if(!p[i])return 0; } *Arr=p;
return 1; } void main() {
double eps ; cout<<"默认最多迭代次数为2000次"<<endl<<"迭 代精度为:"; 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];
cin>>n;
{
Байду номын сангаас
double *X;
matrix[i][j] = -matrix[i][j]/T;
X= new double[n];
double *Y;
}
Y= new double[n];
matrix[i][i] = 0;
double *G;
G[i] = B[i]/T;
G= new double[n];
int i,j,k;
//计数器
cout<<"内存分配失败!";
int M = 2000;
else
int Array(double ***Arr, int n){
cout<<"请输入矩阵:"<<endl;
double **p;
for( i=0;i<n;i++){
int i;
for( j=0;j<n;j++){
for( j=0;j<n;j++){ cin>>matrix[i][j]; } }
cout<<"请输入右端项:"<<endl; double *B;
B = new double[n]; for(i=0;i<n;i++){
cin>>B[i]; }
for (i = 0 ;i< n;i++) {
if (fabs(matrix[i][i]) < eps) {
Y[i] = X[i]; } } counter++; } cout << "迭代次数为:"<<counter<<"次。该线性 方程组的解(X1,X2,X3......Xn)为:"<<endl; for( i = 0; i < n ;i++) { cout << X[i] <<" "; } }
cout<<"请输入右端项:"<<endl; double *B; B = new double[n]; for(i=0;i<n;i++){ cin>>B[i]; }
for (i = 0 ;i< n;i++) { if (fabs(matrix[i][i]) < eps) { cout <<"打印失败"<<endl; return;
}
int counter = 0;
for(i=0;i<n;i++){
while (counter < M)
{ for (i = 0;i < n; i++) { double temp = 0; for (j = 0;j<n; j++) { temp = temp + matrix[i][j]*Y[j]; } X[i] = G[i] + temp; }
p=(double **)malloc(n*sizeof(double *));
cin>>matrix[i][j];
if(!p)return 0;
}
for(i=0;i<n;i++)
}
{
p[i]=(double *)malloc(n*sizeof(double)); cout<<"请输入右端项:"<<endl;
课题三 解线性方程组的迭代法
实验目标:
分别采用 Jacobi 迭代法,Gauss-Seidel 迭代法和 SOR 迭代法求解线性方程组。
Jocabi 迭代法:
#include<iostream>
Y[i]=0;
#include<math.h>
}
using namespace std;
if(!Array(&matrix,n))
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]); } //cout<<temp<<" "; if (temp <= eps) break; else { for( i = 0; i < n ;i++) {
cout <<"打印失败"<<endl; return; } double T = matrix[i][i]; for ( j = 0 ; j< n;j++) { matrix[i][j] = -w * matrix[i][j]/T; } matrix[i][i] = 1-w; G[i] = w * B[i]/T; } int counter = 0; while (counter < M) { for (i = 0;i < n; i++) { double temp = 0; for (j = 0;j<n; j++) {
Y[i] = X[i]; } } counter++; } cout << "迭代次数为:"<<counter<<"次。该线性 方程组的解(X1,X2,X3......Xn)为:"<<endl; for( i = 0; i < n ;i++) {
cout << X[i] <<" ";
}
}
SOR 迭代法:
#include<iostream>
#include<math.h>
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 *));
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 << "迭代次数为:"<<counter<<"次。该线性 方程组的解(X1,X2,X3......Xn)为:"<<endl; for( i = 0; i < n ;i++) { cout << X[i] <<" "; }
if(!p)return 0;
for(i=0;i<n;i++)
{
p[i]=(double *)malloc(n*sizeof(double));
if(!p[i])return 0;
}
*Arr=p;
return 1;
}
void main()
{
double eps ;
cout<<"默认最多迭代次数为2000次"<<endl<<"迭
if(!p[i])return 0;
double *B;
}
B = new double[n];
*Arr=p;
for(i=0;i<n;i++){
return 1;
cin>>B[i];
}
}
void main()
for (i = 0 ;i< n;i++)
{
{
double eps ;
if (fabs(matrix[i][i]) < eps)
}
Gauss-Seidel 迭代法:
#include<iostream>
#include<math.h>
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<n;i++) {
cout<<"默认最多迭代次数为2000次"<<endl<<"迭
{
代精度为:";
cout <<"打印失败"<<endl;
cin>>eps;
return;
double **matrix;
}
int n;
double T = matrix[i][i];
cout<<"矩阵大小为:";
for ( j = 0 ; j< n;j++)
double temp = 0; for (j = 0;j<n; 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++) {
} 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++) {
相关文档
最新文档