|
程序如下,是用来解线性方程的,用的是高斯赛德乐迭代法做的; x C! C+ d/ F" T& e X
在算三个未知量的时是和答案一致的,四个未知量就有出入,做六个未知量就出错了.( w8 J7 V6 B& h! [4 j" b
谁能告诉我错在哪里了,是题错还是程序错了.! I9 W7 m. `0 S8 j$ Y
刚开始学,不足之处请指教。
) e! O2 _" v+ L4 q1 [; ?2 R: @& X- F#include6 G/ i, K C& y7 n
#include
7 h4 R# P( m$ a& B, I% ?#define NN 50
7 V/ h! k% r/ |5 ^9 D4 m void main(), v* L7 t! r" r9 L% n5 E
{
! i- I( ?2 g' r! J cout.setf(ios::fixed,ios::floatfield);
% f% x0 r k4 {1 z( } cout<! d% T1 l' z) T/ v
int i,j,k;7 d2 m' J; e$ I; {* r" ` Q( {
double e,c,T,r;& D% g4 \( t9 b# v; c
//double x[NN],A[NN][NN],B[NN];/ a. |4 p3 i& K# B
//题目如下:任选一个
) v* |) o& D$ I0 N- ` //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};1 T2 \' ^! ~7 N0 G+ 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};. q4 H2 f6 a @; H
//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};; W! e9 t& r8 G- R
//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};
+ Y" m1 G1 d3 M, ~3 L @, P" 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};
. D# c1 V8 W2 I/ ]; u+ Y //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}; l2 Y$ ]' b* L
int N;
% X) _/ g' G+ t* z5 @ cout<<"请输入N值:";$ T: }! u k9 \0 W- V4 D6 B, T
cin>>N;
" a" Q& ?' K: N9 a cout<, b2 O8 m# J7 H1 D& r- `& Y cin>>e;: f0 Z# V. n- `0 p
/*cout<* B! U, b" |- N/ T2 c% N for(i=1;i<=N;i++); x7 T1 x2 W# b- c2 G# C& ]# |
for(j=1;j<=N;j++). Q7 B$ W1 P9 U. D
{
$ N" ^! ~; q: v1 m) P cin>>A[j];
6 ~$ V7 O' g4 j' ~7 H# z- R( d- F }& w; c; L# k/ y( Z; m6 o' \, ?
cout<& l: x. E$ K6 q for(i=1;i<=N;i++)) _! _) W' H1 C w% ~0 v
{
0 _ {6 I# ~+ O* A for(j=1;j<=N;j++)/ U; O% r& C. O D. w
{
. u1 J/ A+ [! I8 V, E cout<[j]<<' ';
5 H, n) K+ f6 ^8 G! K. K- Q
# U4 e' n6 n. [1 k7 C- `! | }
$ s0 ~' m5 z8 B9 O cout<( I3 _+ x$ L5 U }
! d) k! _: |7 M cout<5 A4 ~; z. R" | for(i=1;i<=N;i++)cin>>B;
, u1 v! l5 U/ U3 L0 r& k! d3 r for(i=1;i<=N;i++)cout<<<' ';' O. J0 k- ]6 }7 s2 A. j6 [
cout<9 y* F) j. p2 g3 b1 W
for(i=1;i<=N;i++)cin>>x;*/# w5 t; U, {8 \' s
/ }8 v, Q$ A- K- }8 _
//cout<<"k"<7 D+ ~9 \5 [; t( E, I //for(i=1;i<=N;i++)cout<<"x["<! [" g; z- {5 U7 H6 l# O r=e;
7 q- P" p& t3 H z' z for(k=1;r>=e;k++)
6 V N u) Q0 T { {. ~- D- e$ y: G# V
r=0;
5 p. p2 c, P8 g& ]5 e3 z7 P i=1;2 N* A; ]- C! h1 T
for(i=1;i<=N;i++)
/ x: {5 ?' R" v& s- c1 z0 I {
: I0 G$ B' v* {7 ^0 a) [ T=x;0 r+ ?( X, Z5 k7 u8 M5 A' \- Q
c=0;: i9 O! G- g r, f. A# v+ \* p; t
for(j=1;j<=N;j++)/ w& ~, ]( b! p: y& W
{1 u1 P5 y: [) H/ G* a6 O2 e
if(j!=i)c+=(A[j]*x[j]);2 C4 \7 |7 N/ D* U7 W
cout<<"第"<6 F7 S7 R) c. h }" k1 B |) W3 O |" B( E5 z
//c1=0; }0 e4 O9 F; v# ^5 g
//for(j=1;j<=i-1;j++)c1+=(A[j]*x[j]);; O4 _' D# B5 r2 H: P+ t
x=(B-c)/A;: H# p) V. k+ x
cout<<"第"<<2 j+ K- e$ k- d W* t
if((fabs(x-T))>r)r=fabs(x-T);7 N4 j! J+ `; t. k& Q. B9 x m; _6 D
cout<<"第"<- i1 h- x$ P W }
; d% |' Z6 K ~. f2 j% H cout<$ V3 K, d7 I3 ^7 h# F
//cout<<"k="<; q; G q- o: V+ O9 [ t 6 r3 {- w) p/ ~- Y2 ?* D. \
//cout.setf(ios::fixed,ios::floatfield);
# q" {: o! J3 V1 d for(i=1;i<=N;i++)cout<<"k="<<<' ';//<1 G) K: x2 T% T) ]' y* C) H
6 x/ z2 ^7 ?2 x
}cout<, y- k' I4 W# r: L8 e9 @* d% h% m
L% s- ?- ]3 A8 o3 z/ j
} |
|