⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 diedaifa.htm

📁 该系统实现在线考试功能
💻 HTM
📖 第 1 页 / 共 2 页
字号:
lang=EN-US>高斯-赛德尔迭代</SPAN></SPAN></span></span></font></p>
          <p>#include &lt;iostream&gt; <BR>
            #include &lt;cmath&gt; <BR>
            using namespace std; <BR>
            int a,b,m; <BR>
            double *x0; <BR>
            void Jacobi(double **c,double *d,int n,double eps); <BR>
void Gauss(double **c,double *d,int n,double eps); <BR>
void main() <BR>
{ <BR>
int n; <BR>
double **A,*B; <BR>
double e; <BR>
cout&lt;&lt;"请选择求方程组的迭代方法!雅可比选0,高斯-赛德尔选1!"&lt;&lt;endl; <BR>
cin&gt;&gt;n; <BR>
cout&lt;&lt;"输入方程组的变量的个数以及方程的个数!"&lt;&lt;endl; <BR>
cin&gt;&gt;a&gt;&gt;b; <BR>
A=new double*[b]; <BR>
for(int i=0;i&lt;b;i++) <BR>
{ <BR>
A[i]=new double[a]; <BR>
} <BR>
B=new double[b]; <BR>
x0=new double[a]; <BR>
cout&lt;&lt;"输入每个方程组的变量的系数以及方程右端的值!"&lt;&lt;endl; <BR>
for(int k=0;k&lt;b;k++) <BR>
{ <BR>
for(int j=0;j&lt;a;j++) <BR>
{ <BR>
cin&gt;&gt;A[k][j]; <BR>
cout&lt;&lt;"A["&lt;&lt;k&lt;&lt;j&lt;&lt;"]="&lt;&lt;A[k][j]&lt;&lt;endl; <BR>
} <BR>
cin&gt;&gt;B[k]; <BR>
cout&lt;&lt;"B["&lt;&lt;k&lt;&lt;"]="&lt;&lt;B[k]&lt;&lt;endl; <BR>
} <BR>
cout&lt;&lt;"输入方程组迭代的次数及所要求的精度!"&lt;&lt;endl; <BR>
cin&gt;&gt;m&gt;&gt;e; <BR>
cout&lt;&lt;"输入方程组迭代的初值!"&lt;&lt;endl; <BR>
for(int j=0;j&lt;a;j++) <BR>
{ <BR>
cin&gt;&gt;x0[j]; <BR>
} <BR>
switch (n) <BR>
{ <BR>
case 0:Jacobi(A,B,m,e); <BR>
break; <BR>
case 1:Gauss(A,B,m,e); <BR>
break; <BR>
default:cout&lt;&lt;"你没有选择求解方程组的一种方法!!"&lt;&lt;endl; <BR>
break; <BR>
} <BR>
} <BR>
<BR>
void Jacobi(double **c,double *d,int n,double eps) <BR>
{ <BR>
int k,i; <BR>
double *y = new double[a],*x=new double[a],s,temp=0.0; <BR>
k=1; <BR>
while(1) <BR>
{ <BR>
temp = 0.0; <BR>
for(i=0;i&lt;a;i++) <BR>
{ <BR>
s=0.0; <BR>
for(int j=0;j&lt;a;j++) <BR>
{ <BR>
if(j!=i) <BR>
{ <BR>
s+=c[i][j]*x0[j]; <BR>
} <BR>
} <BR>
s=(d[i]-s)/c[i][i]; <BR>
y[i]=s; <BR>
if(fabs(x0[i]-s)&gt;temp) <BR>
{ <BR>
temp=fabs(x0[i]-s); <BR>
} <BR>
} <BR>
if(temp&lt;eps) <BR>
{ <BR>
cout&lt;&lt;"迭代成功!迭代结果为:"&lt;&lt;endl; <BR>
for(i=0;i&lt;a;i++) <BR>
{ <BR>
cout&lt;&lt;"y["&lt;&lt;i&lt;&lt;"] ="&lt;&lt;y[i]&lt;&lt;endl; <BR>
} <BR>
break; <BR>
} <BR>
if(k==m) <BR>
{ <BR>
cout&lt;&lt;"迭代失败!!"&lt;&lt;endl; <BR>
break; <BR>
} <BR>
k+=1; <BR>
for(i=0;i&lt;a;i++) <BR>
{ <BR>
x0[i]=y[i]; <BR>
} <BR>
} <BR>
} <BR>
void Gauss(double **c,double *d,int n,double eps) <BR>
{ <BR>
int k,i; <BR>
double *y=new double[a],*x=new double[a],s,temp=0.0; <BR>
for(i=0;i&lt;a;i++) <BR>
{ <BR>
x[i]=x0[i]; <BR>
y[i]=x[i]; <BR>
} <BR>
k=1; <BR>
while(1) <BR>
{ <BR>
temp=0.0; <BR>
for(i=0;i&lt;a;i++) <BR>
{ <BR>
s=0.0; <BR>
for(int j=0;j&lt;a;j++) <BR>
{ <BR>
if(j!=i) <BR>
{ <BR>
s+=c[i][j]*y[j]; <BR>
} <BR>
} <BR>
s=(d[i]-s)/c[i][i]; <BR>
y[i]=s; <BR>
if(fabs(x[i]-s)&gt;temp) <BR>
{ <BR>
temp=fabs(x[i]-s); <BR>
} <BR>
} <BR>
if(temp&lt;eps) <BR>
{ <BR>
cout&lt;&lt;"迭代成功!迭代结果为:"&lt;&lt;endl; <BR>
for(i=0;i&lt;a;i++) <BR>
{ <BR>
cout&lt;&lt;"y["&lt;&lt;i&lt;&lt;"] ="&lt;&lt;y[i]&lt;&lt;endl; <BR>
} <BR>
break; <BR>
} <BR>
if(k==m) <BR>
{ <BR>
cout&lt;&lt;"迭代失败!!"&lt;&lt;endl; <BR>
break; <BR>
} <BR>
k+=1; <BR>
for(i=0;i&lt;a;i++) <BR>
{ <BR>
x[i]=y[i]; <BR>
} <BR>
} <BR>
}<BR>
          </p>
        </DIV>
      </DIV>
      ?
      <BR>      <P class=MsoNormal 
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt; LINE-HEIGHT: 20pt; mso-char-indent-count: 2.0; mso-char-indent-size: 12.0pt; mso-line-height-rule: exactly">&nbsp;</P>
      <SPAN class=addtime>
      <P class=MsoNormal 
style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 20pt; mso-line-height-rule: exactly">&nbsp;</P>
    </SPAN></td>
    <td width="1">&nbsp;</td>
    <td width="11" rowspan="2" valign="top" background="images/右边界.jpg"><!--DWLayoutEmptyCell-->&nbsp;</td>
  </tr>
  <tr>
    <td height="3268">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -