Kod programu (środowisko Dev-C++ 5)
#include
<stdio.h>
#include
<stdlib.h>
#include
<math.h>
int
main()
{
int
n,i,j,k,ile = 0;
double
eps = 0.0001, suma, norma, suma2;
printf(
"Podaj rozmiar macierzy: "
);
scanf(
"%d"
, &n);
double
macA[n][n], macB[n][n];
double
y[n], yp[n], wlasny[n], lambda , lambdap;
printf(
"Podaj wyrazy (wierszami):\n"
);
for
(i = 0; i < n; i++)
{
printf(
"Wiersz nr %d:\n"
, i+1);
for
(j = 0; j < n; j++)
{
scanf(
"%lf"
, &macA[i][j]);
}
}
FILE *wynik;
wynik = fopen(
"wynik.txt"
,
"wt"
);
printf(
"\nMacierz postaci:\n"
);
for
(i = 0; i < n; i++)
{
printf(
"| "
);
fprintf(wynik,
"| "
);
for
(j = 0; j < n; j++)
{
printf(
"%-10.5lf "
, macA[i][j]);
fprintf(wynik,
"%-10.5lf "
, macA[i][j]);
}
printf(
"|\n"
);
fprintf(wynik,
"|\n"
);
}
for
(k = 0; k < n; k++)
{
for
(i = 0; i < n; i++)
{
y[i] = 0;
yp[i] = 0;
}
yp[0] = 1;
ile = 0;
lambda = 1;
lambdap = 0;
suma2 = 0;
while
(fabs(lambda - lambdap > eps))
{
suma = 0;
for
(i = 0; i < n; i++)
{
for
(j = 0; j < n; j++)
{
y[i] += yp[j]*macA[i][j];
}
suma += y[i]/yp[i];
}
for
(i = 0; i < n; i++)
{
yp[i] = y[i];
wlasny[i] = y[i];
y[i] = 0;
}
if
(ile > 1)
{
lambdap = lambda;
lambda = (1.0/n)*suma;
}
ile++;
}
//wyznaczanie wektora U = V/|V|
for
(i = 0; i < n; i++)
{
suma2 += pow(wlasny[i],2);
}
norma = sqrt(suma2);
for
(i = 0; i < n; i++)
{
wlasny[i] = wlasny[i]/norma;
}
//macierz B = lambda*U*U'
for
(i = 0; i < n; i++)
{
for
(j = 0; j < n; j++)
{
macB[i][j] = lambda*wlasny[j]*wlasny[i];
}
}
//nowa macierz A
for
(i = 0; i < n; i++)
{
for
(j = 0; j < n; j++)
{
macA[i][j] = macA[i][j] - macB[i][j];
}
}
//wynik
printf(
"\nlambda[%d] = %lf\niteracji: %d\n\n"
,k+1,lambda,ile);
fprintf(wynik,
"lambda[%d] = %lf\n"
, k+1, lambda);
fprintf(wynik,
"["
);
for
(i = 0; i < n; i++)
{
fprintf(wynik,
"%-8.4lf "
,wlasny[i]);
if
(i < n-1) fprintf(wynik,
"; "
);
}
fprintf(wynik,
"]\n"
);
}
fclose(wynik);
system(
"pause"
);
return
0;
}