necesito un codigo fuente para resolver ecuaciones

juan david
17 de Marzo del 2004
por favor colaborenme con el codigo fuente para resolver sistemas de ecuaciones n por n ...

noel solw
17 de Marzo del 2004
te envio un programa para resolver un sistema de ecuaciones lineares.
el programa genera las ecuaciones en forma randomal, tu puedes agregarle una funcion para recibir los coeficientes, porsupuesto.
espero que te sea util.

// program k5a10.CPP - page 83
// for a given matrix float [N][N+1] find the soluction vector
// float x[N] by Gaussian elimination method.
// c++ exercices book - dr. gershon kagan (first edition : 2001)
// 7/8/2001
// written in Borland CPP ver 3.1

#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <math.h>

#define N 5

void Init(float a[N][N+1],float x[N])
{
randomize();
int i,j;
for(i = 0; i < N; i++)
{
for(j = 0; j < N+1; j++)
a[i][j] = random(21)-10;
x[i] = 0;
}
} // INIT

void ShowSystem(float a[N][N+1],char *msg)
{
for(int i = 0; i < N; i++)
{
cout << setw(15) << msg;
msg[0] = 0;
for(int j = 0; j < N+1; j++)
cout << setw(10) << a[i][j];
cout << endl;
}
cout << endl;
} // SHOW SYSTEM

void Swap(float &a,float &b)
{
float c = a;
a = b;
b = c;
} // SWAP FLOAT

void Pivotting(float a[N][N+1],int k)
{
int index = k;
for(int i = k+1; i < N; i++)
if(fabs(a[i][k]) > fabs(a[index][k]))
index = i;
if(index != k)
for(int j = 0; j < N+1; j++)
Swap(a[index][j],a[k][j]);
} // PIVOTTING

void ErrorExit(float a[N][N+1])
{
ShowSystem(a,"system :");
cout << "singular system - program halts ! ! !" << endl << endl;
exit(0);
} // ERROR EXIT

void Process(float a[N][N+1],float x[N])
{
for(int i = 0; i < N-1; i++)
{
Pivotting(a,i);
if(!a[i][i])
ErrorExit(a);
for(int k = i+1; k < N; k++)
for(int j = i; j < N+1; j++)
a[k][j] -= a[i][j]*a[k][j]/a[i][i];
}
ShowSystem(a,"system :");
for(;i >= 0;i--)
{
x[i] = a[i][N];
for(int j = N-1; j > i;j--)
x[i] -= a[i][j]*x[j];
if(!a[i][i])
ErrorExit(a);
x[i] /= a[i][i];
}
} // PROCESS

void Verification(float a[N][N+1],float x[N])
{
cout << "verification : " << endl << endl;
for(int i = 0; i < N; i++)
{
float sum = 0;
for(int j = 0; j < N; j++)
sum += a[i][j]*x[j];
cout << "x[" << i << "] = " << setw(8) << x[i] << setw(20)
<< sum << setw(9) << " = " << setw(11) << a[i][N] << endl;
}
cout << endl;
} // VERIFICATION

void main()
{
clrscr();
float a[N][N+1],x[N];
Init(a,x);
cout << setprecision(3) << setiosflags(ios::showpoint)
<< setiosflags(ios::fixed);
ShowSystem(a,"system :");
Process(a,x);
Verification(a,x);
cout << "end of program - good bye ! ! !n";
getch();
} // MAIN

/*
system : 4.000 -5.000 6.000 -3.000 -7.000 5.000
-4.000 -7.000 6.000 -9.000 -2.000 -3.000
4.000 4.000 6.000 6.000 8.000 7.000
5.000 -2.000 7.000 5.000 4.000 1.000
-1.000 -5.000 -8.000 -3.000 -10.000 -7.000

system : 5.000 -2.000 7.000 5.000 4.000 1.000
0.000 -9.800 -2.400 0.000 -0.400 -2.400
0.000 0.000 2.416 0.000 -1.918 -4.229
0.000 0.000 0.000 0.000 -2.409 8.306
0.000 0.000 0.000 0.000 2.753 11.629

singular system - program halts ! ! !

system : 9.000 -8.000 -9.000 5.000 -5.000 4.000
-3.000 3.000 8.000 2.000 1.000 -9.000
-4.000 4.000 7.000 -5.000 3.000 -8.000
8.000 -8.000 -5.000 3.000 -1.000 -8.000
-10.000 6.000 10.000 8.000 -8.000 1.000

system : -10.000 6.000 10.000 8.000 -8.000 1.000
0.000 -12.800 -10.000 5.400 -0.200 -8.800
0.000 0.000 -3.938 12.797 -0.984 1.375
0.000 0.000 0.000 -54.387 0.443 -3.710
0.000 0.000 0.000 0.000 0.149 -3.889

verification :

x[0] = 24.342 1.000 = 1.000
x[1] = -3.428 -8.800 = -8.800
x[2] = 5.713 1.375 = 1.375
x[3] = -0.145 -3.710 = -3.710
x[4] = -26.128 -3.889 = -3.889

end of program - good bye ! ! !
*/