|
程序如下,是用来解线性方程的,用的是高斯赛德乐迭代法做的 d3 \& x5 d9 e; F( q
在算三个未知量的时是和答案一致的,四个未知量就有出入,做六个未知量就出错了.
5 v5 A# Z7 \1 n9 r谁能告诉我错在哪里了,是题错还是程序错了.
! j. r% `1 q, W# e' E' M+ b刚开始学,不足之处请指教。$ n5 Z7 G2 ^4 r) F4 m2 b' b$ k6 k
#include& t, \+ L/ h# e; _9 {
#include
5 D# v. P& C& G- f& W, E7 A0 Q#define NN 50
$ L. }* C* n" N- O2 X0 m3 O void main()
, j: \7 @# j" P, _ { _- g& N3 b/ T! C
cout.setf(ios::fixed,ios::floatfield);
% N. g$ X+ ~( d7 t+ ]# s cout<! ~ W \+ F% M9 o& }: b e6 [
int i,j,k;& G3 x2 e; i8 }! ]
double e,c,T,r;$ F; h. y5 Y' D3 M" M( s5 [) D
//double x[NN],A[NN][NN],B[NN];
# F' z3 q; V H" S' j$ c //题目如下:任选一个, |; Q& ]& S' W( R4 P* S3 H
//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};6 f, R7 V* C1 c& p% m
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};
% B5 J5 m, o& j: E //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}; @0 ]! ^+ \' D+ [5 l* |
//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+ n4 v9 d8 W4 p5 s; r0 B //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};. D- o% c$ p) s9 E7 w' 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};6 f3 A& j( _$ f3 K0 _
int N;
' l7 y% C" Y% \9 P6 | cout<<"请输入N值:";
% c3 E% y3 y. T9 I" S& q$ J6 O cin>>N;
$ d% y3 R% t% i% _ cout<( ]) P$ a) l, M& J& e cin>>e;
# [! J; \1 [* G( u% T4 i3 h /*cout<8 R2 K/ J% ^- Q* J$ }9 S8 |5 B for(i=1;i<=N;i++)# K; V8 w- r) z/ _# L4 _. J
for(j=1;j<=N;j++)9 f- J+ [( B( u: R
{4 `/ s: f& V5 F% ]2 H! e
cin>>A[j];6 F8 ^; w* Z% q! \
}
$ n& N7 N* t0 K8 c cout<8 ~2 [" `+ t; Q6 w7 B4 T for(i=1;i<=N;i++)
* Y% w+ k2 w: @ u" L {9 M- O9 {' H- Q& _- T8 S
for(j=1;j<=N;j++)
9 D, Z# \0 n* q4 h {5 _) S! I( L3 e/ w1 b4 Y# y
cout<[j]<<' ';+ o; u/ l# R Y+ d
& C. V# B2 S. x7 p3 u* K! s }( A+ z- A& `) \3 f7 S/ ~
cout<. X: ]; t% ]' E0 r( T `! Z } v, p e& f* S5 B \( U
cout<, V0 R, B, v2 d) S5 D( a) }% u- s for(i=1;i<=N;i++)cin>>B;& T" F' i0 Y0 w. c5 j" m$ q) l
for(i=1;i<=N;i++)cout<<<' ';
* S" j1 d# _+ ~6 Y8 b8 p cout<7 V) o0 G& u) Y3 S4 b6 i for(i=1;i<=N;i++)cin>>x;*/
+ [9 A8 c* |6 [- b# R5 |( u
2 z4 Z/ T- i0 B; N2 U //cout<<"k"<0 F' j! X: [; ~. k6 u, A //for(i=1;i<=N;i++)cout<<"x["<# c; M% N# S; w! w* s2 w r=e;2 M3 `8 V, u5 G8 d& K- m
for(k=1;r>=e;k++): Y$ w7 q4 G* ?( W9 g
{ 5 y7 n) j* ]* E5 _& `
r=0;
% e9 [% V5 O+ Z1 q6 ]8 ^: w i=1;
4 U0 F" s, v7 H0 l9 \+ T for(i=1;i<=N;i++) Q3 {3 ~% |1 g/ q1 _: p3 V P6 F
{
6 o3 ?8 B1 E7 w0 j- k' k$ ?) e2 F+ B T=x;: @" C3 d. M3 }" J2 b/ w
c=0;
/ W8 G- e O2 r0 t, O& V for(j=1;j<=N;j++)7 F D6 o4 b+ P' [ C
{
# q+ Z; ~. M: l& e! u! q6 }% Q* P- g if(j!=i)c+=(A[j]*x[j]);
1 M/ f2 `% ]- u0 s( [" m cout<<"第"<9 [4 D4 q7 \7 R( K) L! A
}
1 A; l5 D" A9 `* S //c1=0;5 p& P3 _, o8 B9 [$ ?
//for(j=1;j<=i-1;j++)c1+=(A[j]*x[j]);
0 I" [* j8 c; |% h. _" v x=(B-c)/A;
7 x2 u1 \7 d8 t4 q cout<<"第"<<0 D& |' H6 f, R7 @* w: H Q if((fabs(x-T))>r)r=fabs(x-T);. a3 E7 _2 t/ U4 p- y; j% t
cout<<"第"<* c7 B3 z7 l/ Z' t8 L! e }
% s6 M6 R# M6 ^$ T cout<% E' {' S+ J' A
//cout<<"k="<0 b- M/ _2 X3 j, ^9 ^8 \. J
) x# M! d4 s2 M( U: K+ [0 W //cout.setf(ios::fixed,ios::floatfield);/ y& n" I2 V! u% k7 t0 g
for(i=1;i<=N;i++)cout<<"k="<<<' ';//<+ r Y! z9 |7 B: P7 E1 U% [$ Z
( }/ ^; K& o1 d2 L" k8 B7 L: Q1 r
}cout<( ]3 s9 G. v2 h
) O* O5 u% k' S/ N0 P3 L+ H/ v0 S; x
} |
|