|
程序如下,是用来解线性方程的,用的是高斯赛德乐迭代法做的
. E9 G$ h% h6 i7 [在算三个未知量的时是和答案一致的,四个未知量就有出入,做六个未知量就出错了.+ o3 z8 u1 u0 J8 ^- O
谁能告诉我错在哪里了,是题错还是程序错了.
# E2 a+ y0 @* z* T& A3 B# k0 g, a刚开始学,不足之处请指教。
2 y+ ^$ V% [2 S5 G( J' D! M#include; b9 X: m5 v3 ]4 Z& n @) p8 g
#include& q4 n, d1 U2 J5 ^. _5 l
#define NN 50 ' B1 F' A+ V! B) t z% q
void main()
7 p9 a# z; P5 S% X5 u1 E {
$ i) ?& H! q1 i cout.setf(ios::fixed,ios::floatfield);
! D5 s. `. j* p+ s0 p& \8 X cout<7 ]1 l3 U2 b- s- x5 E- T" q, m: p; J int i,j,k;
; v" q! Q+ j m7 e double e,c,T,r;
. u- O0 w6 j0 k" x( m. L //double x[NN],A[NN][NN],B[NN];
" b3 j% `3 M" m, A8 H2 ] //题目如下:任选一个
$ g8 _5 T- ?# b //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};
" W! f% H1 b+ g 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}; B! W7 u) K: _
//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};
/ Q& P! p) ^/ f; r+ W6 I9 a4 T+ n/ V //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};
6 G1 M$ j* |8 v+ _ //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};
# [2 Z* {# g" q0 ]7 r //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};
$ G; w$ ^& L# U4 O3 {+ y8 h int N;
( e0 a$ @% S! W7 ~ cout<<"请输入N值:";
7 t3 {1 z8 F' t1 @' P: T; G cin>>N;4 y; J, e; i7 R# L: G+ ]2 h
cout<- k$ k/ r; p" X1 G) R" a, T0 O
cin>>e;! m/ p" W2 y; s: c1 k
/*cout<4 A+ C; q$ N# `" q3 z for(i=1;i<=N;i++): U! G2 ?0 ?/ p: I2 a% j) d
for(j=1;j<=N;j++), p1 Y u u1 M% F% r
{' h- n8 c) `2 E
cin>>A[j];+ x6 H5 _% t Y3 H* t' d
}
8 ^* G6 L! `3 [7 A: S( [3 O' r9 m cout<# V( }* n3 q* \: o5 f/ q* H
for(i=1;i<=N;i++)
7 ^% L, O; W7 h* k f {0 z* q/ U0 K' o# @ ^) n
for(j=1;j<=N;j++)- n" d5 u8 l4 P0 x e
{; J. _) j x' e( q- W
cout<[j]<<' ';2 ^# C6 \2 i3 O
. Y, l: t L$ b; S }) I8 _& v* i/ v$ C- X- Q
cout<8 \/ O2 _) |* G( m1 \3 u% D }7 T3 Q7 K) z" P. c! V
cout<4 h6 n# |5 H2 O5 }8 L
for(i=1;i<=N;i++)cin>>B;, h, A" [3 i7 n6 p* D. K
for(i=1;i<=N;i++)cout<<<' ';
4 a. [) |" K3 i4 Q& e1 C3 A cout<0 i2 K5 x3 B2 K7 r for(i=1;i<=N;i++)cin>>x;*/
' ~0 b. k* S2 q Z; f8 }8 I. c4 g6 L% A. g- E
//cout<<"k"<7 G/ ]7 r0 O1 l* _& }1 v
//for(i=1;i<=N;i++)cout<<"x["<8 {# F- j) e- @. U r=e;6 U8 B+ s- r! o( \/ ?
for(k=1;r>=e;k++)
5 z, l# Z8 r' Q8 {8 `9 e. H% T: j! s* { { . c% E P% p4 `1 f9 g! u( N9 M
r=0;1 n) M$ R5 W. V( N( C. ?" n" p
i=1;' \1 t- G5 |1 p# i- _4 r
for(i=1;i<=N;i++)- X* [8 X2 J- P8 L) J, w
{
! R3 o! s5 Q* ]' d" U( A# a& |) h T=x;
2 b% E x+ ?% m- `: [ c=0;3 i) A: b4 ^$ I# e f0 ?. x2 z% j
for(j=1;j<=N;j++)+ }3 I4 B) B, o! L R7 p
{
# f* B) Z+ {" d if(j!=i)c+=(A[j]*x[j]);) G( ^. E1 W6 d8 l# a& b' v
cout<<"第"<$ f3 Q0 o/ [" n2 Q: l; E! h
}
9 d, i6 j7 q" N% c //c1=0;0 K% N4 R ?' |$ S# y5 Q n
//for(j=1;j<=i-1;j++)c1+=(A[j]*x[j]);4 ~9 N' B% l# e! c( V
x=(B-c)/A;7 j4 ^. Q2 H7 A* D0 R# w
cout<<"第"<<9 M1 [2 P6 |' ]( K/ O0 ` if((fabs(x-T))>r)r=fabs(x-T);, ~% o& s/ [6 ]+ _* [
cout<<"第"<7 I* I) W! @. b" ?, |5 R
}
. R- e! H- W) [' h2 q cout<1 g5 Y' V7 R$ E7 N
//cout<<"k="<9 n; [' \. ^' G6 r1 K
5 B- w" T6 ~+ u5 y //cout.setf(ios::fixed,ios::floatfield);- E* o2 `3 I% Y! I
for(i=1;i<=N;i++)cout<<"k="<<<' ';//<' j: P7 I! r5 h3 G0 O, l$ O! ^
' F0 j5 {" p0 ~, ^3 k: F9 F1 ~ }cout</ A$ U5 N% n3 n( o; j6 V
' E. ]6 A$ }- u1 A! }: N" ?} |
|