|
程序如下,是用来解线性方程的,用的是高斯赛德乐迭代法做的
, d) q3 } D* i0 O& k在算三个未知量的时是和答案一致的,四个未知量就有出入,做六个未知量就出错了.
# P5 k1 @) H: |) e/ Y谁能告诉我错在哪里了,是题错还是程序错了.
2 D. _& W, _/ k# h) N& w- N刚开始学,不足之处请指教。
7 Q8 r0 L+ t$ }7 Y* W#include
6 u. g" S# E1 E0 @# }7 v#include L u9 \( l) i# o! `/ E/ L1 C
#define NN 50
$ a% t. Q, z; T1 f! ]6 f void main()* W/ K7 m: K9 p& u" u9 h2 S
{ $ c# s+ e; V' M
cout.setf(ios::fixed,ios::floatfield);, q7 z# k& k1 P
cout<( v$ {& J9 q3 ~, c- n, V int i,j,k;* C# ]( [+ H4 t/ Z8 ~- H$ x
double e,c,T,r;
: o# C6 X. h5 K$ f //double x[NN],A[NN][NN],B[NN];
) M/ l* a$ S3 F1 x* L //题目如下:任选一个
5 E0 [; y; e: F9 X' ]; Z+ K //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};
2 k; e' u- N. Q8 P 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};
$ V& J$ N$ V& | ~/ @ //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};% T# \1 T7 z& I5 F8 R3 z9 G6 V% [
//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};8 U Z. W4 ^( q: \; k& |9 U
//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};8 ]- m+ L. { u" 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};( X2 X* H' h* B' ^1 M; t) v
int N;
4 B+ a& g3 O$ p cout<<"请输入N值:";1 O0 o; m Q; y+ F
cin>>N;
' u! L1 M! @' R0 \: Y G cout<1 [; h9 i9 \5 E! p
cin>>e;2 C' G: q- R- Y9 t% E. f3 ^
/*cout<! s9 \; Q# P7 W. C for(i=1;i<=N;i++)* Y" q) H3 \) u: I& N/ G- ~
for(j=1;j<=N;j++)
" ~( z' ^: s" Q; G {
, w4 f! }; k" p$ y- c1 R cin>>A[j];
' K% ?! i7 k1 t/ L% G- o }
% ?" ?% \7 T4 c6 ] i/ B9 K cout<4 h2 U( H" g( @2 C; z% c { for(i=1;i<=N;i++)
. c7 P: n/ X% E& o {
3 \! q% ^/ R; d2 {) Z8 A for(j=1;j<=N;j++)
; Z) ^" H1 I. ?, q7 q7 u% J {
U2 E3 ~, P0 i& c cout<[j]<<' ';) z) W$ X; B( w- c
- Q: t. N3 {( F4 o8 _2 i }! h5 u, p+ w) M' F2 [1 b4 O0 C' A" r
cout<1 U5 q; O' z% g" d3 _ W1 H. G. G: r }
; `6 l' J) i' H/ M" h; f. B1 e# ` cout<4 e& t9 p( f0 x3 [ for(i=1;i<=N;i++)cin>>B;
0 B3 @% @. B; c8 K& J. u* x for(i=1;i<=N;i++)cout<<<' ';
( c, v7 J' {; [4 w: @1 V" F' B, B cout<. u9 |: b' ^ T
for(i=1;i<=N;i++)cin>>x;*/# e2 L2 B1 P1 P
' Y2 C z' X& p) ^" c$ g* [* D$ B //cout<<"k"<' S2 e& l0 W! [# `# x4 [ {, r //for(i=1;i<=N;i++)cout<<"x["<& t; r& ?+ ]- k( N. K- g r=e;7 M0 B b, ?% S1 I, g# W, ~
for(k=1;r>=e;k++)
" x# R1 g0 |7 }, B {
# k& G! P: a* L* M& r r=0;
. G p- A! m# m. r i=1;
8 Z. a3 O6 z. H for(i=1;i<=N;i++)
: s3 y/ Z8 O5 Y/ f( m {$ |1 E5 N: C% Y! [' G; ?0 d
T=x;
) N! n6 {" z! o* A c=0;5 O! ?2 [4 K, g
for(j=1;j<=N;j++)
) p) }4 J2 Y1 T$ R2 B& t* H {
; \) R$ [4 D! N; R" X$ d& o, A if(j!=i)c+=(A[j]*x[j]);) `+ w8 K3 c3 C. ?% P" r {
cout<<"第"<, C6 ]1 r0 @, d" {8 q% E5 G0 d" o }* v, X+ R* v7 _# l8 K; v3 i
//c1=0;. q4 x0 _% g$ L
//for(j=1;j<=i-1;j++)c1+=(A[j]*x[j]);$ q+ ]; j* b( ~3 C
x=(B-c)/A;- H$ S% _, |, \6 n: W
cout<<"第"<</ E: ~ m3 o8 K$ n) W
if((fabs(x-T))>r)r=fabs(x-T);: D1 H/ z8 @5 `) J: {: s' A5 w
cout<<"第"<' @3 C6 L- v( x; q7 ]4 V
}
) q7 L9 H( ~$ o1 B cout<1 r4 F6 e3 p; j5 _' Y //cout<<"k="<+ Q# m9 B/ L5 O ; e- e9 R' f' h3 w* h# z
//cout.setf(ios::fixed,ios::floatfield);, [; o# P2 K& I: M( x
for(i=1;i<=N;i++)cout<<"k="<<<' ';//<+ i+ o$ C) {, ^$ } ) h! G4 g! A' c0 N# w
}cout<9 Z9 L3 A) e: _" s$ O3 P, E , I, {/ D! ?0 h; Z: [* k( j; s* q
} |
|