Sunteți pe pagina 1din 5

Universitatea Tehnic a Moldovei Facultatea Calculatoare Informatic i Microelectronic

Lucrarea de Laborator nr. 2


la Metode Numerice Tema: Rezolvarea numerica a sistemelor de ecuatii liniare.

A efectuat: Chihai Alexandru A verificat: Moraru Vasile

Chiinu 2011

Scopul lucrrii:
S se rezolve sistemul de ecuaii liniare Ax=b,utiliznd -metoda lui Cholesky(metoda rdcinii ptrate). -metoda iterativ a lui Jacobi cu o eroare e=0.001. -metoda iterativ a lui Gauss-Seidel cu o eroare e=0,001 i c=0,00001.

S se determine numrul de iteraii necesare pentru aproximarea soluiei cu eroarea dat e. S se compare rezultatele. S se inverseze matricea A cu ajutorul metodei Jordan-Gauss..Matricele iniiale A,respectiv b A[N][N]= { 5, 1, -1,
1, 56, 1, -1, 1, 3

}; b[N]={ - 9,13,15 }; Listingul programului: Metoda lui Cholesky


#include <iostream.h> #include <math.h> #include <conio.h> #define nr 3 void main() { float sist_ec[nr][nr]={ 8, 4, -9, 13, 90, 10, -12, 15, 30 }; float b[nr]={- 6,33,55 }; float l[nr][nr]={0}; float y[nr]={0},x[nr]={0}; clrscr(); cout<<"Matricea A este..."; for(int i=0;i<nr;i++){cout<<endl<<"|"; for(int j=0;j<nr;j++) cout<<" "<<sist_ec[i][j]; } cout<<"\nVectorul B este...\n"; for(i=0;i<nr;i++) cout<<" "<<b[i]; l[0][0]=(float)sqrt(sist_ec[0][0]); for(int j=1;j<nr;j++) l[0][j]=sist_ec[0][j]/l[0][0]; float sum=0; for(i=1;i<nr;i++){ for(int k=0;k<=i-1;k++) sum=sum+l[k][i]*l[k][i]; l[i][i]=(float)sqrt(sist_ec[i][i]-sum); sum=0; for(int j=i;j<nr;j++) { if(i>j) l[i][j]=0; else if(i<j){ for(int k=0;k<=j-1;k++) sum=sum+l[k][i]*l[k][j]; l[i][j]=(sist_ec[i][j]-sum)/l[i][i]; sum=0; } }

} y[0]=b[0]/l[0][0]; sum=0; for(i=1;i<nr;i++){ for(int k=0;k<=i-1;k++) sum+=l[k][i]*y[k]; y[i]=(b[i]-sum)/l[i][i]; sum=0; } sum=0; x[nr-1]=y[nr-1]/l[nr-1][nr-1]; for(i=nr-2;i>=0;i--){ for(int k=i+1;k<nr;k++) sum+=l[i][k]*x[k]; x[i]=(y[i]-sum)/l[i][i]; sum=0; } cout<<"\n\nSolutiile sint:\n"; for(i=0;i<nr;i++) cout<<"\tX"<<i+1<<"="<<x[i]<<endl; getch(); }

Listingul programului: Metoda iterativ a lui Jacobi& Gauss-Seidel


#include<iostream.h> #include<math.h> #include<conio.h> #include<stdlib.h> #define NR 3

#define Q 0

// 1-JACOBI // 0-GAUSS-SEIDEL int DiagDom(float a[NR][NR], int size) { float s; for( int i=0; i<size; i++) { s = 0; for( int j=0; j<size; j++) if( i != j ) s += a[i][j]; if( a[i][i] < s ) return 0; if( a[i][i] == 0) return 0; } return 1; } void main(void) { float a[NR][NR]={8, 4, -9, 13, 90, 10, -12, 15, 30 }; float x[NR],x1[NR],q[NR][NR]={0},d[NR],max,P; float t[3]={-6,33,55}; int size; int i,ni = 0; clrscr(); cout<<"Matricea A este..."; for(i=0;i<NR;i++){cout<<endl; for(int j=0;j<NR;j++) cout<<" "<<a[i][j];} for( i=0; i<NR; i++) for( int j=0; j<NR; j++) if( i != j ) q[i][j] = - ( a[i][j] / a[i][i] ); else q[i][j] = 0; if( !DiagDom(a,NR) ){ cout<<"Aceasta matricea nu este diagonal dominata\n";getch();exit(1); } cout<<"\nVector 'b'este...\n"; for( i=0; i<NR; i++) { cout<<" "<<t[i]; d[i] = t[i] / a[i][i]; } while(1){ ni=0; cout<<"\nIntrodu eroarea:"; float Eps; cin>>Eps; for( i=0; i<NR; i++) if(Q) x1[i] = d[i];//Jacobi else x[i] = d[i]; //Gauss-Seidel do{ for( i=0; i<NR; i++) x1[i] = x[i]; for( i=0; i<NR; i++){ P = 0; for(int j=0; j<NR; j++) if(Q) P+= q[i][j] * x1[j]; // Jakobi

else P+= q[i][j] * x[j]; // Gauss - Seidel x[i] = P + d[i]; } max = (float)fabs(x[0] - x1[0]); for( i=1; i<NR; i++) if( (float)fabs(x[i] - x1[i]) > max ) max = (float)fabs(x[i] - x1[i]); ni ++; } while( max > Eps); if(Q) cout<<"***JACOBI***\n"; else cout<<"***GAUSS-SEIDEL***\n"; for( i =0; i<NR; i++) cout<<"x"<<i+1<<" = "<<x[i]<<endl; cout<<"\nS-a efectuat in<"<<ni<<">iteratii"<<endl; cout<<"\nDin nou(NU->Esc)?"; if(getch()==27) exit(1); } getch(); }Metoda iterativ a lui Gauss-Seidel

Concluzie: n urma efectuarii lucrrii de laborator am nsuit metodele de rezolvare numeric a sistemelor de ecuatii liniare.Rezolvarea este de doua tipuri: metode directe si metode iterative.Metodele directe constau in transformarea sistemului Ax=b intr-un sistem echivalent pentru care rezolvarea este mult mai simpla. In metodele directe solutia exacta se obtine dupa un numar finit de operatii aritmetice elementare(adunare, scadere, inmultire, impartire si radacina patrata) si acest numar de operatii este de ordinal n3.