0% found this document useful (0 votes)
45 views6 pages

Priscilla Chaves de Araújo Felipe Vinícius S. de Oliveira

This C code defines functions for solving systems of linear equations using several different numerical methods: Gauss-Jordan with and without partial pivoting, Jacobi iteration, and Seidel iteration. It includes functions to read a matrix from a file, display a menu to select a method, and implements the core algorithms for each method. The main function brings it all together by reading the matrix, selecting a method, calling the corresponding function, and returning the result.

Uploaded by

Felipe Oliveira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views6 pages

Priscilla Chaves de Araújo Felipe Vinícius S. de Oliveira

This C code defines functions for solving systems of linear equations using several different numerical methods: Gauss-Jordan with and without partial pivoting, Jacobi iteration, and Seidel iteration. It includes functions to read a matrix from a file, display a menu to select a method, and implements the core algorithms for each method. The main function brings it all together by reading the matrix, selecting a method, calling the corresponding function, and returning the result.

Uploaded by

Felipe Oliveira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Priscilla Chaves de Arajo

Felipe Vincius S. de Oliveira

#include <stdio.h>

#include <math.h>

#include <malloc.h>

#include <conio.h>

int ReadFile ( )

char filename[] = "Matriz.txt";

FILE *file = fopen ( filename, "r" );

if (file != NULL)

char line [100];

while(fgets(line, sizeof line, file)!= NULL)

fprintf(stdout,"%s",line); //print the file contents on stdout.

fclose(file);

else {

perror(filename); //print the error message on stderr.

return filename[0];

int menu()

printf("\nMenu:\n");

printf("1 - Out\n");
printf("2 - Method GJpp\n");

printf("3 - Method GJsp\n");

printf("4 - Method Jacodl\n");

printf("5 - Method Seidl\n");

printf("Type the method you want: \n");

int method;

scanf("%d", &method);

return method;

// Gauss-Jordan no pivot

float GJsp()

int i, j, n, m, k;

float matrix_coef[i][j], matrix_sol[i][j];

for (i=1; i<=n; i++)

for(j=1; j<=n; j++)

m = matrix_coef[j][i]/matrix_coef[i][i];

matrix_coef[i][j]= 0;

for(k= i+1; k<=n; k+1)

matrix_coef[j][k]=matrix_coef[j][k]-m*matrix_coef[i][k];

matrix_sol[i][j]=matrix_sol[i][j]-m*matrix_coef[i][j];

//Gauss-Jordan partial pivot

float GJpp()

{
int i, j, n, max_line;

float matrix_coef[i][j], matrix_sol[i][j];

float max_number, aux;

max_number = matrix_coef[0][0];

for(i=j; i<n-1; i++)

if (max_number < matrix_coef[i+1][j])

max_number = matrix_coef[i+1][j];

max_line = i+1;

if(max_number != matrix_coef[j][j])

for(i=1; i<= n; i++)

aux= matrix_coef[j][i];

matrix_coef[j][i] = matrix_coef[max_line][i];

matrix_coef[max_line][i] = aux;

aux = matrix_sol[i][j];

matrix_sol[i][j] = matrix_coef[max_line][j];

matrix_sol[max_line][j] = aux;

// Jacobi

float Jacodl ()

int i, j, n, z,k;

float matrix_coef[i][j], a[n], x[n], y[n], t[n];


float soma, norma, e;

for(i=0; i<n; i++)

for(j=0; j<i; j++)

soma = soma + matrix_coef[i][j]*t[j];

x[i]=(a[i]-soma)/matrix_coef[i][i];

printf("indique a precisao\n");

scanf("%f", &e);

for(i=0; i<n-1;i++)

float z=0, k=0;

z = z + y[i]*y[i];

k = k + x[i]*x[i];

float norma=0;

norma = sqrt(z)/sqrt(k);

//Seidel

float Seidl ()

int i, j, n, z, k;

float matrix_coef[i][j], a[n], x[n], y[n];

float soma1=0, soma2=0, norma=0, e;

for(i=0; i<n; i++)

for(j=0; j<i; j++)

{
soma1 = soma1 + matrix_coef[i][j]*y[j];

for(j=i+1; j<n; j++)

soma2 = soma2 + matrix_coef[i][j]*y[j];

x[i]=(a[i]-soma1-soma2)/matrix_coef[i][i];

printf("indique a precisao\n");

scanf("%f", &e);

for(i=0; i<n-1;i++)

float z = 0, k = 0;

z = z + y[i]*y[i];

k = k + x[i]*x[i];

norma = sqrt(z)/sqrt(k);

int main()

char filename[] = "file.txt";

char n;

int method;

float norma, e;

FILE *file = fopen ( filename, "r" );

printf("The file.txt is: \n");

ReadFile();

filename[0] = n;

printf("%c", n);

method = menu();
switch (method)

case 1:

break;

case 2: printf("Gauss-Jordan com pivotacao parcial\n");

GJpp();

break;

case 3: printf("Gauss-Jordan sem pivotacao\n");

GJsp();

break;

case 4: printf("Metodo Iterativo de Jordan com Dominancia por linha\n");

do

{Jacodl();}

while (norma<=e);

break;

case 5: printf("Metodo Iterativo de Seidel com Dominancia por linha\n");

Seidl();

break;

default: printf("****Invalid Option****");

break;

return 0;

You might also like