|
程序如下,是用来解线性方程的,用的是高斯赛德乐迭代法做的
) m! L) W: K% z5 d+ y在算三个未知量的时是和答案一致的,四个未知量就有出入,做六个未知量就出错了., t# I( o+ ]# t2 M7 v, X1 z) m, u
谁能告诉我错在哪里了,是题错还是程序错了.3 |6 q/ h2 H8 b' y
刚开始学,不足之处请指教。* E+ j Z- @2 k0 Z) G: T9 x8 o% o
#include& Q, O$ v9 @. F2 K" x. T3 \* U
#include$ |8 J; ?# t U0 H
#define NN 50
* Y1 C0 |/ |$ |" Z6 `# M void main()/ `) \3 T% o0 W$ _' O
{
: i) _+ p. C9 U0 I# z( z7 o& P cout.setf(ios::fixed,ios::floatfield);4 Z% s( B0 P- G. a
cout<- ?1 c) ?7 k: `) s
int i,j,k;
3 d$ O2 |4 I* p5 s, h( t/ q double e,c,T,r;! _# y+ ~# ]) d4 K
//double x[NN],A[NN][NN],B[NN];
7 d" f3 @) A% w //题目如下:任选一个
! i P% r% D! d+ F: _ //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};; v9 t+ U/ A& W' G
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};
' D. r: Z, r6 x: C1 e9 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}; {4 D' A4 k( d1 m, u+ |3 k7 l: g
//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};( m% ]3 ]9 i9 y' [8 h
//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};- P8 i9 f' T1 m2 t
//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};
$ q! d1 `. U! n int N;. V& I- S/ ^1 V C3 d
cout<<"请输入N值:";, o6 q+ H' P( x( w( ^$ e- x2 b
cin>>N;
9 w. e$ m8 m! X% t; D2 D8 d3 U cout<8 p! b, C4 ~2 [
cin>>e;
% B9 Y5 `1 @' c) n7 J3 \ /*cout< S7 @( L! j7 _' }# Q. k for(i=1;i<=N;i++)1 ]* Q' O' Z- P+ F: G
for(j=1;j<=N;j++)- @1 u! w o3 Y, r/ s
{( ?# a# ]! ? m" A5 @
cin>>A[j];
" E5 e& @* m# c }7 f: j$ M4 Z% ?
cout<( V% p: F- L: i4 x
for(i=1;i<=N;i++)
1 C9 _$ c1 H9 M- @% f. L$ D {
2 u9 F5 x$ n0 k+ r& P for(j=1;j<=N;j++)% r3 I# ^9 K* L3 L/ l" r( a) n
{* a( O/ ]" V- I
cout<[j]<<' '; E8 W7 }: h( {
& `( l8 S9 d( A' @. s1 q* X }% _ x- O. s/ ?! G4 k
cout<9 A% ?$ b9 n0 F8 a6 \ }) u8 b8 E( Z/ K
cout<' E( r( m! u X3 m+ u, R5 j for(i=1;i<=N;i++)cin>>B;8 c3 f" L' T7 D
for(i=1;i<=N;i++)cout<<<' ';9 D9 c' F" z5 U0 y% [7 q) u
cout<$ Z/ _2 }$ V: Y: }' e
for(i=1;i<=N;i++)cin>>x;*/9 k$ o0 e9 b1 F
% k7 J+ w+ G; _) Y: [6 Z2 | //cout<<"k"<8 k1 U, J8 p" P* y7 Q9 p! j9 j5 Z //for(i=1;i<=N;i++)cout<<"x["<! g, g6 I/ s4 w/ b: t0 Q
r=e;
: o( w! Q% `5 B8 J. k. j for(k=1;r>=e;k++); X. b9 D+ C8 {8 z; z9 z
{ % `; |9 M0 q0 [4 e1 R# S
r=0;
. v9 {# O& |, W0 @9 f6 _- m i=1;
/ @2 x s/ w9 N z! w, j- o for(i=1;i<=N;i++)6 @% q& |* @' P$ |- F# S7 y
{" O6 x3 K0 t7 r! [
T=x;5 ?2 _$ o" S8 M. b0 @% }
c=0;
, p+ S* z1 ]& x% {. d3 ~( Q, y1 y for(j=1;j<=N;j++)* G5 r# k0 P5 I
{
- u, J5 h; }$ t$ h if(j!=i)c+=(A[j]*x[j]);
2 V$ X; B" m3 w& J cout<<"第"<4 W' Z0 Q7 B4 W, {. ]4 F }2 S5 t- ^6 g( [8 \4 k
//c1=0;; s' o5 H8 Y5 o
//for(j=1;j<=i-1;j++)c1+=(A[j]*x[j]);
3 D; z, X+ {8 }/ p" j2 V. d x=(B-c)/A;
* u4 }7 f- O9 z. U/ S+ W! K cout<<"第"<<" E& y) U$ u2 A8 N+ T
if((fabs(x-T))>r)r=fabs(x-T);/ T# p( v/ x/ Y' a
cout<<"第"<9 }. `3 j0 T. Y }
# k) {; a# J2 y1 I7 V1 Y cout<6 ]% Q% b: E `- c9 @* W2 w& C8 Y //cout<<"k="<8 h: |# [% O# T( G0 |; ~" k
! w- X$ G. Z7 J! F4 l( T
//cout.setf(ios::fixed,ios::floatfield);; A6 K; [: g7 X9 Q/ ~( a- |
for(i=1;i<=N;i++)cout<<"k="<<<' ';//<' S+ t! a3 Q- _" e6 K8 u
- c5 F5 R' f& o. R1 {& u6 ? }cout<" N3 c, t: k$ A" s5 [4 O4 G
3 r% g9 d1 Y, z+ R! y1 F( C7 `
} |
|