c++课程设计3-6 迭代法求线性方程
更新时间:2024-05-09 16:56:01 阅读量: 综合文库 文档下载
- 软件工程课程设计推荐度:
- 相关推荐
/*第6题 高斯-赛德尔迭代法--源代码及关键源代码注解如下:*/ /******************************************* * Class Matrix * *******************************************/ #include
friend void operator<<(ostream &,Csimple &); friend void operator>>(istream &,Csimple &); protected: int flag;
int row,column;
//'mat' is my actual matrix double **mat;
//size of 'variable' always =(column-1) int varnum;
//contains the solution set double *variable;
//counts number of iterations int itercount;
//performs a single iteration on all the equations //the argument is the relaxation coefficient double realx;
void iteration(double,double *); //checks the allowable error
bool epsilon(double*,double*,int,double); public:
Csimple(int nRow,int nColumn,double rex,double Mat[MAX][MAX+1]); Csimple();
Csimple(int,int);
//small funtion for asking the user for the number of rows and columns void ReadFromFile(); void WriteToFile();
static void initialize(int&,int&); //prepares function for reduction void rearrange();
//solves by gauss-seigel method,
//perfomes multiple iterations until a solution is found
//the argument is the relaxation-coefficient, and this is \
void solve(double); //prints the solution void show_answer(); };
//END OF CLASS MATRIX /************************** * constructor--构造函数 * **************************/ Csimple::Csimple(int nRow,int nColumn,double Mat[MAX][MAX+1]):row(nRow),column(nColumn),realx(rex) { mat=new double*[row]; for(int i=0;i Csimple::Csimple(){ mat[MAX][MAX+1]; flag=0; } Csimple::Csimple(int r, int c):row(r),column(c) { //creates my matrix dynamically, but there are still no values in the positions mat=new double*[row]; for(int i=0;i //end of constructor /********************************* * 矩阵行列设置 * ROWS:线性方程数 * COLUMNS:系数及常数的个数 *********************************/ /********************************* * static initialization * *********************************/ void Csimple::initialize(int&i,int&j) { cout<<\ rex,double cin>>i; cout<<\cin>>j; } //clrscr(); /********************************* * 简单矩阵变换函数 * * rearrange function * *********************************/ //rearranges the system so that it can be solved by gauss-seigel method //must always rearange before solving void Csimple::ReadFromFile() { char str[10][100]; int i=0; ifstream infile(\ while(!infile.eof()) {infile.getline(str[i],100); i++; } row=i; column=i+1; mat=new double*[row]; for(i=0;i mat[i][j]=-mat[i][j]; } else { for(int q=0;q } while(str[i][p]!='=') { if(str[i][p]=='x') { j=(int)(str[i][p+1]-'0')-1; int k=0; for(k=0;str[i][p-k]!='+'&&str[i][p-k]!='-';k++) {} if(k==1) { if(str[i][p-k]=='+') mat[i][j]=1; else mat[i][j]=-1; } else{ if(str[i][p-k]=='+') for(;k>1;k--) mat[i][j]=mat[i][j]*10+(double)(str[i][p-k+1]-'0'); else{ if(str[i][p-k]=='-') { for(;k>1;k--) mat[i][j]=mat[i][j]*10+(double)(str[i][p-k+1]-'0'); mat[i][j]=-mat[i][j]; } } } } p++; } if(str[i][p+1]=='-') { for(int m=2;str[i][p+m];m++) mat[i][row]=10*mat[i][row]+(double)(str[i][p+m]-'0'); mat[i][row]=-mat[i][row]; } else { for(int m=1;str[i][p+m];m++) mat[i][row]=10*mat[i][row]+(double)(str[i][p+m]-'0'); } } infile.close(); } void Csimple::WriteToFile() { ofstream outfile(\解.txt\ outfile<<\用简单迭带法求方程解依次为:\ for(int i=0;i void Csimple::rearrange() { varnum=column-1; //'variable' will contain the solution set //they will get initialized with the first guess in the 'solve' function variable=new double[varnum]; /*divides all terms by the desired variables (the variable which is being solved for) coefficient*/ for(int i=0;i double coefficient=mat[i][i]; for(int j=0;j mat[i][j]/=coefficient; } } //all variables except the diagonal are being brought to the other side for( i=0;i { for(int j=0;j mat[i][j]*=-1; } mat[i][i]=0; } } //END OF 'REARRANGE' FUNCTION /********************************** * iteration funtion--迭代函数 * **********************************/ //performes a single iteration on the system, using passed assumed values void Csimple::iteration(double lambda,double *t) { for(int i=0;i variable[i]=0; for(int j=0;j variable[i]+=mat[i][j]*t[j]; } variable[i]+=mat[i][column-1]; //new value after relaxation variable[i]=t[i]+lambda*(variable[i]-t[i]); } } /******************************** * solve function--求解函数 * ********************************/ void Csimple::solve(double lambda) { //DECLARATIONS AND INITIALIZATIONS for (int i=0;i itercount=0; //this is the allowable error this value can be changed to suit the problem double criterion=0.0001; double *newest=new double[varnum]; double *last=new double[varnum]; for( i=0;i { newest[i]=variable[i]; } //END OF DECLARATIONS AND INITIALIZATIONS //MAIN BODY OF THE FUNCTION STARTS HERE- //while('condition not met'){perform another iteration} do { for(int i=0;i last[i]=newest[i]; } //this is the most important part, //everything else in this loop is only to check that the criterion is met if(flag==1){iteration(realx,last);} else iteration(lambda,last); for( i=0;i /******************************************* * Epsilon Criterion-Epsilon 精度要求 * *******************************************/ bool Csimple::epsilon(double *newest,double *last,int size,double criterion) { for(int i=0;i //if(it has not met the criterion) if((fabs(newest[i]-last[i])/newest[i])>criterion) { //then (return 1)=(condition not met) and the loop is repeated return 1; } } //criterion has been met return 0; } /************************************************ * show answer function-- 输出求解结果函数 * *************************************************/ //'solve' function must be executed before 'show_answer' function void Csimple::show_answer() { cout<<\ for(int i=0;i cout< /*********************************************************** * stream operators--矩阵输入、输出流重载函数 * ***********************************************************/ void operator<<(ostream& out,Csimple& m) { for (int i=0;i out< void operator>>(istream& in,Csimple& m) { for(int i=0;i for(int j=0;j cout<<\ in>>m.mat[i][j]; } } //clrscr(); } class matrix //高斯-赛德尔矩阵算法类 { friend void operator<<(ostream &,matrix &); friend void operator>>(istream &,matrix &); protected: int flag; int row,column; //'mat' is my actual matrix double **mat; //size of 'variable' always =(column-1) int varnum; //contains the solution set double *variable; //counts number of iterations int itercount; //performs a single iteration on all the equations //the argument is the relaxation coefficient double realx; void iteration(double); //checks the allowable error bool epsilon(double*,double*,int,double); public: matrix(int nRow,int nColumn,double rex,double Mat[MAX][MAX+1]); matrix(); matrix(int,int); //small funtion for asking the user for the number of rows and columns void ReadFromFile(); void WriteToFile(); static void initialize(int&,int&); //prepares function for reduction void rearrange(); //solves by gauss-seigel method, //perfomes multiple iterations until a solution is found //the argument is the relaxation-coefficient, and this is \void solve(double); //prints the solution void show_answer(); }; //END OF CLASS MATRIX /************************** * constructor--构造函数 * **************************/ matrix::matrix(int nRow,int nColumn,double Mat[MAX][MAX+1]):row(nRow),column(nColumn),realx(rex) { mat=new double*[row]; for(int i=0;i rex,double matrix::matrix(){ mat[MAX][MAX+1]; flag=0; } matrix::matrix(int r, int c):row(r),column(c) { //creates my matrix dynamically, but there are still no values in the positions mat=new double*[row]; for(int i=0;i //end of constructor /********************************* * 矩阵行列设置 * ROWS:线性方程数 * COLUMNS:系数及常数的个数 *********************************/ /********************************* * static initialization * *********************************/ void matrix::initialize(int&i,int&j) { cout<<\cin>>i; cout<<\cin>>j; } void matrix::ReadFromFile() { char str[10][100]; int i=0; ifstream infile(\ while(!infile.eof()) {infile.getline(str[i],100); i++; } row=i; column=i+1; mat=new double*[row]; for(i=0;i } for(i=0;i } while(str[i][p]!='=') { if(str[i][p]=='x') { j=(int)(str[i][p+1]-'0')-1; int k=0; for(k=0;str[i][p-k]!='+'&&str[i][p-k]!='-';k++){} if(k==1) { if(str[i][p-k]=='+') mat[i][j]=1; else mat[i][j]=-1; } else{ if(str[i][p-k]=='+') for(;k>1;k--) mat[i][j]=mat[i][j]*10+(double)(str[i][p-k+1]-'0'); else{ if(str[i][p-k]=='-') { for(;k>1;k--) mat[i][j]=mat[i][j]*10+(double)(str[i][p-k+1]-'0'); mat[i][j]=-mat[i][j]; } } } } p++; } if(str[i][p+1]=='-') { for(int m=2;str[i][p+m];m++) mat[i][row]=10*mat[i][row]+(double)(str[i][p+m]-'0'); mat[i][row]=-mat[i][row]; } else { for(int m=1;str[i][p+m];m++) mat[i][row]=10*mat[i][row]+(double)(str[i][p+m]-'0'); } } infile.close(); } void matrix::WriteToFile() { ofstream outfile(\解.txt\ outfile<<\用高斯-赛德尔迭带法求方程解为:\ for(int i=0;i //clrscr(); /********************************* * 高斯-赛德尔矩阵变换函数 * * rearrange function * *********************************/ //rearranges the system so that it can be solved by gauss-seigel method //must always rearange before solving void matrix::rearrange() { varnum=column-1; //'variable' will contain the solution set //they will get initialized with the first guess in the 'solve' function variable=new double[varnum]; /*divides all terms by the desired variables (the variable which is being solved for) coefficient*/ for(int i=0;i double coefficient=mat[i][i]; for(int j=0;j mat[i][j]/=coefficient; } } //all variables except the diagonal are being brought to the other side for( i=0;i for(int j=0;j mat[i][j]*=-1; } mat[i][i]=0; } } //END OF 'REARRANGE' FUNCTION /********************************** * iteration funtion--迭代函数 * **********************************/ //performes a single iteration on the system, using passed assumed values void matrix::iteration(double lambda) { //'last' is for the relaxation equation double last; for(int i=0;i { last=variable[i]; variable[i]=0; for(int j=0;j variable[i]+=mat[i][j]*variable[j]; } variable[i]+=mat[i][column-1]; //new value after relaxation variable[i]=last+lambda*(variable[i]-last); } } /******************************** * solve function--求解函数 * ********************************/ void matrix::solve(double lambda) { //DECLARATIONS AND INITIALIZATIONS //initializes first guess for (int i=0;i itercount=0; //this is the allowable error this value can be changed to suit the problem double criterion=0.0001; double *newest=new double[varnum]; double *last=new double[varnum]; for( i=0;i newest[i]=variable[i]; } //END OF DECLARATIONS AND INITIALIZATIONS //MAIN BODY OF THE FUNCTION STARTS HERE- //while('condition not met'){perform another iteration} do { for(int i=0;i last[i]=newest[i]; } //this is the most important part, //everything else in this loop is only to check that the criterion is met if(flag==1){iteration(realx);} else iteration(lambda); for( i=0;i /******************************************* * Epsilon Criterion-Epsilon 精度要求 * *******************************************/ bool matrix::epsilon(double *newest,double *last,int size,double criterion) { for(int i=0;i //if(it has not met the criterion) if((fabs(newest[i]-last[i])/newest[i])>criterion) { //then (return 1)=(condition not met) and the loop is repeated return 1; } } //criterion has been met return 0; } /************************************************ * show answer function-- 输出求解结果函数 * *************************************************/ //'solve' function must be executed before 'show_answer' function void matrix::show_answer() { cout<<\ for(int i=0;i cout< /*********************************************************** * stream operators--矩阵输入、输出流重载函数 * ***********************************************************/ void operator<<(ostream& out,matrix& m) { for (int i=0;i out< void operator>>(istream& in,matrix& m) { for(int i=0;i for(int j=0;j cout<<\ in>>m.mat[i][j]; } } //clrscr(); } //end of stream operators /************************ * MAIN * ************************/ void main() { cout<<\ cout<<\学号:******\ cout<<\ char Y; cout<<\you want to input from keyboard,press 1,if you want to input from file,press 2\ cin>>Y; if(Y=='2'){ matrix one; Csimple two; one.ReadFromFile(); two.ReadFromFile(); char X; cout<<\ cin>>X; if(X=='y'){cout< for(char x='y';x=='y' && x!='n';) { cout< one.solve(relax_coef); two.solve(relax_coef); one.show_answer(); two.show_answer(); one.WriteToFile(); two.WriteToFile(); cout<<\已经存放到解文件中\ cout< cout<<\} else if(Y=='1') { int i,j; matrix::initialize(i,j); Csimple::initialize(i,j); matrix one(i,j); Csimple two(i,j); cin>>one; cin>>two; char X; cout<<\ cin>>X; if(X=='y'){cout< cin.ignore(128,'\\n'); cin.ignore(128,'\\n'); } 方程为x1-x2=1 -x1+2x2+2x3=1 x1+x2-x3=0保存在a.txt中 结果在解.Txt显示
正在阅读:
c++课程设计3-6 迭代法求线性方程05-09
新目标英语八年级上同步阅读及答案(全册)05-09
1.4《从三个方向看物体的形状》12-04
化工综合实验讲义(10.10)02-26
2018版03706思想道德修养与法律基础精简笔记08-27
英语阅读:枯叶蝴蝶03-14
2011秋高二期中生物试题05-24
国际结算SWIFT报文模板04-12
关于2007年南宁市教育学会中学政治专业委员会教师论文09-15
2010高三数学月考试卷(文科)06-03
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 迭代法
- c++
- 线性方程
- 课程
- 设计
- 医学心理学单选多选题集
- 安标受控件采购管理制度
- 2005届高三第三次联考物理试卷
- 司法考试改革最新消息:非科班出身不能参加考试每日一练(2014.7.
- 尔雅通识课社会心理学答案-完整课后答案 + 观看视频过程中弹出的
- 计算机一级MSoffice选择题及答案解析汇总(二)
- 施工方案四公司
- Bivqdob软件公司商业计划书
- 2018年人教版小学五年级下册语文第六单元试题及答案
- 我的评价故事征文
- 焦化操作规程
- 职业健康安全和环境管理方案
- 毕业设计论文--机器人
- 高效液相色谱在药学中的应用
- DOA文献综述
- 气象灾害 - 洪涝学案
- 党务党建知识题库及答案(2016年11月更新)
- 福建省咨询工程师的报考流程每日一讲(5月7日)
- 2015年全国大学生英语竞赛C类真题、答案及评分标准
- 无线电对讲机设计论文