|
程序如下,是用来解线性方程的,用的是高斯赛德乐迭代法做的
8 E# \- ]" C8 o. P在算三个未知量的时是和答案一致的,四个未知量就有出入,做六个未知量就出错了." l6 `. D: ?6 ^5 O8 p. h
谁能告诉我错在哪里了,是题错还是程序错了.; O8 m" I. l; ~ U) j- ?
刚开始学,不足之处请指教。8 O: H- Q; p, O4 l
#include# `+ u- e; C9 K# b! V0 ^
#include) ~; Z4 y, e& f4 W
#define NN 50 6 Y" J, {9 e2 D+ ~3 Y. Q
void main()
7 B8 S7 V$ o7 m/ ^1 d, a$ B {
3 n4 d8 H4 Z- N0 k! h" {3 q cout.setf(ios::fixed,ios::floatfield);
, c+ B" L. q; i* _* C cout<9 n( s7 k$ ]; a/ @ int i,j,k;7 l9 @ p& @/ s% g6 [: D) j& T% {
double e,c,T,r;
" @- a- z' y! U. e F) Y+ }$ |* I/ i //double x[NN],A[NN][NN],B[NN];
0 q7 Q* M, ~+ k! F8 e4 z1 |+ _ //题目如下:任选一个
( K4 F' U2 a# w+ l: i# | //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};% b# }" v* C) T4 b
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};
1 v4 O- ]1 d& z2 `' N //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};
# O1 S4 i5 W: Q; c2 Q$ C; ` //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};" |& n, S& u, l8 U6 g
//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 B* }+ y* ~ V% b/ H3 e
//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};
5 U6 w# S; n Z; T& `/ m/ Z int N;2 M R5 V4 D( z3 Y# O" H
cout<<"请输入N值:";4 s7 f/ k/ X) Q# ]* S
cin>>N;
& Z; o! Q, L2 K$ m$ M cout<. j# g ` |1 V: i
cin>>e;% J* ?# h& `! }# B
/*cout<' ]9 A6 V% V! J0 d for(i=1;i<=N;i++)
$ X% H r, E4 a1 l; X" v for(j=1;j<=N;j++)
* M& n2 k" G! B- `) U" n- _* U {' a' S d& J, ^
cin>>A[j];
" I* e3 {( o5 E3 E! U }
/ c+ t% `+ R* E8 f# I cout<5 U9 E/ C2 c/ }9 ]! P- O* |# ^
for(i=1;i<=N;i++)
/ `5 A, Y/ j+ C; L5 j4 F! ^ {
1 y; t/ Q# s& x4 V& k4 x3 i for(j=1;j<=N;j++)& u2 r0 t+ f7 X& L
{( q4 `$ w0 x) ~1 j
cout<[j]<<' ';0 ~' Q9 s" [) [7 Y: | v+ z" \
, Y! Y4 n: F3 N% G# O }5 L- G. ? m) C5 B
cout<; v# B/ \& Q1 v' V3 H% `
}( o; i, }) Y0 \3 _& C5 I1 @2 U) x
cout<! A5 c1 z; Y9 W( m" _+ X N for(i=1;i<=N;i++)cin>>B;
2 n# b/ y+ L2 D4 ~* Y for(i=1;i<=N;i++)cout<<<' ';* c7 _9 c5 w$ j& W
cout<, @+ |; B; D9 }( ?7 t for(i=1;i<=N;i++)cin>>x;*/
8 I8 h4 B4 R3 O9 G& v: y1 l8 s1 _& j+ w+ _ y0 t, K) m
//cout<<"k"<) d* d1 o" m( W
//for(i=1;i<=N;i++)cout<<"x["<& R$ r* U2 R! j
r=e;
: Z9 U6 F1 R7 k for(k=1;r>=e;k++)8 j$ q9 D9 f' c9 ` H
{
/ \" d7 b0 }2 {# ^% g r=0;
- D9 m( x9 b. g4 c i=1;
) R1 L) ?. K+ O; k t" } for(i=1;i<=N;i++) @# Z) p5 m/ [1 v! T# U) D- w
{3 Z! T' w& J4 S3 b& l
T=x;
6 W6 ?4 I+ j! C, \ ]7 @ c=0;* i, f5 B1 g1 X5 E# M
for(j=1;j<=N;j++)
! P+ ^& T0 H; p/ u" a {
$ j& p- V5 c& j9 |+ j if(j!=i)c+=(A[j]*x[j]);
2 R5 `' q0 j" }3 V9 o; i8 @% t cout<<"第"<( |5 B2 P% g- R* o
}
0 `# i/ i; L5 m' w2 F" }. b //c1=0;/ {* \$ K0 }2 K0 e. x6 D
//for(j=1;j<=i-1;j++)c1+=(A[j]*x[j]);
& ^" w9 `2 n* ~# U r" G: m9 _/ _ x=(B-c)/A;$ t- P. s6 W0 z* l6 f/ a7 R
cout<<"第"<<) h4 I0 I8 ]) Y( m: B8 [" p if((fabs(x-T))>r)r=fabs(x-T);+ m; e& l0 R* j4 G& R
cout<<"第"<3 w2 m `: J! R6 L6 R
}
% J$ u+ Q! _/ w ? cout<# g4 {$ S( g& ~+ q
//cout<<"k="<' Q! d) a5 T8 ~# p& g+ K8 w: V" {
/ }6 z3 t$ J4 s. T$ Y0 m7 D$ R' j
//cout.setf(ios::fixed,ios::floatfield);
7 l" G: R% {$ f7 \) N) C for(i=1;i<=N;i++)cout<<"k="<<<' ';//<( B: J0 i. k5 R4 F
8 ~) p, o7 j( C4 b. A }cout<3 l: c+ J2 b3 I* E. a3 q
8 y4 E% O1 X! s} |
|