|
程序如下,是用来解线性方程的,用的是高斯赛德乐迭代法做的! N$ z/ H6 i$ k4 Z& x
在算三个未知量的时是和答案一致的,四个未知量就有出入,做六个未知量就出错了.- l ^% Z+ S3 t: [+ m
谁能告诉我错在哪里了,是题错还是程序错了.
% H7 N* f2 M; m刚开始学,不足之处请指教。
. n+ J, X5 ~- ]5 M$ W3 W#include
3 r$ D- X8 z$ ~. o: N" G3 u#include
$ w8 \! {. q; ^/ \ U#define NN 50
7 N; O W6 \( W F9 q void main()
* R; I9 q$ e, u% F% A( N { 7 V+ W' c$ C2 b7 w3 b
cout.setf(ios::fixed,ios::floatfield);
! s, T+ G' N3 e- o$ ]$ |, y cout<2 `( p" H' l2 I. Y" u int i,j,k;
. [% n7 d9 }, b double e,c,T,r;4 p9 ^" a' N& u5 \. G
//double x[NN],A[NN][NN],B[NN];
2 a+ p; ?7 u. i+ I$ o //题目如下:任选一个6 s+ j, m* `( Z7 D
//double x[NN]={0,0,0,0},A[NN][NN]={{0,0,0,0},{0,10,-2,-1},{0,-2,10,-1},{0,-1,-2,5}},B[NN]={0,3,15,10};
: M' v5 k" _0 a$ Y/ ?6 h double x[NN]={0,0,0,0},A[NN][NN]={{0,0,0,0},{0,10,-1,-2},{0,-1,10,-2},{0,-1,-1,5}},B[NN]={0,7.2,8.3,4.2};- t( }7 ^- r$ i+ v
//double x[NN]={0,0,0,0,0},A[NN][NN]={{0,0,0,0,0},{0,-5,1,1,2},{0,2,8,1,3},{0,1,-2,-4,-1},{0,-1,3,2,7}},B[NN]={0,-2,-6,6,12};& W; N& `# q3 y: `, x% I
//double x[NN]={0,0,0,0,0,0,0},A[NN][NN]={{0,0,0,0,0,0,0},{0,1,-1,0,-1,0,0},{0,2,4,-1,0,-1,0},{0,4,-1,4,-1,0,-1},{0,8,0,-1,4,-1,0},{0,12,-1,0,-1,4,-1},{0,16,0,-1,0,-1,4}},B[NN]={0,0,5,-2,5,-2,6};
A: I7 f7 z/ | //double x[NN]={0,0,0,0,0},A[NN][NN]={{0,0,0,0,0},{0,1,0.333,1.5,-0.333},{0,-2.01,1.45,0.50,2.95},{0,4.32,-1.95,0.007,2.08},{0,5.11,-4.00,3.33,-1.11}},B[NN]={0,3.00,2.62,0.130,3.77};; @' s4 [0 U1 G8 L+ T+ [
//double x[NN]={0,0,0,0},A[NN][NN]={{0,0,0,0},{0,10,3,1},{0,2,-10,3},{0,1,3,10}},B[NN]={0,14,-5,14}; K8 k; u' w1 H8 W
int N;- v% L1 z. @# n
cout<<"请输入N值:";& i7 L0 G' K. X) h
cin>>N;. Q9 s% v4 s/ S+ N& w
cout<; R0 c4 ?$ \' ?, z cin>>e;
: e- W. z* h+ k- q9 v6 Q7 { /*cout<2 r( z# ^# f) M0 ?
for(i=1;i<=N;i++)
% X/ a* f, j8 M8 P x( m for(j=1;j<=N;j++)3 s+ v' h+ z+ n7 m& H% L5 J7 |
{4 }! {. }8 q5 c6 n) ~! N) W; I
cin>>A[j];
2 j5 e5 N7 [ n }8 p, B7 m1 q+ l8 k5 [) y
cout<& b/ e8 p" f9 h# d% C for(i=1;i<=N;i++)2 S$ a! \# q3 Q2 n; B: s
{
4 M7 U, L5 b6 E2 L2 f, Y for(j=1;j<=N;j++)
6 r$ |7 l; {! _7 u9 N {
7 @9 C4 Q9 L/ R# X! l6 ^ cout<[j]<<' ';
# \3 z" R' }; c8 K' e, w6 r6 V- W8 j
; i w+ w9 _. K( M( d" r0 W }
' k" w. S" _/ N cout<4 z1 V' Y8 `* T% ~ }
- |7 l1 K t6 z- b& g5 g; T6 ~" K cout< f8 x$ H" g/ j4 v$ T; X$ k: ~
for(i=1;i<=N;i++)cin>>B;
7 A, Q* C6 `% x4 _/ Y8 f# Q6 @5 t for(i=1;i<=N;i++)cout<<<' ';
% x! R% s4 j# i, _0 r cout<9 u- E1 Z9 l& p! _8 v9 v% g for(i=1;i<=N;i++)cin>>x;*/& O' Y' N$ p$ r
! B5 x' L- o. S) B, l3 g( N p //cout<<"k"<6 R( H! D( e7 S
//for(i=1;i<=N;i++)cout<<"x["<" ]; J) I4 M3 ]4 a i9 d. Z
r=e;
2 x4 ?/ J) J% ]5 V for(k=1;r>=e;k++)! ?; _5 m {0 f
{
$ f) d3 Q6 D0 s$ T r=0;
- j! b" H( Z& `3 j. n i=1;' U. z; P: X+ c/ s
for(i=1;i<=N;i++)
* ~2 V4 i; ~3 c( D0 { {
- F7 r) J) d/ n( J7 E9 [2 ]7 x4 t T=x;. Y9 M0 \* z# e8 u9 S0 U7 R
c=0;" q' Z. L8 o* b
for(j=1;j<=N;j++): o+ Q B" I- F1 T; j8 M
{
1 ~; L7 t& q, `: X if(j!=i)c+=(A[j]*x[j]);. r# Y. X, I- _. W
cout<<"第"<. s) [7 w% u& [6 X! e. E
}" _# ^- m0 m, ?$ @9 r' a0 ^" v
//c1=0;
H6 L* p. j. o2 I1 Q: a9 ~ //for(j=1;j<=i-1;j++)c1+=(A[j]*x[j]);' @; Y: T0 L1 `& d( }2 k1 I7 w4 \9 C( J
x=(B-c)/A;
3 X) N1 \, H" U$ |) w V7 [ cout<<"第"<<8 o/ d' p* s/ ~" a- G" G
if((fabs(x-T))>r)r=fabs(x-T);3 U9 r: R2 W& i! z0 i# v
cout<<"第"<% H; Z3 W' l4 f; Q3 k4 l }6 l5 v' E) ~! M, y6 l+ \
cout<- O1 \2 X) a2 U5 ~% Z( R: H& ^
//cout<<"k="<% |# N/ N( m% r/ F* d ; R2 p8 ]0 j9 l. l3 T
//cout.setf(ios::fixed,ios::floatfield);+ \8 ~7 n# P8 I2 ^" b4 C
for(i=1;i<=N;i++)cout<<"k="<<<' ';//<7 N, l" ]0 A) U7 P- A* O
, f$ J+ P' P$ P3 k+ u2 f* {
}cout<: V5 d- o3 N" N5 ]2 h- X5 i1 B
: q7 r1 O, V, n5 E6 T} |
|