|
程序如下,是用来解线性方程的,用的是高斯赛德乐迭代法做的9 ?2 L- B. H! x1 A' J
在算三个未知量的时是和答案一致的,四个未知量就有出入,做六个未知量就出错了.
" t( x" \# y6 f6 c+ \谁能告诉我错在哪里了,是题错还是程序错了.' B( v: I9 P) k8 D$ w2 [ _
刚开始学,不足之处请指教。
+ B7 ?& c" ~* z6 [#include3 I3 K( |% O4 _" b) V
#include: ] U7 S/ D% I& G( y: T
#define NN 50
) N, U: v- ~3 h2 Q& I( |3 u void main()- h: v5 e0 {+ ^9 q9 a1 K
{ : a# R, v7 ^. q: _% r( L( h
cout.setf(ios::fixed,ios::floatfield);$ V/ g& \* @ _" Z f9 C
cout<% j3 E) B3 ]# B int i,j,k;# ]" Z& g" e7 c9 Q
double e,c,T,r;, U) q$ W: y+ o
//double x[NN],A[NN][NN],B[NN];" z% P% I7 d7 u. C: c
//题目如下:任选一个
?4 h% t. ~ e4 u+ g: W0 E //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};+ @; J/ R" ` V& Z, w
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};7 W# G& h3 { i# k/ q
//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};2 Z3 i9 u& V& X$ W+ I3 ] O$ E
//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}; ^* h. Y& `) |8 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};' \& ]9 i0 {) r$ q% M, 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};" i m) z, V+ ^: M6 ?
int N;( M. S, o- E% l* e0 m% _3 E
cout<<"请输入N值:";) C# i/ x7 @: }# q y" F" L( x) `
cin>>N;* |3 b$ l7 `% Q! @: T6 d" `
cout</ [8 V9 P N5 O0 Y4 F. q4 d
cin>>e;
0 k3 e8 o# G: P$ c' i2 { /*cout<$ O5 r* J/ f4 {1 d
for(i=1;i<=N;i++)
/ D( F& v% Y8 S7 `5 f for(j=1;j<=N;j++)4 Q$ M2 c, c7 r4 D/ \! A
{
" H6 S& D: e7 f* V9 ? cin>>A[j];
v6 p$ |2 p0 B5 K& Y }
7 ~: `+ v7 C4 i4 D$ w# I cout<" |; A7 a; f( q$ P+ x g for(i=1;i<=N;i++)" V1 i3 U/ P! T6 ?
{" N9 Y0 b0 s0 q. \) {
for(j=1;j<=N;j++)9 b: f; V1 b. \+ k$ m' n+ ]: R3 T
{/ }0 i4 ?2 A* y/ j/ ~
cout<[j]<<' ';
; s, C. |+ ]3 J' P ! R) \5 B0 p3 W @5 d
}
3 c1 [1 h' y, T# Y cout<: T5 [' I% a* p4 \ ?! C* k1 h }
, ]& t* q( E& D ~8 Q; [ cout<1 V2 n: c) j. P8 Z* g
for(i=1;i<=N;i++)cin>>B;
! C) i v+ O1 ]7 ` for(i=1;i<=N;i++)cout<<<' ';# l, A5 V' y% Q, e: D/ j: {
cout<1 H0 }% ^; V" I
for(i=1;i<=N;i++)cin>>x;*/
& l+ c1 _/ ?: N: p3 u& U+ g# ?
' q$ J' ^9 x3 m( }* F //cout<<"k"<& G; \4 g7 u7 C //for(i=1;i<=N;i++)cout<<"x["<5 I- t5 c: x' l( I& b
r=e;
) n8 F5 _. R8 z4 {6 R for(k=1;r>=e;k++), d+ A. q2 U( i7 r0 M: `
{ " N w0 _! W8 Z; }
r=0;
7 P7 {5 K! N$ e% b4 A i=1;. X. M' e" H) ]0 O/ j, _ |
for(i=1;i<=N;i++)0 Z. y; s7 B9 v; T4 I! @& s" ~
{' u4 H+ \8 [4 Q( f4 L6 W7 d
T=x;% T/ w3 C* d2 h& y Y( r$ R
c=0;
" h- W7 N- A+ r4 W' [7 f! E. x for(j=1;j<=N;j++)
4 i; s# i2 c4 l$ F& u" k* n8 ] { g0 H) I. V+ y! N0 c2 ^% c
if(j!=i)c+=(A[j]*x[j]);( \* Q8 S9 ]. _3 j( }
cout<<"第"<# A& Z8 u7 W) ^0 b+ b. Y4 g% b6 N
}* |( {- n X- T8 H/ U; D
//c1=0; t; Y+ u4 O% W: y
//for(j=1;j<=i-1;j++)c1+=(A[j]*x[j]);
8 K: E) V9 Z1 Z0 S% M x=(B-c)/A;. I) `; z1 _! q: Y9 ~
cout<<"第"<< k6 ?& p: J* n/ H( _ if((fabs(x-T))>r)r=fabs(x-T);
& U5 h/ K& a" L& `2 a cout<<"第"</ o5 ]" l$ i8 i! Q" Z }: ]+ }3 H5 D, \) y6 g+ u
cout<6 H$ m( u& M2 b" D' R1 h //cout<<"k="<4 r9 j5 j; P8 }9 ]% m4 I
& W& ~8 ?& I$ X
//cout.setf(ios::fixed,ios::floatfield);
8 Z: S' @4 K0 X* f for(i=1;i<=N;i++)cout<<"k="<<<' ';//<1 e3 q a+ x( D1 F! f # V+ G* i( |: G- V Q" T
}cout<# C2 u+ [7 K/ y2 W; O" u4 l! G, }
$ A. T5 i4 z! w" T} |
|