|
程序如下,是用来解线性方程的,用的是高斯赛德乐迭代法做的
7 k9 N5 W; w" K3 s% n; M在算三个未知量的时是和答案一致的,四个未知量就有出入,做六个未知量就出错了.
/ f- p# j9 r' e3 G3 c) _* Q. N谁能告诉我错在哪里了,是题错还是程序错了.# f A% x* G6 K% F0 p
刚开始学,不足之处请指教。5 O% I% x# U, s& c
#include P+ U+ o* v1 i
#include
+ S# j3 I8 k4 n9 q+ Y; n#define NN 50 , [% g, @/ p5 t/ B6 Q
void main()
+ L; A* v9 O8 k4 G5 ]4 J {
; ~7 {$ z1 B6 D! D cout.setf(ios::fixed,ios::floatfield);$ f3 ?3 T- K& C# B& o
cout<0 U7 N% v: Q' W+ F0 H int i,j,k;
, g: s$ W: H' ` |7 }8 y! _2 G. y. K/ p double e,c,T,r;
- A7 y/ a9 C. @ h, z" \ //double x[NN],A[NN][NN],B[NN];
' K) P3 [6 b4 o6 K6 v1 C5 v# ] //题目如下:任选一个$ o! y. R9 ?* S H0 [7 M3 X
//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};
' s- E; M2 ?5 ~9 t7 P. E3 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};7 _! ]$ {/ [# L
//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};" G- E7 n4 h" \
//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};
( T( ?# g; I- k( Q //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};
& T0 A0 n9 R+ s$ s' M6 \ //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};
% S5 G8 \2 I) K6 I3 v8 @) ]: i int N;' O4 b4 J! e* H) b0 E! }- \( \
cout<<"请输入N值:";3 F8 d% `' ?+ i
cin>>N;
3 r) c3 }8 C% A. L9 e cout<7 y% Y( R$ A1 F, ?7 [2 w' a- V7 n
cin>>e;
/ _' |+ }& F2 |8 U, \5 D' `9 W /*cout<$ r2 q" D6 f& J7 u8 r- P+ C
for(i=1;i<=N;i++)& o1 }: R5 V, ]9 }% e: Q
for(j=1;j<=N;j++)3 t, a/ L' {3 P
{
5 q5 t. q/ |% ]4 I, K1 }* Q cin>>A[j];
% i9 [. K) U+ \$ p `5 B }' `0 d, H+ E1 z: I
cout<3 \ G+ K* `* A! V5 q for(i=1;i<=N;i++)
3 I8 c( G8 R3 w' ^/ _ {
+ ]/ `2 i6 l. j: k/ V z- ` for(j=1;j<=N;j++)
3 p: U( z# k: }' z& m {
, q7 i. @ j5 k5 L* n( @' \3 z cout<[j]<<' ';
; g* h8 @$ j1 |8 l
9 S4 \- w, ?4 h* [1 Q }
- I9 M, ] Z% i, [. h cout<2 l8 _0 {+ [& E5 u! Q! N }
( w& q( H' O1 ` d cout<: K) E) d! d! A, ~
for(i=1;i<=N;i++)cin>>B;" d! e W6 g) z
for(i=1;i<=N;i++)cout<<<' ';6 [; t- z+ s6 a, V7 N3 [% }
cout<' _5 J c6 \; n8 H: p" T
for(i=1;i<=N;i++)cin>>x;*/7 l9 B' c: Z4 K4 A1 [
& w1 v1 g( H1 z
//cout<<"k"<' K) s c. S( A8 s6 J! t7 X8 P( k! ?
//for(i=1;i<=N;i++)cout<<"x["<) r7 H$ V$ B, ]# L2 O1 F r=e;6 p- B% r" z; }. H! |
for(k=1;r>=e;k++)
! W& L3 Y& Q% W$ e, C { 4 U$ w9 S' Z. D5 n
r=0;" [* f: V( _9 [( i2 u) ^
i=1;
; q2 D& M8 z" C5 i4 V& d a% R for(i=1;i<=N;i++)
6 \0 D* r, l: b: z/ r, U {
! I' B: |4 R8 j, f- s- E2 H T=x;; D$ _! |2 }% I Q$ ]
c=0;2 H8 w( g4 ^; }; \/ W# ~
for(j=1;j<=N;j++)3 k7 w* h$ G7 g7 \! k
{
5 M' x& | f+ i" l3 [ if(j!=i)c+=(A[j]*x[j]);
5 D+ {) g# G- l7 v: S% H5 M cout<<"第"<7 X: q; q- ^) h X! ~6 e2 V }3 a! ~- P: a# F) V6 o
//c1=0;5 v5 ]0 O) S" }. ^ Y/ F+ Z
//for(j=1;j<=i-1;j++)c1+=(A[j]*x[j]);3 R$ I" z& e/ ~1 q0 Y; w
x=(B-c)/A;
_( b+ w ?0 G& f& U cout<<"第"<< s+ v( a t Y$ m" c1 Y* x/ Z1 R; u9 f if((fabs(x-T))>r)r=fabs(x-T);! `5 m8 W: G2 v0 k- c* Y6 p
cout<<"第"<0 f; R: S3 D9 v" J
}
, i+ K* l/ ]9 U4 e cout<8 W9 D' m# {+ a% M5 O8 M //cout<<"k="<% P1 x! u9 H' K5 l* R( }, q 4 y) X: }6 K2 ~
//cout.setf(ios::fixed,ios::floatfield);9 t! I1 v9 m3 e) ^7 z) j
for(i=1;i<=N;i++)cout<<"k="<<<' ';//<& H& y" X* C/ ]9 J) H) s5 J2 X ; b; f+ r7 V+ t
}cout<5 \8 c* X/ E4 m! e9 _) w: a
( ]. \, }$ q6 z8 C+ n} |
|