0% found this document useful (0 votes)
26 views

Midrigan SI191 Lab 2

The document describes a laboratory report that solved a system of linear equations using four different methods: Gaussian elimination, Cholesky decomposition, Jacobi iteration, and Gauss-Seidel iteration. The same system of 4 equations was solved using each method and the results were identical, showing the methods were successful. The purpose of the laboratory work was to gain experience solving systems of linear equations using different numerical methods in code.

Uploaded by

Alberto Bonnuci
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Midrigan SI191 Lab 2

The document describes a laboratory report that solved a system of linear equations using four different methods: Gaussian elimination, Cholesky decomposition, Jacobi iteration, and Gauss-Seidel iteration. The same system of 4 equations was solved using each method and the results were identical, showing the methods were successful. The purpose of the laboratory work was to gain experience solving systems of linear equations using different numerical methods in code.

Uploaded by

Alberto Bonnuci
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Ministerul Educaţiei

al Republicii Moldova

Universitatea Tehnică a Moldovei

RAPORT
despre lucrarea de laborator Nr. 2
la Metode si Modele de Calcul

Tema: Rezolvarea Numerica a Sistemelor de Ecuatii Liniare


Varianta 17

A îndeplinit: Midrigan Ovidiu

Chişinău – 2020
Scopul lucrarii:
1) Să se rezolve sistemul de ecuații lineare Ax=b, utilizandii lineare Ax=b, utilizand
-Metoda eliminarii lui Gauss
-Metoda lui Cholesky
-Metoda iterativa a lui Jacobi cu o eroare
-Metoda iterativa a lui Gauss-Seidel cu o eroare

Codul:
#include <iostream>
#include <cmath>
#include <stdio.h>

double **matrice = NULL;


double *vectorulB = NULL;
int marimeMatrice;
void introducereaDatelor();
void metodaGauss();
void metodaCholesky();
void metodaJacobi();
void metodaGaussSeidel();

int main() {
introducereaDatelor();
metodaGauss();
metodaCholesky();
metodaJacobi();
metodaGaussSeidel();
for (int row = 0; row < marimeMatrice; row++)
delete matrice[row];
delete matrice;
delete vectorulB;
return 0;
}
void introducereaDatelor() {
std::cout << "Introdu marimea matricii: ";
std::cin >> marimeMatrice;
matrice = new double*[marimeMatrice];
for (int row = 0; row < marimeMatrice; row++)
matrice[row] = new double[marimeMatrice];
vectorulB = new double[marimeMatrice];
std::cout << "Introdu datele matricii: " << std::endl;
for (int rand = 0; rand < marimeMatrice; rand++) {
for (int coloana = 0; coloana < marimeMatrice; coloana++) {
std::cin >> matrice[rand][coloana];
}
}
std::cout << std::endl << "Introdu datele vectorului b" << std::endl;
for (int i = 0; i < marimeMatrice; i++) {
std::cin >> vectorulB[i];
}
std::cout << std::endl;
}
void metodaGauss() {
double gaussmatrice[marimeMatrice][marimeMatrice+1];
for (int row = 0; row < marimeMatrice; row++) {
for (int col = 0; col < marimeMatrice+1; col++)
gaussmatrice[row][col] = matrice[row][col];
gaussmatrice[row][marimeMatrice] = vectorulB[row];
}
int rowWithMax;
double tempRow[marimeMatrice+1];
for (int idx = 0; idx < marimeMatrice; idx++) {
rowWithMax = idx;
for (int k = idx+1; k < marimeMatrice; k++) {
if (gaussmatrice[idx][idx] < gaussmatrice[k][idx])
rowWithMax = k;
}
if (rowWithMax != idx) {
for (int il = 0; il < marimeMatrice+1; il++) {
tempRow[il] = gaussmatrice[idx][il];
gaussmatrice[idx][il] = gaussmatrice[rowWithMax][il];
gaussmatrice[rowWithMax][il] = tempRow[il];
}
}
}
double t;
for (int i = 0; i < marimeMatrice-1; i++) {
for (int k = i+1; k < marimeMatrice; k++) {
t = -gaussmatrice[k][i] / gaussmatrice[i][i];
for (int cell = 0; cell < marimeMatrice+1; cell++) {

gaussmatrice[k][cell] += t*gaussmatrice[i][cell];
}
}
}
double results[4] = {};
double sum;
for (int i = marimeMatrice-1; i >= 0; i--) {
sum = gaussmatrice[i][marimeMatrice];
for (int termen = i+1; termen < marimeMatrice; termen++) {
sum -= gaussmatrice[i][termen]*results[termen];
}
results[i] = sum / gaussmatrice[i][i];
}
printf("\nRezultatele Metodei Eliminarii lui Gauss: \n");
for(int i = 0;i < sizeof(results)/sizeof(results[0]);i++){
printf("x%d = %f, ",i+1, results[i]);
}
}
void metodaCholesky() {
double metodaCholeskymatrice[marimeMatrice][marimeMatrice];
for (int row = 0; row < marimeMatrice; row++) {
for (int col = 0; col < marimeMatrice; col++)
metodaCholeskymatrice[row][col] = matrice[row][col];
}
double lowermatrice[marimeMatrice][marimeMatrice] = {};
double L_transpose[marimeMatrice][marimeMatrice] = {};
double sum;

for (int j = 0; j < marimeMatrice; j++) {


for (int i = j; i < marimeMatrice; i++) {
sum = 0;
if (i == j) {
for (int k = 0; k < j; k++)
sum += pow(lowermatrice[i][k], 2);
lowermatrice[i][j] = sqrt(metodaCholeskymatrice[i][j]-sum);
}
else {
for (int k = 0; k < j; k++)
sum += lowermatrice[i][k] * lowermatrice[j][k];
lowermatrice[i][j] = (metodaCholeskymatrice[i][j] - sum)/lowermatrice[j][j];
}
}
}
for (int i = 0; i < marimeMatrice; i++) {
for (int j = 0; j < marimeMatrice; j++) {
L_transpose[i][j] = lowermatrice[j][i];
}
}
double y[4] = {};
for (int i = 0; i < marimeMatrice; i++) {
sum = vectorulB[i];
for (int cell = 0; cell < i; cell++) {
sum -= lowermatrice[i][cell] * y[cell];
}
y[i] = sum / lowermatrice[i][i];
}
double x[4] = {};
for (int i = marimeMatrice-1; i >= 0; i--) {
sum = y[i];
for (int cell = i+1; cell < marimeMatrice; cell++) {
sum -= L_transpose[i][cell]*x[cell];
}
x[i] = sum / L_transpose[i][i];
}
printf("\nRezultatele Metodei lui Cholesky: \n");
for(int i = 0;i < sizeof(x)/sizeof(x[0]);i++){
printf("x%d = %f, ",i+1, x[i]);
}
}
void metodaJacobi() {
double solutions[4] = {};
double svSolutions[4];
double sum;

for (int j = 0; j < 30; j++) {


for (int x = 0; x < marimeMatrice; x++) {
sum = vectorulB[x];
for (int termen = 0; termen < marimeMatrice; termen++) {
if (x == termen) continue;
sum -= matrice[x][termen] * solutions[termen];
}
svSolutions[x] = sum / matrice[x][x];
}
for (int i = 0; i < marimeMatrice; i++) {
solutions[i] = svSolutions[i];
}
}
printf("\nRezultatele Metodei Iterative Jacobi: \n");
for(int i = 0;i < sizeof(solutions)/sizeof(solutions[0]);i++){
printf("x%d = %f, ",i+1, solutions[i]);
}
}
void metodaGaussSeidel() {
double solutions[4] = {};
double sum;
for (int j = 0; j < 30; j++) {
for (int x = 0; x < marimeMatrice; x++) {
sum = vectorulB[x];
for (int termen = 0; termen < marimeMatrice; termen++) {
if (x == termen) continue;
sum -= matrice[x][termen] * solutions[termen];
}
solutions[x] = sum / matrice[x][x];
}
}
printf("\nRezultatele Metodei Gauss-Seidel: \n");
for(int i = 0;i < sizeof(solutions)/sizeof(solutions[0]);i++){
printf("x%d = %f, ",i+1, solutions[i]);
}
}
Output:
Introdu marimea matricii: 4
Introdu datele matricii:
19.2
-0.9
0.8
0.7
-0.9
13.9
1.2
0.6
0.8
1.2
20.1
0.4
0.7
0.6
0.4
11.5

Introdu datele vectorului b


-13.2
9.2
8.6
14.7

Rezultatele Metodei Eliminarii lui Gauss:


x1 = -0.726263, x2 = 0.525018, x3 = 0.399926, x4 = 1.281165,
Rezultatele Metodei lui Choesky:
x1 = -0.726263, x2 = 0.525018, x3 = 0.399926, x4 = 1.281165,
Rezultatele Metodei Iterative Jacobi:
x1 = -0.726263, x2 = 0.525018, x3 = 0.399926, x4 = 1.281165,
Rezultatele Metodei Gauss-Seidel:
x1 = -0.726263, x2 = 0.525018, x3 = 0.399926, x4 = 1.281165,
Verificare

Concluzie:
În aceasta lucare de laborator, utlizand un limbaj de programare și cunostinelei cunostinele
obtinute la cursuri, am realizat un sistem de ecuații lineare Ax=b, utilizandii liniare, utilizand 4 metode diferite.
Putem observa ca a fost rezolvat cu succes deoarece inlocuind x-urile în ecuații lineare Ax=b, utilizandii, obții lineare Ax=b, utilizandinem
răspunsul la care ne ași cunostineleteptam.

You might also like