77范文网 - 专业文章范例文档资料分享平台

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

来源:网络收集 时间:2019-05-18 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

/*第6题 高斯-赛德尔迭代法--源代码及关键源代码注解如下:*/ /******************************************* * Class Matrix * *******************************************/ #include #include #include #include #include #define MAX 10 class Csimple {

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

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库c++课程设计3-6 迭代法求线性方程在线全文阅读。

c++课程设计3-6 迭代法求线性方程.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/630753.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: