Code:
include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#define MODULO(x) (x>=0 ? x : (-1) * x)
#define CONSTCOL 1
#define YPOS 0
#define CONSTID 2
int n; // dimensão do sistema linear
char *vetPts = "pontos.dat";// nome do arquivo com o vetor de aproximação inicial
void carregaPontos(char *arq, int n, double ***matPts);
void resolveEGauss(int n, double **mat, double *y);
void mostraMat(int n, double **mat);
void geraMatrizElem(int n, double ***matPts, double ***matElem);
void geraVetorInd(int gPol, int m, double *x, double* y, double* vec);
double somatorioIndependentes(int ini, int fim,int gX, double *x, double* y);
double somatorioGrau(int ini, int fim, int exp, double* x);
double **matPts; //Eu vou gerar uma matriz só pra carregar os pontos do arquivo
double **matElem; //essa matriz aqui recebe os pontos já somados e elevados na n
int main(){
int n;
int gPol;
printf("Informe o numero de pontos n: ");
scanf("%d", &n);
printf("Informe o grau do polinomio: ");
scanf("%d", &gPol);
carregaPontos(vetPts, n, &matPts);
mostraMat(n, matPts);
geraMatrizElem(n, &matElem, &matPts);
return 0;
}
/*read the points from the file and store them into a xy array*/
void carregaPontos(char *arq, int n, double ***matPts)
{
int i,j;
float lido;
FILE *fp;
*matPts = calloc(n, sizeof(double **));
fp=fopen(arq,"r");
for(i=0;i<n;i++)
{
(*matPts)[i] = (double *) calloc(CONSTID,sizeof(double));
if((*matPts)[i] == NULL) {
printf("Error while allocating");
}
for(j=0;j<CONSTID;j++)
{
fscanf(fp,"%f ",&lido);
//printf("%f \n", lido);
(*matPts)[i][j] = lido;
}
}
fclose(fp);
}
//This should generate the matrix of elements but is not working
/*void geraMatrizElem(int gPol, int n, double *x, double **matElem){
int i, j;
matElem = calloc(n, sizeof(double *));
for(i=0;i<n;i++)
{
matElem[i] = calloc(n,sizeof(double));
if(matElem[i] == NULL) {
printf("Error while allocating");
}
}
for (i=0;i<gPol;i++){
for (j=0;j<gPol;j++){
matElem[i][j] = somatorioGrau(0,n,i+j,x);
}
}
}
*/
/*This function generates the vector of independent elements
void geraVetorInd(int gPol, int m, double *x, double* y, double* vec){
int i;
for (i = 0;i<gPol;i++){
vec[i] = somatorioIndependentes(0,m,i,x,y);
}
}
*/
/*make the sums of the independent elements vector*/
/*Samir eu refiz essa função agora tá bem mais perto de dar certo tipo a explicação e a mesma logica do anterior, mas eu mão to conseguindo acessar os elementos da matriz original, mas isso é um problema de ponteiros vale a mesma lógica de soma dos pontos, talvez isso esteja errado vou rever mais aqui*/
void geraMatrizElem(int n, double ***matPts, double ***matElem){
int i, j;
float k, l;
matElem = calloc(n, sizeof(double *));
for(i=0;i<n;i++)
{
matElem[i] = calloc(CONSTCOL,sizeof(double));
if(matElem[i] == NULL) {
printf("Error while allocating");
}
}
for (i = 0;i<n;i++){
for (j = 0; j < n; j++){
k = i;
l = j;
//matElem[i][j] = pow((matPts)[i][YPOS],i+j);
//matElem[i][j] = pow((matPts[i][YPOS],k+l);// This is broken
printf("%f",matElem[i][j]);
}
}
}
/*double somatorioIndependentes(int ini, int fim,int gX, double *x, double* y){
int i = 0;
double result=0;
for (i=ini;i<=fim;i++){
result += (pow(x[i],gX)*y[i]);
}
return result;
}
*/
/*make the sums of the main matrix
double somatorioGrau(int ini, int fim, int exp, double *x){
int i;
double result = 0;
for (i=ini;i<=fim;i++){
result+=pow(x[i],exp);
}
return result;
}
Testing function just to assure the matrix is there is prints the matrix created with the points read from inpout*/
void mostraMat(int n, double **mat){
int i,j;
for(i=0; i<n; i++)
{
for(j=0; j<CONSTID; j++)
printf("%03.2f ", (float)mat[i][j]);
printf("\n");
}
printf("\n");
}
/*Given a matrix **mat its dimension n and a vector *y solves the system Ax = b using Gaussian Elimination */
void resolveEGauss(int n, double **mat, double *y)
{
int k, i, j; // para uso em fors
int index; // Ã..ndice da linha que tem o pivô
double maior; // pivô da coluna
double fator; // fator multiplicativo para zerar os elementos abaixo do pivô
double temp; // variável auxiliar para trocar os elementos de posição em y
double *lmaior; // ponteiro para a linha indexada por index
double x[n]; // vetor com o resultado
for(k=0; k<n; k++) // percorre todas as colunas
{
maior = 0;
for(i=k; i<n; i++) // encontra o maior da coluna k
{
if(MODULO(maior)<MODULO(mat[i][k]))
{
maior = mat[i][k];
index = i;
}
}
if(maior == 0)
{
printf("\nEl. de Gauss: MATRIZ SINGULAR!\n");
return;
}
lmaior = mat[index]; // Troca linhas para o maior elemento ser pivô
mat[index] = mat[k];
mat[k] = lmaior;
temp = y[index]; // Faz o mesmo no vetor de termos independentes
y[index] = y[k];
y[k] = temp;
for(i=k+1; i<n; i++) // Zera os elementos abaixo do pivô
{
fator = mat[i][k]/mat[k][k];
y[i] = y[i] - fator*y[k];
for(j=k; j<n; j++)
{
mat[i][j] = mat[i][j] - fator*mat[k][j];
}
}
}
// Para finalizar, faz-se a retrosubstituição
for(k=n-1; k>=0; k--)
{
x[k] = y[k];
for(i=k+1; i<n; i++)
{
x[k] = x[k] - mat[k][i]*x[i];
}
x[k] = x[k]/mat[k][k];
}
printf("Resposta obtida pela Eliminacao de Gauss com Pivoteamento:\n");
for(j=0; j<n; j++)
printf("\nx%d = %f", j+1, (float)x[j]);
printf("\n\n");
}