简单迭代法的C++实现

pilow 2008-06-03 11:54:20
就是简单迭代法的算法,用C++实现一下。哪个有代码。。给个参考,谢谢了
...全文
1648 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
执假以为真 2008-06-04
  • 打赏
  • 举报
回复
没时间看,先mark
pilow 2008-06-04
  • 打赏
  • 举报
回复
这是论坛其他人发的高斯消去法

#include <iostream>
using namespace std;

void Gauss(double A[],double b[],int n) //高斯算法
{
int i,j,k;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
A[i*n+j]/=A[i*n+i];
for(k=i+1;k<n;k++)
{
A[k*n+j]-=A[k*n+i]*A[i*n+j];
}
}
b[i]/=A[i*n+i];
for(k=i+1;k<n;k++)
{
b[k]-=A[k*n+i]*b[i];
}
}
//回代
for(i=n-1;i>=0;i--)
{
for(j=i+1;j<n;j++)
{
b[i]-=A[i*n+j]*b[j];
}
}
}

int main()
{
double A[16]={8,2,1,2.5,1,8,-0.5,2,1.5,2,8,-1,1,0.5,0.7,8}; // 一维数组
double b[4]={1.5,-3,-4.5,3.2};
int k;
Gauss(A,b,4);
for(k=0;k<4;k++)
{
cout<<"x["<<k<<"]= "<<b[k]<<endl;
}
system("pause");
}


结果是正确的。就是还有一个问题
1、这里用的是回代法,怎么修改成无回代高斯消元法
pilow 2008-06-04
  • 打赏
  • 举报
回复
不过对我来说难了点。矢量都没学到过。。
pilow 2008-06-04
  • 打赏
  • 举报
回复
先谢谢了

不懂再来问
pilow 2008-06-04
  • 打赏
  • 举报
回复

pilow 2008-06-04
  • 打赏
  • 举报
回复
上面这些都是有回代的高斯主元消去法。我的目标是要实现无回代过程的高斯主元消去法,不知道怎么改下?
已经知道无回代过程的主元消去法算法:
第一步:选主元,在第一列中选绝对值最大的元素,设第k行为主元行,
将主元行换至第一行,将第一个方程中x1的系数变为1,并从
其余n – 1个方程中消去x1。
第二步:在第二列后n – 1个元素中选主元,将第二个方程中x2的
系数变为1,并从其它n – 1个方程中消去x2。
.........
第k步:在第k列后n – k个元素中选主元,换行,将第k个方程xk的系数
变为1,从其它n - 1个方程中消去变量xk,
消元公式为:

对k = 1, 2, …, 按上述步骤进行到第n步后,方程组变为:
pilow 2008-06-04
  • 打赏
  • 举报
回复
再发一个高斯消元法(选主元)的代码

#include<iostream>
#include<math.h>
using namespace std;
int GAS(float a[][4],float b[],int n,double ep)
{
double dmax;//用来存放列主元
float s,temp;//s回代时用来存s+a[i][j]*b[j];temp交换时用
int i,j,k,m;//m 为列主元所在的列
int IP=1;
for(k=0;k<n;k++)
{
dmax=fabs(a[k][k]);//找列主元
m=k;
for(i=k+1;i<n;i++)
if(fabs(a[i][k])>dmax)
{ dmax=fabs(a[i][k]);
m=i;
}
if(dmax<ep)
{
IP=-1;
return IP;
}
if(m!=k)
{
for(j=k;j<n;j++)//用于k列与m列的交换
{
temp=a[k][j];
a[k][j]=a[m][j];
a[m][j]=temp;
}
temp=b[k];
b[k]=b[m];
b[m]=temp;
}
for(i=k+1;i<n;i++)//消元
{
a[i][k]=a[i][k]/a[k][k];
for(j=k+1;j<n;j++)
a[i][j]=a[i][j]-a[i][k]*a[k][j];
b[i]=b[i]-a[i][k]*b[k];
}
}

b[n-1]=b[n-1]/a[n-1][n-1];//回代求解
for(i=n-2;i>=0;i--)
{
s=0;
for(j=i+1;j<n;j++)
s=s+a[i][j]*b[j];
b[i]=(b[i]-s)/a[i][i];
}
return IP;
}

int main()
{
float a[4][4],b[4];
double ep=0.001;
int i,j;
int IP;
int n=4;//矩阵A为n阶的
cout<<"Please input a[4][4]"<<endl;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
cout<<"Please input b[4]"<<endl;
for(i=0;i<n;i++)
cin>>b[i];
IP=GAS(a,b,n,ep);
if(IP==-1)//判断能否用高斯列主元法
cout<<"Gauss method is failure"<<endl;
else
{//输出结果
cout<<"The solution of equation is:"<<endl;
for(i=0;i<n;i++)
cout<<"x["<<i+1<<"]="<<b[i]<<endl;
}
system("pause");
}

baihacker 2008-06-03
  • 打赏
  • 举报
回复

//求2的平方根
#include <iostream>

using namespace std;

inline double f(double x)
{
return x*x-2;
}
inline double df(double x)
{
return x*2;
}
int main(int argc, char* argv[])
{
double x = 1;
cout.precision(16);
for (int i = 0; i < 20; ++i)
{
cout << (x-= f(x)/df(x)) << endl;
}
return 0;
}
太乙 2008-06-03
  • 打赏
  • 举报
回复
什么迭代??就是递归??
fallening 2008-06-03
  • 打赏
  • 举报
回复
设置好A[][] B[]的值,声明一个储存解X[]的矢量
直接调用
lq<long double> ( A, B, N, X );
Peter_Cheung 2008-06-03
  • 打赏
  • 举报
回复
改了版面, 大家给点意见, 谢谢
http://forum2.kingofcoders.com
fallening 2008-06-03
  • 打赏
  • 举报
回复
det.h

1 //return the determinant of a square matrix arr whose size is order by order
2 template< class T >
3 T det ( const std :: vector<T>& arr, const unsigned int& order = 0 )
4 {
5 unsigned int Order = order;
6
7 //if order is set to default, calculate it
8 if ( 0 == Order )
9 {
10 Order = sqrt ( arr.size() );
11 }
12
13 assert ( Order * Order == arr.size() );
14
15
16 if ( 1 == Order )
17 return ( arr[0] ) ;
18
19 T sum = 0;
20 std ::vector<T> arr2 ;
21 int sign = 1;
22
23 for ( unsigned int i = 0 ; i < Order ; ++i, sign *= -1 )
24 {
25 /* copy n-1 by n-1 array into another array */
26 const unsigned int newsize = ( Order - 1 ) * ( Order - 1 ) ;
27
28 arr2.resize ( newsize );
29 for ( unsigned int j = 1 ; j < Order ; ++j )
30 {
31 for ( unsigned int k = 0, count = 0 ; k < Order ; ++k )
32 {
33 if ( k == i )
34 continue ;//jump out of k loop
35
36 const unsigned int pos = j * Order + k ;
37 const unsigned int newpos = ( j - 1 ) *
38 ( Order - 1 ) + count ;
39 arr2[newpos] = arr[pos] ;
40 count++ ;
41 }//end of k loop
42 }//end of j loop
43
44 /* find determinant value of n-1 by n-1 array and add it to sum */
45 sum += arr[i] * sign * det ( arr2, Order - 1 ) ;
46 }
47 return sum;
48 }
49

lq.h

1 #include "det.h"
2
3 #include <vector>
4 #include <cassert>
5 #include <cstdlib>
6
7 //
8 //for solution of the linear equation
9 // A X = B
10 //
11 template<class T>
12 void lq ( const std :: vector<T>& a, //store A[size][size]
13 const std :: vector<T>& b, //store B[size]
14 const unsigned int& order, //store size
15 std :: vector<T>& result //store X[size]
16 )
17 {
18 unsigned int Order = order;
19 //if order is set to default, calculate
20 if ( 0 == Order )
21 {
22 Order = b.size();
23 }
24
25 assert ( Order * Order == a.size() );
26 assert ( Order == b.size() );
27
28 result.clear();
29
30 const T _D = det ( a, Order ); //store D
31
32 if ( 0 == _D )
33 {
34 if ( "Failed to solve the linear equation" )
35 exit ( EXIT_FAILURE );
36 }
37
38 for ( unsigned int i = 0; i < Order; ++i )
39 {
40 std :: vector<T> A = a;
41 for ( unsigned int j = 0; j < Order; ++j )
42 {
43 A[i+j*Order] = b[j];
44 }
45 const T D = det ( A, Order ); //store D[i]
46
47 result.push_back ( D / _D ); //get X[i]
48 }
49 }
50
myhuochai 2008-06-03
  • 打赏
  • 举报
回复
这…得用矩阵运算呀…
执假以为真 2008-06-03
  • 打赏
  • 举报
回复
对嘛,直接说不就得了嘛,还迭代法的C++实现。原来是数学上的迭代!
pilow 2008-06-03
  • 打赏
  • 举报
回复
用无回代高斯消元法,求解一次线性方程组
测试用例如下:
8a+2b+c+2.5d=1.5
a+8b-0.5c+2d=-3
1.5a+2b+8c-d=-4.5
a+0.5b+0.7c+8d=3.2

求C++程序
pilow 2008-06-03
  • 打赏
  • 举报
回复
努力研究中
K行天下 2008-06-03
  • 打赏
  • 举报
回复
迭代1+2+3+...+N


#include <iostream>
using namespace std;

int addn(int n)
{
if (n == 1)
return 1;
else
return n+addn(n-1);
}

int main()
{
// 求1+。。。+100
cout<<"1+2+3...+100 = "<<addn(100)<<endl;
system("pause");
return 0;
}



64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧