}
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--矩阵输入、输出流重载函数 * ***********************************************************/ 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库c++课程设计3-6 迭代法求线性方程(3)在线全文阅读。
相关推荐: