|
程序如下,是用来解线性方程的,用的是高斯赛德乐迭代法做的
4 h1 i3 A5 y, d5 [在算三个未知量的时是和答案一致的,四个未知量就有出入,做六个未知量就出错了.
* U4 ^) @. M# N# c, y谁能告诉我错在哪里了,是题错还是程序错了.7 L( w9 l2 G+ {
刚开始学,不足之处请指教。
0 {3 D% L; N$ I) n3 r. f#include
0 U4 |3 \+ z" Z- K#include
' s1 b% |2 B/ t" ?& }0 G#define NN 50 & f1 I, Z, Q* d4 O" n* N! j P
void main()# l" I7 v* B6 W/ c8 \- L* Z3 t
{ - V* E" s% @ U/ F. ?, U' J
cout.setf(ios::fixed,ios::floatfield);7 K. L" O o7 R& b2 r
cout<$ f- M' V9 F" J& @% n
int i,j,k;
: G; E2 T' d! ~0 b/ v9 L2 s double e,c,T,r;: C8 d7 T- {! `
//double x[NN],A[NN][NN],B[NN];
, h; }0 a/ Z+ ~. y //题目如下:任选一个
- r& Z- x5 u" X/ 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};
$ |& ]0 |; A& P P0 B/ B6 [ 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};, I& u2 y y3 r3 k6 F. i3 f
//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};$ t$ s+ C) c; l8 ~: t5 f" 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};
2 z" X$ `" `/ O$ U. w9 j //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};
1 f% d# h! E; V8 r //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};
0 m7 j0 {( L q! W9 B5 Y int N;
/ l% h$ R9 s2 b8 ^0 G7 f9 ~ cout<<"请输入N值:";
! f0 `/ ^5 z: e cin>>N;, G% f( B1 F) [( ]
cout< c1 p3 ~* G0 d8 W# i
cin>>e;
* \, |3 Q p0 e0 ~ /*cout<' ?# R5 `. d& l' Q5 H# r4 W for(i=1;i<=N;i++); A' T5 F+ h# I
for(j=1;j<=N;j++), k0 j/ N: S' W( S3 w& @
{' E' y C8 p) ^/ v1 {
cin>>A[j];& W& }$ l8 [! e$ ], g3 o' j
}1 Q9 g, X; m1 |; J
cout<7 k6 I1 L8 B+ S
for(i=1;i<=N;i++)
6 p# ]+ W9 E: v4 ^9 @ { {8 R4 j a3 c" x
for(j=1;j<=N;j++)
, p) R8 B h' J" L, P {
8 {: V) ^! B0 z cout<[j]<<' ';
; w. f$ ~+ |2 B) H: A) |$ k- u
/ `, Z4 ]$ i% L% c5 }$ M }
, e- H' [4 u. v H% @1 k cout<% d d& H3 f& k6 b; ^; U3 i
}! |. Q2 W2 d2 Q$ T( x
cout<( A2 Y/ D1 X" ?6 ^! u( X* [, ? for(i=1;i<=N;i++)cin>>B;
: F, M! [2 e% d# ]" y4 D6 K0 E for(i=1;i<=N;i++)cout<<<' ';
* g# {9 n8 C3 x [" a t cout<+ I9 X. c9 |# Y; ^( w U- ? for(i=1;i<=N;i++)cin>>x;*/
/ @8 B2 [8 i6 B( i
1 |3 q: l( B, G0 O //cout<<"k"<3 }$ r x4 T' J2 a2 U //for(i=1;i<=N;i++)cout<<"x["<6 G: y F5 ? A& n
r=e;
3 x% t- C9 m, \$ v# R% C1 Q, ] for(k=1;r>=e;k++)
2 C# f, N. ?3 L; C { ( r$ W" Z; \. J. \
r=0;
. U" d. b N9 |' c i=1;9 B8 g/ V: v4 A% w/ J
for(i=1;i<=N;i++)7 h+ Z8 F8 m. @' [0 l- d
{5 ~4 L* X; M9 u: C
T=x;
; L9 C4 D0 s$ B$ ~ c=0;
) ` j1 { W1 M% r, _+ l for(j=1;j<=N;j++)
0 o# K" E+ c* w* k; j0 w, ~ {
7 _. E! ^, c* F1 R if(j!=i)c+=(A[j]*x[j]);! [1 v2 B$ ?% w# b7 d
cout<<"第"<2 m/ w# v; ]; E( Z }
2 y7 [6 w4 P6 I& D* l0 k6 Y //c1=0;* v* @, V2 P4 h9 ~; {3 ]4 @$ }
//for(j=1;j<=i-1;j++)c1+=(A[j]*x[j]);
( H' u& H! f: L% v( Z& v x=(B-c)/A;) Z8 r k& ?! w, R; l, C
cout<<"第"<<! ]8 h! |) }! F" P& k& | if((fabs(x-T))>r)r=fabs(x-T);1 _; w3 V& W( |. i& n9 D
cout<<"第"<$ u! P+ @7 I0 E7 S }
* U9 S& Y& V! \. c9 c1 m9 T) l) x cout<9 H- l: x5 B3 o* f6 c: w
//cout<<"k="<# R& }% v! [9 G T; O
: R3 I+ T9 U; _+ k //cout.setf(ios::fixed,ios::floatfield);
3 ^1 ~0 |2 B% Q0 i' t for(i=1;i<=N;i++)cout<<"k="<<<' ';//<9 c3 b& B4 P# e, i) {3 o# [ $ _. u- }& Y9 {6 }7 i/ n+ W' s9 [" Z
}cout<* O9 G* p7 Z( b8 V) r9 Q: e 5 i9 G& m) I1 \1 E1 ?
} |
|