c++课程设计3-6 迭代法求线性方程(3)

2019-05-18 15:10

}

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--矩阵输入、输出流重载函数 * ***********************************************************/


c++课程设计3-6 迭代法求线性方程(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:煤矿安全管理制度(28条制度)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: