|
程序如下,是用来解线性方程的,用的是高斯赛德乐迭代法做的$ D3 k# F# H9 |/ J* k* u
在算三个未知量的时是和答案一致的,四个未知量就有出入,做六个未知量就出错了.
' A0 v; W$ O8 x谁能告诉我错在哪里了,是题错还是程序错了.
! e& s2 C3 H6 s O; d刚开始学,不足之处请指教。4 l" f" I0 V, [+ V& d
#include
6 Z/ b% t7 M" i- r) n3 y#include
4 N! N2 p; j* \3 j9 [#define NN 50
* w; [7 s' `0 D9 E0 g( D: V2 g void main()0 Y! \0 v9 G9 [! P' ?
{ M- c, Q+ h( m ^4 b4 G8 U) q
cout.setf(ios::fixed,ios::floatfield);
* T# N" w' @9 m6 S9 T" g8 @' B; Y% K; x cout<' ~2 a% d) b1 X7 Q" P
int i,j,k;
$ x$ k# }' J9 Q3 I double e,c,T,r;
' g" N# A$ Y u1 i //double x[NN],A[NN][NN],B[NN];# g! l T+ u" C( N
//题目如下:任选一个& f6 f% G$ w* \/ [
//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};$ f* ]! T* a4 U
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};
2 a7 r- L6 B* Y7 K- ?$ w% N //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};% B# o$ H: y* ~* T P0 k
//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};# A" W* G! b& M4 B& ]7 U( M9 @
//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};
. M8 n w5 h3 E0 B, j //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}; l+ S1 n3 N+ z# U. l- a, G
int N;
$ Y" p/ Z4 x6 u& ^/ z cout<<"请输入N值:";6 |- ^" j- h0 s# J) m3 L. E& I
cin>>N;
K& l7 @6 ]1 q) [/ L cout<: I3 e/ N' }/ z9 }7 @4 v' c cin>>e;( Q5 Q' M: E' r
/*cout<# c4 n' O! u z3 g* Q8 L! u for(i=1;i<=N;i++)
1 k+ T: m& f+ Q7 F( T for(j=1;j<=N;j++)
/ j+ T5 Y" T G5 v; j! p' a {5 B# h8 D+ p0 {2 J; A
cin>>A[j];6 l4 V/ b2 B* W6 d j
}' a$ o0 k6 _8 Q% _: N W# S$ t+ O
cout<- o; a. ?& t/ S( e) \0 k7 {% g: d for(i=1;i<=N;i++)
/ C' f, u! C3 N: U$ t; ?+ s {
/ k9 G9 J+ @+ {! ]& W5 Y7 m+ R for(j=1;j<=N;j++), k1 h. u/ d& G" N5 k: m
{5 r) }5 Z# I4 P# D. }
cout<[j]<<' ';
2 k2 O2 p9 m8 E$ m9 X9 k 8 @, b$ m4 @" d
}/ y. H* R! n/ S/ m
cout<9 t% C# L( I9 y4 r% G" y6 U }
, {( K+ `4 ?; w @% S cout<3 q3 R* y+ _% |* x3 o8 U for(i=1;i<=N;i++)cin>>B;% _5 L6 Y5 j3 D, j" ~/ R7 u
for(i=1;i<=N;i++)cout<<<' ';4 W% }/ N8 T( o: o7 g
cout<. o# G* H# T/ N+ a+ ^
for(i=1;i<=N;i++)cin>>x;*/
4 H6 l; P, O+ V0 r
+ E( N" g6 g9 E1 z, F //cout<<"k"<* ]. P! _. }$ D! k7 k. X
//for(i=1;i<=N;i++)cout<<"x["<; e# w. `' X1 K+ E" y$ g# X! C8 A r=e;9 y3 o! \3 l8 r" J. E2 ^
for(k=1;r>=e;k++)
# g9 @8 w/ U5 k0 g6 K4 a7 x { 1 L) L$ J4 V7 B) L- ^; ?! c
r=0;( f0 f! w8 e! x
i=1;
( w% q/ w- F. L7 K- | for(i=1;i<=N;i++)
2 m- H; q' T5 |/ W- g {
# m7 g. I1 N, d& g/ d9 {) l5 M T=x;
. k* T4 s( ? A+ N4 F) N1 W c=0;
/ D; I6 F5 S( o for(j=1;j<=N;j++)# L+ A) |8 ~6 `0 h& Q
{
8 t3 Q. b7 T3 J if(j!=i)c+=(A[j]*x[j]);
R0 w- C( V. U2 r cout<<"第"<: y; A5 C4 v; ~1 E" {0 g }
. F) E9 X' f$ ?4 M6 x3 @3 S6 `4 K //c1=0;
8 _. {7 F- f2 d4 [2 r' i //for(j=1;j<=i-1;j++)c1+=(A[j]*x[j]);
( N' N; B1 [6 x3 O( Y x=(B-c)/A;) u3 T# y. C/ u- {$ Q$ z
cout<<"第"<<; F$ l Y3 S Q Q+ r: z if((fabs(x-T))>r)r=fabs(x-T);
" \* G& F, @6 t6 q& D5 T cout<<"第"<: t+ U, \* j) C- C }
; G& x; @' T8 r& H cout<' B8 ?9 |/ z6 `- A& d3 L
//cout<<"k="<$ ~( U; z1 g3 A8 X! I
( }. s4 }4 f' T& Q9 V3 m
//cout.setf(ios::fixed,ios::floatfield);1 g- _# ?" J& x, H
for(i=1;i<=N;i++)cout<<"k="<<<' ';//<9 F3 ^) n, }) ~2 H9 D
& h* ?7 W9 C ]! p }cout<2 T0 O1 J: Z' H4 T/ O
4 ~1 R; j5 O. N& v8 e' o: S
} |
|