|
程序如下,是用来解线性方程的,用的是高斯赛德乐迭代法做的. c6 j* s7 [. M$ `3 q
在算三个未知量的时是和答案一致的,四个未知量就有出入,做六个未知量就出错了. M& v7 g0 }: U/ q; z0 r' H6 A8 a
谁能告诉我错在哪里了,是题错还是程序错了.+ e/ g( P" t9 Q7 b; N
刚开始学,不足之处请指教。
5 H' J; q& `% T0 W#include2 i' Q: u$ M& r. k. z! ?4 |
#include
8 G( J2 }! J/ `' E; V# P#define NN 50
0 {- d( I, @* ]2 f. w void main()
6 n; [! x! o! D; g# |, y { % S; ?& P. ?# [( v/ b3 m
cout.setf(ios::fixed,ios::floatfield);1 R- f1 C% y8 f5 f3 w2 x- J3 |
cout<% w5 f9 S p' f% b. @
int i,j,k;
) z! `' b! B. n- T) i double e,c,T,r;' l, ~. S$ @6 J( W4 G6 G5 B
//double x[NN],A[NN][NN],B[NN];
% X" e% C4 _( k7 Z( D0 c" t/ ` //题目如下:任选一个0 V0 d) ~, [2 Q9 D" \8 H' n/ e
//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};
" z$ _1 ~) q i$ I2 o 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};' f' ~% k, s: R, u/ T
//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* L u7 R- q3 w
//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};) A5 m4 G* _1 X6 J5 D o
//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};
' u& H6 `6 e# a5 Y, N$ g //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};
% Z; a$ O8 ?2 {& F int N;
5 x6 d T; h1 h cout<<"请输入N值:";. I% H" I7 t- r# \( h
cin>>N; z+ b- g1 D/ B# L* P* [
cout<" G7 A& R- t4 P \ W! e! B cin>>e;
- [9 X# q& T0 c /*cout<. t4 |3 ?3 P8 s for(i=1;i<=N;i++)
" n+ a/ F6 E: H$ H) p' |* u( C for(j=1;j<=N;j++)) I1 L- v% l# U: K5 U" c
{
$ \4 E2 ^. U( g" U, `- [1 t cin>>A[j];$ }- Y) c; b+ f! s6 @# v' B" c
}
. L( f* E& W9 I/ c" H( G' @7 Z# R cout<2 g% U1 w H9 z, x) |2 ^# Z for(i=1;i<=N;i++)% \1 Q* t7 M4 c) b5 e* c
{
4 m% q; ~) z* Y4 M for(j=1;j<=N;j++)4 U2 F4 E4 g# R f5 g$ w/ r! }! O
{ ?$ L7 I& H( |) t
cout<[j]<<' ';
6 m1 `5 B4 V8 [ h" e: r 1 r1 s; E& e" V8 o" S: M, R; X$ S
}
) i5 k, a, a6 k, }0 V) k) N9 Y( U# p cout<" h, I& Y, L: y& n2 G3 H: H) M' X
}
- k2 X8 P1 U5 z, h' x cout<! o4 l* w2 d9 v/ f for(i=1;i<=N;i++)cin>>B;3 ]7 z3 y/ T: L' K5 e9 S6 l
for(i=1;i<=N;i++)cout<<<' ';
$ x$ {. m) a' P ^7 l# k! w/ r cout<4 p/ R6 ?4 |8 x$ U( {% N for(i=1;i<=N;i++)cin>>x;*/
' J% K. f5 r" w( G, u
7 k3 W) h) ~5 x5 D. i8 S4 e //cout<<"k"<9 N% J9 S+ p v' F9 W
//for(i=1;i<=N;i++)cout<<"x["<* ^/ m9 x3 y$ J% r9 ]$ x r=e;0 k9 P- f* M6 S" m
for(k=1;r>=e;k++)
- ?+ D5 x7 ^% N { O( G6 {. i, f
r=0;0 Y, n8 c+ s- S
i=1;) ~' b6 N8 P8 q4 U0 l/ Z4 l! I5 L+ ?
for(i=1;i<=N;i++)9 C Q# E3 z' ~6 Z" Q5 A$ c, `
{3 B- Y2 a, i( F6 p' O0 T! G
T=x;, \! m, ]8 o. L1 f: a
c=0;
! h4 M v$ _1 y4 x, C/ `; m for(j=1;j<=N;j++)) t5 N% e# q" X6 E' T
{
+ F( V# o' V/ Q0 h6 _3 H, A if(j!=i)c+=(A[j]*x[j]);
# A% v2 v- P6 D6 p7 c0 \' U2 @ cout<<"第"<7 ^2 Z7 y1 F) k, B& N }
2 I) h$ r& a, |" H; F3 ] //c1=0;
* P( k8 O6 I" ?2 D5 v //for(j=1;j<=i-1;j++)c1+=(A[j]*x[j]);6 I3 G" B+ e( d: R+ s+ H
x=(B-c)/A;) N/ k2 a+ E/ n+ y+ M7 j) ^8 [. I
cout<<"第"<<9 |0 r, ~4 K) C; D" I A, _# Y: u
if((fabs(x-T))>r)r=fabs(x-T);% g! }: O. h- {7 v. e1 o3 j
cout<<"第"<2 [& F" A+ u" a" {: \
}( O, r# J) n. [+ ]8 x7 n
cout< p# U: M, B! d0 b8 K3 J
//cout<<"k="<1 S8 |: z7 \: U1 _5 R3 D& c 8 }8 r1 `4 |+ ~$ W) S2 C' G
//cout.setf(ios::fixed,ios::floatfield);* r2 \# q0 g" E4 R
for(i=1;i<=N;i++)cout<<"k="<<<' ';//<# |' i" C5 i! r7 u5 e' Z2 a
- Q3 b' U: ~# u: h0 s
}cout<$ V+ n4 l' C6 Y/ }) i' g) u 1 U3 s: X5 M* T* x$ `# u5 Y
} |
|