|
程序如下,是用来解线性方程的,用的是高斯赛德乐迭代法做的: `- V5 X* y. ^
在算三个未知量的时是和答案一致的,四个未知量就有出入,做六个未知量就出错了.
' p1 P9 Z, h. [/ J* s$ z谁能告诉我错在哪里了,是题错还是程序错了.
0 R: ?5 I- E& |% `) P. w" o刚开始学,不足之处请指教。/ q* |) }8 z( b
#include! a! l" |1 V2 a
#include' ^3 l) L U, S
#define NN 50
$ K% q8 r1 V4 f2 M+ ^2 p void main()
8 Z# C+ ^( b4 Q {
! F$ `3 j, S; q& |4 G$ C7 t7 i# @4 G5 W cout.setf(ios::fixed,ios::floatfield);$ b, `* C% J; a% b1 q& w
cout<+ h# U; l5 i8 j
int i,j,k;
+ Y3 ?: R+ L- M& O- G! c+ v double e,c,T,r;
* j+ R( M! b6 x8 a' l" |$ ^* V //double x[NN],A[NN][NN],B[NN];
1 e7 {( l( B8 D. ^: \ n7 J _6 w //题目如下:任选一个7 g0 {$ ~$ P% y0 ]" s, y7 |
//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};
3 h# Y; l( b; Y% R3 O' z$ r7 Z 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}; G J) t0 F5 E7 I0 a1 q# y
//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};
+ {1 S! G- Y: c: |5 |; b. y //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};
: q& h3 [! v4 b# G6 }6 c/ c //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};8 z6 ~' s1 E6 F! m
//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};* ~$ u' T k0 J2 p: e
int N;4 b$ X( r: c0 {" u
cout<<"请输入N值:";
/ F- I8 ]+ x9 E7 C cin>>N;) t1 a- e' J8 k2 L5 O
cout< I, A2 U" `, |5 j- P A; K9 G
cin>>e;
5 L( b* H1 e" s& @7 ?: ]2 |/ { /*cout<8 r0 p( h0 A/ C3 B/ o4 e
for(i=1;i<=N;i++)- V% W% k$ _$ W* i* z5 I
for(j=1;j<=N;j++)
( q4 f7 U! T; e) q# w$ B6 j. R {
$ U/ j- h" G' U9 b. ^/ ]: J+ g j cin>>A[j];
$ h( I0 h8 X5 D$ V) T$ [$ C0 ] }# X5 c) A9 q/ F w! w- Y" a! g% i
cout<: h6 b4 w) D- L7 e Y for(i=1;i<=N;i++)5 R) T5 f: Y3 w
{2 i9 U& Y& R2 ?. c! i
for(j=1;j<=N;j++)/ B- r" G0 |9 K+ S9 ?) r0 q4 t
{* ~% H: m/ R, W& S) j* Y
cout<[j]<<' ';
0 y# w# a( i6 I \' `
4 V% F E- y, ~2 x }9 u- L1 r. k0 W4 T7 t
cout<) X8 _; W1 b6 L* F5 H
}) k! q! j( M9 _' B8 U5 ~5 \$ G
cout<" \% W$ B: a& Q, N! _ for(i=1;i<=N;i++)cin>>B;% V) a' P# v" l7 x& @
for(i=1;i<=N;i++)cout<<<' ';# [( Z0 P6 u6 d: Z
cout<) \5 M* u6 g% l5 d* [% g! z
for(i=1;i<=N;i++)cin>>x;*/
; q D9 g5 @+ W+ P \9 i8 d }7 f4 V4 v+ f$ R k% Q
//cout<<"k"<: m+ B0 J5 x& g$ A/ C! t
//for(i=1;i<=N;i++)cout<<"x["<( c4 {0 j( {* B
r=e;
% t4 Y- D6 O0 P: ?) a for(k=1;r>=e;k++)
4 X6 I% Q! L, C% C" ~/ r { " A) N$ a2 }2 X5 f5 `- f
r=0;: A, Z3 S y- [8 `5 K
i=1;: v8 e* h+ ? m! s3 M& Z/ w& D
for(i=1;i<=N;i++)7 X% E* i9 L. \( G# }2 ?
{
4 t! a7 s0 o1 l6 [- W T=x;
, H# w9 u* G, I p: p c=0;8 a8 R. }! r b2 g; r6 | T( v
for(j=1;j<=N;j++). M4 D) E, a( U3 p
{' A7 q. f2 Z( B6 d3 h) P% t, p
if(j!=i)c+=(A[j]*x[j]);; y0 \1 I) j9 u/ N
cout<<"第"<: D: c% O. W6 A/ ?( k. V
}, K+ d# [" ^* c! a
//c1=0;
! l6 X* i7 F9 I' a; h; ^, h //for(j=1;j<=i-1;j++)c1+=(A[j]*x[j]);1 z Y3 `& x1 {# u/ m
x=(B-c)/A;
+ B9 X0 U4 N6 x1 p5 x2 M cout<<"第"<<* S& C5 a8 m2 E" b* n
if((fabs(x-T))>r)r=fabs(x-T);
& A5 Z( ]* S1 T/ R3 i cout<<"第"<5 G8 \8 S% z6 W, ]* k4 q( e$ K0 W" n
}5 [$ P: y+ X9 a+ P9 Y: W
cout<2 k0 }# h" G$ y! N) ^- W8 m' w& k //cout<<"k="<3 @4 |( q6 e$ ]- w! B
: P4 q5 }) L7 s; M5 n2 Q2 U //cout.setf(ios::fixed,ios::floatfield);
1 b7 m j! ]: E$ P6 g2 o' u; t# q for(i=1;i<=N;i++)cout<<"k="<<<' ';//<, P- I7 ~5 w- m* r
' o, y. |- G1 z \' d
}cout<% m, R2 p! R4 {/ J- b3 k
1 C8 b% H0 ~9 P+ b; U
} |
|