|
程序如下,是用来解线性方程的,用的是高斯赛德乐迭代法做的
$ L' |" a( j B/ d X1 s4 @在算三个未知量的时是和答案一致的,四个未知量就有出入,做六个未知量就出错了.
+ @; p* O7 R, N; K谁能告诉我错在哪里了,是题错还是程序错了.- z* S3 s# ^5 i% @
刚开始学,不足之处请指教。
! |+ g% B8 U, Y) @4 Q8 w#include& L6 e' z1 y9 A1 z
#include
2 F p7 o/ E* L2 I#define NN 50
* j6 |- K% ^8 [. ~ void main()
1 M3 B/ \% |3 c" { {
* f+ m3 k" a/ e* r cout.setf(ios::fixed,ios::floatfield);
, T/ M* Y+ G8 U2 L, p cout</ F1 R4 l) ?" s
int i,j,k;- p* j! }( M5 ?4 t3 z
double e,c,T,r;1 T6 t: x6 [$ |
//double x[NN],A[NN][NN],B[NN];
/ [ G _9 K. g* k //题目如下:任选一个' a: V" D# w1 p! e' K* R
//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};; j6 v9 o& ?9 k$ ~
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};) s& ]; I; V0 i* |; X
//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};
& W2 v H5 m5 B2 p$ Y& } //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};
; o- X4 o- L, B+ H: \) 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};
2 i: |% T7 b$ ^# N; ` //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};
4 Z( h7 v4 ^$ M \" r, g H int N;
2 X5 G( f1 [2 h1 z' d, p& |% p cout<<"请输入N值:";
) m& C3 y8 J" c4 |2 m* g& S# x4 _ cin>>N;
' A+ s$ _/ |" e8 {0 R cout<* d: \' }+ h3 `+ _; @9 n
cin>>e;+ H0 B6 V7 x+ i. Q+ H; n; l
/*cout<5 X% l! G$ U2 M# B- E: _7 i for(i=1;i<=N;i++)$ J9 {; v9 o0 I( E% B
for(j=1;j<=N;j++)8 G7 G" O }: {5 S/ T' r: f
{
* \8 W' q+ {& p; D# ? cin>>A[j];! H% A) j" J, l' D) Z1 b2 P
}0 R3 w+ R2 z; _3 g8 ]
cout<0 C/ `4 c) o9 d) `" r$ a for(i=1;i<=N;i++)
- I m" Z, E b q# \$ A {
& s/ ^' q- K- ^( ^4 W for(j=1;j<=N;j++)$ g( o$ V+ g1 j
{
) X/ C- q5 N7 G' Z U8 q cout<[j]<<' ';, \0 @1 r0 `* ~( {
: }; G0 D4 e4 O6 C: `$ V }# Z$ c! g. W) G8 R
cout<9 v* U9 _% B; R' f$ W& q }: ]# R& A. X1 C0 n% J8 H$ P! P/ e% d
cout<' j& s8 q" e1 n3 v2 T. H$ T: E! \
for(i=1;i<=N;i++)cin>>B;2 D+ Z- l( h5 n- y
for(i=1;i<=N;i++)cout<<<' ';
! }6 T1 i9 \+ t' G: G cout<2 x, i" \/ E3 y8 H+ p- i for(i=1;i<=N;i++)cin>>x;*/& Z0 q) D7 v, |
6 P) |9 `4 W9 U e; ]. v //cout<<"k"<# {. d! n+ f, E Z6 F
//for(i=1;i<=N;i++)cout<<"x["<" r" V* O, K, e9 q: B8 z! g r=e;
1 x8 j6 C) M6 W% b/ Q5 G6 x4 d for(k=1;r>=e;k++)1 R, o: Y* G0 N! P: B% ?+ u: y/ ^
{
+ X3 l6 p8 }$ k& E a r=0;
( ]$ k5 m; n9 r! W5 P i=1;4 i# F% w0 C3 {
for(i=1;i<=N;i++)
( Z; d5 l! j% B4 Q1 F/ T9 k {
0 {, i+ l$ w0 C7 q7 [# U T=x;1 u! x% r' ~- ?, Y
c=0;
/ R: s; M' V6 g# J# [& E# N for(j=1;j<=N;j++)
2 E8 G$ ?7 p6 Q# Y' O {5 v8 q* m3 U8 o6 E& s/ _, B9 o. T
if(j!=i)c+=(A[j]*x[j]);5 m+ Q% Q/ O* {+ m
cout<<"第"<7 ~1 Q4 [% ~7 d& ?
}
) n7 P0 x7 x) D' x& {7 l //c1=0;
: I3 |) G% A- o( I* r //for(j=1;j<=i-1;j++)c1+=(A[j]*x[j]);. k5 ~# s1 T, T P$ q
x=(B-c)/A;% v, h+ w' v+ v! B
cout<<"第"<<: n" C! H4 ^8 J if((fabs(x-T))>r)r=fabs(x-T);
3 Z5 y+ f6 t! b# W l4 e; x cout<<"第"<1 w5 _5 M4 N2 C }
1 N6 E8 t$ w a+ B. n cout<7 s5 f, t: G" ?2 O/ Z //cout<<"k="<2 r; n" c& n( u* i5 ?
4 ~8 p+ a" t' l' n! Y `4 s
//cout.setf(ios::fixed,ios::floatfield);+ r* h, V% Z1 z- l) a
for(i=1;i<=N;i++)cout<<"k="<<<' ';//<" G& Q8 {3 S W! b
7 s- e. _& O$ j" |! a0 l' w' r1 T5 \8 v
}cout<6 _+ b7 i! t1 y- a0 v
! M. L5 h, n& H) i7 {4 @- D B
} |
|