|
程序如下,是用来解线性方程的,用的是高斯赛德乐迭代法做的
: B: y) |# u. F) P在算三个未知量的时是和答案一致的,四个未知量就有出入,做六个未知量就出错了.
( x! a: N% t0 F; Z谁能告诉我错在哪里了,是题错还是程序错了.
$ K) Q# n. w/ s1 B; B刚开始学,不足之处请指教。
+ N9 k" l+ l& q3 I% J#include
( d% i# l/ }7 R* P X5 Q, R#include+ G2 V; O( E# h
#define NN 50 5 v) z( x$ u, U9 U3 k* n( r- {
void main()
Y, z8 E! m$ z$ ` { 9 L) ^. h# h% j' n* `) K
cout.setf(ios::fixed,ios::floatfield);
) l* O( ^- h: o' [& X1 A cout<* U' @; m8 K6 \0 D$ Z7 y# [
int i,j,k;$ L8 \; V1 u- h: C5 A! S
double e,c,T,r;
8 I/ X# k( U K- k0 T, ?3 i$ m/ V+ } //double x[NN],A[NN][NN],B[NN];
1 D' u& g& g: i7 I1 z" I //题目如下:任选一个1 [" H5 A( S/ v1 J4 X0 ^ s
//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};
! F$ M3 k9 s* |& W 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};
6 v$ x q' }) U: M% F6 |: m9 g //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};
$ r" p) n/ z% L5 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};9 Y0 u5 O9 m3 f& H, O. s+ m
//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};
5 X$ @4 f- O A& {# 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};* Y* x* A1 A+ P' T8 N2 {
int N;; `5 F2 i) S7 |8 X) \: C+ A
cout<<"请输入N值:";- z" s1 b! r; j9 P
cin>>N;
& ^, g* _8 g' e cout<! p2 _ a! ~$ f* k9 `( @' C1 @ cin>>e;; n' _1 F4 e, O. l. [1 `: n
/*cout<* E/ {- R7 N. W z1 p8 N
for(i=1;i<=N;i++)
; R% u( {' b0 p# j for(j=1;j<=N;j++)
% ~: L8 C# x* V, E- R" h. R {8 C% a# M9 y. F4 ~8 I! z( Q: ?
cin>>A[j];
, B& Z3 C( B2 I; B% }+ y' L# p }6 f- n, j$ a U7 j; Y2 y% _
cout<4 H8 q3 ]8 Y1 T x# b
for(i=1;i<=N;i++)
+ Y o$ \" W+ A0 b% R0 U A5 p Z$ v {# F8 y; ~. a- d# E: C
for(j=1;j<=N;j++)4 n! o! i) n& J- S
{: m' H0 V4 {5 x3 f$ Z; S. _' U( K
cout<[j]<<' ';
( i P W0 {1 V 2 Q$ W ?, r+ |+ S+ t% T8 z
}
9 e6 ^4 y; B. x5 M cout<' |: k, D0 o+ e( F
}6 e; c4 J3 c( v! Z( B! C
cout<2 L- g. _# Q( O2 u for(i=1;i<=N;i++)cin>>B;' Q9 R3 `% g7 l8 {- @
for(i=1;i<=N;i++)cout<<<' ';7 N' W# q6 N. |5 v
cout<! p, |; x0 u" K% ~) c3 E
for(i=1;i<=N;i++)cin>>x;*/
, G' |- L( `4 d+ f) n1 M8 \) K" W9 m7 Q# v& c' x+ m
//cout<<"k"< @/ y4 w: ~& q" @" v3 N
//for(i=1;i<=N;i++)cout<<"x["<3 l$ h# Z. L* }) W
r=e;* r* e6 r- N9 k" c- O& x
for(k=1;r>=e;k++)
- A4 k2 H5 o; r( W6 L {
' G( j6 i" k: w O9 i7 T7 K ] r=0;. q+ [ C3 ?3 R( O
i=1;
* w2 s4 h5 {; F( S3 q( F for(i=1;i<=N;i++)
: Z7 c; c6 a7 X" n {
5 k, Q" \2 b5 S# T) h0 z T=x;
& U# C0 i, e) B) _% \5 D& k c=0;+ f* t2 ^7 _3 d' H4 ~2 x8 Q3 h3 D
for(j=1;j<=N;j++)
6 X* l% O4 t7 s& M E! i$ y( ?( X {
5 v3 T1 p( e0 w) i% n$ D" @ if(j!=i)c+=(A[j]*x[j]);4 O" c- _4 R* o
cout<<"第"<" l) A/ ^* Q" Y" a }9 d% h- c! g6 C% O2 l
//c1=0;
- X, e+ | c: O4 B3 u3 H //for(j=1;j<=i-1;j++)c1+=(A[j]*x[j]);
) { U$ D/ y0 _) z' x$ X* M x=(B-c)/A;
# {3 U4 |3 w; H/ n# w4 L4 `5 ? cout<<"第"<<3 q1 |- [! J ?7 G3 T
if((fabs(x-T))>r)r=fabs(x-T);
, C! y4 b; K0 z. ^: T cout<<"第"<4 `' f0 W) Q" h1 D c: z: `
}7 P9 y- X; [5 c. w* `$ v
cout<: G: S# \3 U7 x" x! P: ~ //cout<<"k="<8 O& b) M4 v5 P* v$ p
8 P; T) X5 I+ O" B, @$ A9 F: E //cout.setf(ios::fixed,ios::floatfield);2 Q) R P6 u- h
for(i=1;i<=N;i++)cout<<"k="<<<' ';//<) ^' k6 y1 K8 x/ f2 E' u
- S$ B' U- i- c# n8 p }cout<5 W7 f$ P: }9 v9 V9 K( F7 `
, K. s0 x. ^6 x; g7 q- L4 h5 ~} |
|