|
程序如下,是用来解线性方程的,用的是高斯赛德乐迭代法做的
4 _* d' l% h) b: {3 [3 m在算三个未知量的时是和答案一致的,四个未知量就有出入,做六个未知量就出错了.
$ p- I! w7 o( Y& |' _谁能告诉我错在哪里了,是题错还是程序错了.
g6 G9 [* K. S! x3 l4 b% Q刚开始学,不足之处请指教。/ u7 X# L9 a& ]' d% b
#include
9 u5 z+ g9 D4 D+ b4 \" P+ N1 ~- B8 [#include
7 m- ^: h. ^- x5 y- M- c#define NN 50
4 O u3 T. P! P9 ]' G6 e1 s- I void main(): u$ D: R3 Z0 e8 M7 M4 `8 U
{
$ o+ ]& ^ h+ Z cout.setf(ios::fixed,ios::floatfield);
' t5 _! z" d, K7 c cout<! `1 w: a, T, |6 y0 v5 J2 ~ int i,j,k;+ j9 b7 ^. S9 E, d
double e,c,T,r;
) A; U) ^, H- n3 p: w' c //double x[NN],A[NN][NN],B[NN];" Z R, J4 G. s3 c$ \
//题目如下:任选一个* K; g( |5 e5 F" }/ Y
//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};8 n- h, [8 S; F& F5 x: T% d
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};& F% Q# n' E# L' ]5 X* ~* A
//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};5 Z# y0 Y; N( s
//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};- \1 j% L8 X/ E+ D
//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};
7 f3 L& ?0 o5 L+ k //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};' g8 y% x( B) `$ W6 F
int N;
# }3 X0 K* \' E0 F7 u cout<<"请输入N值:";
8 L# z2 x8 Z4 s' s' w" I cin>>N;
g8 {/ K0 s+ N3 E" H: F6 ?1 @. s cout<. N& u" } Q7 j cin>>e;1 t5 b+ z) b' N' D- k, x6 J
/*cout<8 W5 p4 ^/ N$ C$ o+ A: t9 E for(i=1;i<=N;i++)! `4 o/ @7 h. l0 F7 Z9 c
for(j=1;j<=N;j++): W/ A2 O. z* g
{8 @+ J1 R( i1 |7 h- C
cin>>A[j];
) r2 R) Q! N7 R, {* u. y9 V- f6 f9 S }0 ~5 ?$ c4 H8 v f6 g) L; e8 h. L
cout<) Z! X# ~9 O6 k5 z7 z
for(i=1;i<=N;i++), N$ p& z& R F0 r( ?
{! X `- o5 ], X2 q% ]
for(j=1;j<=N;j++)3 N O5 M: T* i/ ^) Q; j
{
2 M+ y* f- A: u+ V8 m' M( I9 r cout<[j]<<' ';
- U! w0 k) c7 `1 T4 E # k) ^1 j& q4 Z, t0 {5 a+ M% H
}. e1 h: t/ I% R0 F
cout<6 a. g0 f% J# j, ~: p. Q }: x, h/ T& P* z! o) M
cout<! d+ x+ d" u1 B% z& }; I% P
for(i=1;i<=N;i++)cin>>B;) q9 ]9 U1 u8 F' N, Z' }( ^# n: c1 r
for(i=1;i<=N;i++)cout<<<' ';
, h0 B5 K% o o" r cout<; ^; [6 E- z# b/ C8 V
for(i=1;i<=N;i++)cin>>x;*/2 g6 r9 x, y. X
( h$ I1 P$ z- q' [' u6 H7 x //cout<<"k"<2 I' \! n+ _7 n7 h0 \- R
//for(i=1;i<=N;i++)cout<<"x["<' O0 A1 C4 W( G8 w# p6 n
r=e;
+ ^% T9 m) k# l9 X) ` for(k=1;r>=e;k++)8 z7 C% |) F0 X: h4 n; P1 ?2 I
{
. }& F1 C4 P; J& x r=0;, ]8 z7 S- f5 h3 I
i=1;8 y" m8 W/ m9 J( k1 Q1 U
for(i=1;i<=N;i++)' \' f! g M6 p% K: N( s5 n/ E. |
{9 M1 f5 C" C h+ C
T=x;& n! k- t M( ~% X* ]/ u
c=0;
) v7 n* ] E8 b. @ for(j=1;j<=N;j++): v3 e) r% \, U9 @8 [ H! g; l& K
{
( |( r! T3 \+ A" k# i' Q6 P if(j!=i)c+=(A[j]*x[j]);
5 E8 |1 }! u2 _/ M" x* V cout<<"第"<& c: |; U. l0 A3 J }# Q4 ~/ p$ p1 `1 Y
//c1=0;6 Q" x9 G; G' o/ G2 p" E. p
//for(j=1;j<=i-1;j++)c1+=(A[j]*x[j]);
* k* I/ P) Y3 W3 t, t x=(B-c)/A;
1 n& P9 W$ J% c8 v" w cout<<"第"<<! }+ g2 W9 I5 X% ~: R6 R if((fabs(x-T))>r)r=fabs(x-T);& K5 m1 H" m2 j9 M1 k& u
cout<<"第"<6 ^1 V9 q5 d* i$ z, T4 U: ]9 X( b }
/ p5 J* U/ e3 P) R& V cout<% @. X9 |6 e0 i, |7 {
//cout<<"k="<: e6 D, B$ X: b/ F7 b 5 ~( K9 C8 j3 O! P7 ~
//cout.setf(ios::fixed,ios::floatfield);3 }( [+ T" V6 u2 \
for(i=1;i<=N;i++)cout<<"k="<<<' ';//<8 Q+ t/ k0 F& F: R3 p- C
' ]* I" `0 O$ w- W' M }cout<' \0 g1 }6 _% q 8 P1 A8 M7 d6 [
} |
|