|
程序如下,是用来解线性方程的,用的是高斯赛德乐迭代法做的) R: U& N" P# u) ~$ N
在算三个未知量的时是和答案一致的,四个未知量就有出入,做六个未知量就出错了.
3 Z' U: a' P. p. ]# j谁能告诉我错在哪里了,是题错还是程序错了.
% K3 Z7 T' @) S! d$ C$ L) G刚开始学,不足之处请指教。8 C5 a' V/ g- g7 A3 v6 o* N: G; e
#include
( n8 r# ?9 x- P3 L1 P7 u# b#include
5 f( m7 ~/ r3 N#define NN 50 6 L9 ~9 H# I7 A8 j
void main()( @: ~. U* e9 ^0 D4 ]/ M9 G
{
1 l0 m! k" R% r1 u. t1 j# Z cout.setf(ios::fixed,ios::floatfield);
" i: h9 w2 h7 G0 }$ ] cout</ T0 r. _/ }0 f) y
int i,j,k;
) k! o% P1 N$ i. t double e,c,T,r;. h9 F: z; S) V: M
//double x[NN],A[NN][NN],B[NN];, U1 }" g6 t0 g, x2 ?% B( c. c3 y
//题目如下:任选一个 b) a& G6 q$ C; z0 G
//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 v* S1 n. j7 Q3 `3 f, ~ 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};5 ]7 i8 s/ S3 x4 a/ I1 i8 m
//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 k: f" X0 X. E/ F9 G2 Q2 } //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};& ^5 X$ t' `; q/ q6 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};2 S. F3 O( w5 U T6 p$ m% A
//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};* A `$ g" L/ n& q# w( z0 l+ l; Q
int N;
' l2 Y) v# N+ O, C cout<<"请输入N值:";
! E; l6 h7 q% v cin>>N;# s, Q. `# G" i3 e3 `. `" u
cout<" Y' o8 V6 V" ?* T9 e
cin>>e;
$ ]8 B2 b! w% A2 e% p7 ] /*cout<% x: s- m9 ]) Z, z) M
for(i=1;i<=N;i++)
; t) f* u& [( u1 m, w for(j=1;j<=N;j++)9 I& S' b- Y+ K0 N3 Y9 F6 }
{
4 N* s/ e8 f" g: r( @ cin>>A[j];
8 j: W' E& J: B/ N }9 Q% C' T; @- A, f% Q
cout</ b0 v# D9 K9 Y" w8 s6 A& i
for(i=1;i<=N;i++)$ I& x9 F7 |8 k2 M( r& T
{& K( y" o5 Y9 }$ P
for(j=1;j<=N;j++)
( Q3 \! f# j1 z' h( C& [: S {
% {. L8 { t, l8 B5 K1 A2 n1 A cout<[j]<<' ';
5 y& k5 m7 {" Y# J% K' J# a& B * l& `5 d4 y- U
}# p, f8 b4 R ~4 N
cout<' Q8 P7 E; O8 ~ `$ \4 r" J/ P7 m
}& i7 d) t! S( r5 ?) I
cout<: f. I+ f% }3 m- @. x for(i=1;i<=N;i++)cin>>B;
, p$ O' b4 F4 j' ~" K2 @* @ for(i=1;i<=N;i++)cout<<<' ';. ?" o% J, K( \/ S0 {
cout<) L& Q# o3 I" w; x9 W
for(i=1;i<=N;i++)cin>>x;*/
1 m9 U _% b$ N( J" t+ I
* A; \( ?$ z7 t5 D! x$ I: ~- X8 K" z //cout<<"k"<& G& ~% u3 P4 e5 Z //for(i=1;i<=N;i++)cout<<"x["<) d$ {: Z+ ?, K; Q2 r1 C( o
r=e;
) |4 o" R" W7 a9 A for(k=1;r>=e;k++)
) T4 m9 g# h/ ^2 r {
; ^1 h/ V+ l6 `; h( b6 x7 m r=0;
# e# o7 Y8 E, j5 g. u! Y5 ] i=1;
& n. N! p0 R# O: K) K B for(i=1;i<=N;i++)! n8 w% O; M. F' `: v% X0 J
{
% K4 W0 l0 c5 } T=x;( [5 i+ J3 w/ d. b" P4 s( c
c=0;) ^" }5 S h, u7 w5 i. D
for(j=1;j<=N;j++)
$ E* B( ~: ?% E! b$ H {
! {% g+ G% \% T$ b" ~ N if(j!=i)c+=(A[j]*x[j]);* ^5 ^) E1 E4 R2 f
cout<<"第"<# [ d, L$ R' x$ W6 c* N8 ?. Z y8 d, Y
}
5 R9 I: [7 d' T" H* r //c1=0;2 x$ S5 v6 t* Q7 e) d8 `4 L/ Q! E
//for(j=1;j<=i-1;j++)c1+=(A[j]*x[j]);5 G" Q4 a; n0 ?8 Y
x=(B-c)/A;; S7 V. v4 W6 _5 y% o1 w
cout<<"第"<<2 l' O$ w S4 H/ g6 }1 P" | if((fabs(x-T))>r)r=fabs(x-T);
# p4 C% w: h$ W' _9 } J cout<<"第"<6 m# `( P& u- s5 x- d
}
1 R( c2 q* E- h; ?2 ^7 p cout<8 T' M7 }+ v2 u( Q( g8 L" Q8 `
//cout<<"k="<8 d& U3 E; n# L" H3 m# X/ I- K
( S. d8 H F# S( `: ^7 L7 c9 c //cout.setf(ios::fixed,ios::floatfield);: \1 g6 S- F( m8 ^2 h4 j& j
for(i=1;i<=N;i++)cout<<"k="<<<' ';//<1 N5 _+ z" F ~& Y I6 M
7 |" f) \% w1 l9 r# R, D D+ I }cout<. ^; w5 q# p8 U+ k; z& P% C& J& |
m J3 Q" w( I0 F} |
|