|
程序如下,是用来解线性方程的,用的是高斯赛德乐迭代法做的
. M( L3 `( z) v, t. N" H5 ?在算三个未知量的时是和答案一致的,四个未知量就有出入,做六个未知量就出错了./ u! J9 c1 r2 h6 o6 V
谁能告诉我错在哪里了,是题错还是程序错了.
4 F8 U) @6 [/ X. y刚开始学,不足之处请指教。
6 B* Q( H* L5 n' y#include8 R9 N. m2 c8 i$ \! z
#include) U k( _' p; R# K+ z$ _5 {1 W: _. [( H
#define NN 50 p# g8 n* z8 W$ i! |; q
void main()
0 r+ z2 P8 D/ |) W {
) q X3 {( j- m5 J8 O7 c5 y cout.setf(ios::fixed,ios::floatfield);
7 R$ X1 e; I* g7 q9 X cout<, E8 G; @% C6 E z6 X5 ? int i,j,k;
( |# e+ X t! ^! g" j6 e g double e,c,T,r;
- W& c7 z+ u! T$ I* A' [# i4 S$ l //double x[NN],A[NN][NN],B[NN];
8 ` I$ z& m0 T6 C //题目如下:任选一个
7 O7 H$ B& s8 P- U //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};) w( t. y, Y6 m3 A$ x
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};$ ~$ I/ }8 T- A1 t, P
//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};
' f4 t% q) z1 P- e5 Z //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};
6 \9 f6 b5 q, I //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};
. b: C- x/ l( {+ L* M1 L- c* R; 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};) n- m1 o7 Q5 o8 Q `, I
int N;, w6 @2 Z x+ ]8 V5 s, U
cout<<"请输入N值:";2 i+ s' H: L6 u, }1 X8 O
cin>>N;
. G1 d7 `0 {6 L6 m cout<+ Q/ E; `7 @8 y- x; l) [
cin>>e;9 D1 n" E: O0 B
/*cout<3 F, e( {3 |7 V$ x$ V for(i=1;i<=N;i++), K, \! N0 V8 L% d* U7 u4 i: Z
for(j=1;j<=N;j++)8 ~. q! b! Z1 d: _; i
{
! a$ _2 @( a M: P o+ a cin>>A[j]; x/ E5 c% |! d! o3 K4 [
}1 v8 A0 S$ T3 q6 l" T0 W3 C4 f
cout<: \5 y$ B% n& |6 [2 B; G
for(i=1;i<=N;i++)
8 g( D: a: Z4 e! z& a2 x# @ {
" _8 N- f* X0 d. n7 `3 Z; s for(j=1;j<=N;j++)2 l7 L0 f) k& d7 T4 p
{
& Q* t% i( Z$ F" g9 i. g: M cout<[j]<<' ';# K8 F9 r% b# L: @% F% y
$ T A. ?8 W* }4 m3 j7 W
}
2 c% H/ `2 r/ M1 y cout<, m3 c) j* [' B8 x" S }
9 n3 ?9 h; t) u. U/ N# p; _6 [ cout<1 A) c9 E& V v S; w for(i=1;i<=N;i++)cin>>B;; j" u$ p7 \0 G
for(i=1;i<=N;i++)cout<<<' ';
: P1 U4 r) I8 e2 O cout<2 d- ?9 w6 n3 N for(i=1;i<=N;i++)cin>>x;*/4 F( h, ^' {4 r+ D1 L7 R
; A7 o u, n8 c! T1 r6 M" g //cout<<"k"<, Q, _) ?" a- Z1 o
//for(i=1;i<=N;i++)cout<<"x["<5 Y: S# l! k+ m$ Z r=e;! p, ~! R' y4 U6 f- v
for(k=1;r>=e;k++)
p4 G& N) \) L% T# y {
. W+ w B! r- ]0 y4 I3 U% b) c r=0;9 p$ O# Y N3 Y# B# j
i=1;
$ |7 o* _. C, z7 d: X# I for(i=1;i<=N;i++). _, W( \; s2 h, Y* D
{0 C8 b. J; t# r0 V4 ~/ R1 A- x
T=x;) Q5 a; Y5 I- E; u" O6 z ?
c=0;, U' j) m( k2 m2 I
for(j=1;j<=N;j++)( e$ h: \# j/ z# `, d& H0 a& V. A
{ h1 e. s' B# w7 p
if(j!=i)c+=(A[j]*x[j]); [' @5 h& _7 V" ~4 ^( k
cout<<"第"<6 {7 m. W4 q/ j8 h) H: ^( {$ Z1 _& B" A
}4 H* E6 J3 q$ J1 o
//c1=0;8 ?9 `, G3 {/ M% v8 v, N/ K
//for(j=1;j<=i-1;j++)c1+=(A[j]*x[j]);5 W9 P, g, j; _
x=(B-c)/A;
3 D4 r3 N# L' {1 |: x' K0 M8 D cout<<"第"<<2 j' T r9 u. x4 i
if((fabs(x-T))>r)r=fabs(x-T);
* d2 U! Y T* |2 \1 P cout<<"第"<1 R% y' }; {/ N1 x; y" O
}
, V: f5 S5 x9 I- r4 v cout<7 X* T( e9 w- w f
//cout<<"k="<+ m( O O1 a: [# {0 @9 s/ v
, ^$ r/ C. Q' B9 t% s! { //cout.setf(ios::fixed,ios::floatfield);
3 m* Z3 u% l, I for(i=1;i<=N;i++)cout<<"k="<<<' ';//<& @9 |# e( I6 @) m+ x
* j7 ~9 x- [3 Q) F- z5 R }cout<. v! ~0 s$ U6 C( X/ l ! P& N4 u0 e: n, Y! `1 M
} |
|