Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Лабораторная работа - Итерационные методы решения СЛАУ

.doc
Скачиваний:
25
Добавлен:
02.05.2014
Размер:
128.51 Кб
Скачать

Тема: ІТЕРАЦІЙНІ МЕТОДИ РОЗ'ЯЗАННЯ СИСТЕМ ЛІНІЙНИХ АЛГЕБРАЇЧНИХ РІВНЯНЬ (СЛАР)

1. Умова задачі:

А * x = b:   

  *  x  =  

Примітка:

Конец формы

2. Хід роботи:

Запрограмувати один з ітераційних методів, що зводяться до формули

xk+1 := B*xk + d;

2. Для методу простої ітерації достатньою умовою збігу ітерацій є наявність діагональної переваги у системі. Вручну спробуйте перевести задану систему рівнянь до виду "з діагональною перевагою".

3. Аналіз:

Дана система не є системою з діагональною перевагою, тому перед застосуванням методом перетворимо її та отримаємо матрицю з діагональною перевагою:

7.14 1 1.07 1.12 -0.96

1.2 3.28 1.3 -1.63 -1.08

0.87 -2.46 6.32 2.1 -0.617

1.36 0.16 2.1 10 5.22

1.44 0.42 0.883 -5 13

та перетворений вектор:

2.1 0.96 -2.15 -1.92 2.76

Для них і застосуємо метод простої ітерації.

4.Результат роботи:

Файл Input.txt:

7.14 1 1.07 1.12 -0.96

1.2 3.28 1.3 -1.63 -1.08

0.87 -2.46 6.32 2.1 -0.617

1.36 0.16 2.1 10 5.22

1.44 0.42 0.883 -5 13

2.1 0.96 -2.15 -1.92 2.76

A

7.140000 1.000000 1.070000 1.120000 -0.960000

1.200000 3.280000 1.300000 -1.630000 -1.080000

0.870000 -2.460000 6.320000 2.100000 -0.617000

1.360000 0.160000 2.100000 10.000000 5.220000

1.440000 0.420000 0.883000 -5.000000 13.000000

b

2.100000 0.960000 -2.150000 -1.920000 2.760000

x

0.294118 0.292683 -0.340190 -0.192000 0.212308

iteration 1

norma 0.340190

x

0.362770 0.294402 -0.182229 -0.276068 0.119533

iteration 2

norma 0.157961

x

0.339570 0.134354 -0.172134 -0.270175 0.068810

iteration 3

norma 0.160048

x

0.352729 0.125067 -0.238148 -0.240102 0.078131

iteration 4

norma 0.066013

x

0.360458 0.164431 -0.252656 -0.232746 0.093024

iteration 5

norma 0.039364

x

0.357968 0.175913 -0.239389 -0.239154 0.094711

iteration 6

norma 0.013268

x

0.355603 0.168936 -0.232283 -0.242666 0.091250

iteration 7

norma 0.007106

x

0.355601 0.164100 -0.233844 -0.241918 0.089904

iteration 8

norma 0.004836

x

0.356214 0.164648 -0.236106 -0.240810 0.090454

iteration 9

norma 0.002262

x

0.356377 0.166052 -0.236291 -0.240714 0.090948

iteration 10

norma 0.001404

x

0.356259 0.166276 -0.235751 -0.240978 0.090935

iteration 11

norma 0.000541

x

0.356186 0.165970 -0.235561 -0.241072 0.090802

iteration 12

norma 0.000307

x

0.356198 0.165831 -0.235652 -0.241028 0.090771

iteration 13

norma 0.000139

x

0.356220 0.165874 -0.235725 -0.240992 0.090798

iteration 14

norma 0.000073

5.Висновок:

У даній лабораторній роботі я розв’язав систему лінійних алгебраїчних рівнянь методом простої ітерації. Отримав розв’язок з точністю 0.0001

6. Текст програми

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <mem.h>

#include <string.h>

#include <math.h>

#define eps 0.0001

void Out_A(float** a, int size)

{

for (int i=0;i<size;i++)

{

for (int j=0;j<size;j++)

{

printf("%f ",a[i][j]);

}

printf("\n");

}

}

void Out_B(float* b, int size)

{

for ( int i=0;i<size;i++)

{

printf("%f ",b[i]);

}

printf("\n");

}

void iteration(float** a,int size, float* b,float* x)

{

int i,j,k=0;

float norma;

float * xn = new float[size];

memset(xn,0,size*sizeof(float) );

do{

norma=0.0;

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

{

xn[i]=-b[i];

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

{

if(i!=j)

xn[i]+=a[i][j]*x[j];

}

xn[i]/=-a[i][i];

}

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

{

if(fabs(x[i]-xn[i]) > norma)

norma=fabs(x[i]-xn[i]);

x[i]=xn[i];

}

k++;

printf("x\n");

Out_B(x, size);

printf("iteration %d \n",k);

printf("norma %f \n",norma);

}

while(norma > eps);

return;

}

int main()

{

float **a,** a1;

float b[10],*x;

char str[255];

int count = 0,

count_read = 0,

res = 0;

int i,j;

char *tk;

a = new float*[10];

for (int k = 0; k<10 ;k++)

a[k] = new float[10];

a1 = new float*[10];

for (int k = 0; k<10 ;k++)

a1[k] = new float[10];

FILE *stream;

if ( ( stream=fopen("input.txt","rt")) == NULL )

{

printf("File %s didn\'t found\n ", "input.txt");

return 1;

}

while (!feof(stream))

{

fgets(str,255,stream);

if (!count_read || (count_read < count))

{

tk = strtok(str," ");

i = 0;

while(tk)

{

a[count_read][i++] = strtod(tk,NULL);

tk = strtok(NULL," ");

}

count_read++;

if(!count)

count = i;

}

else

{

tk = strtok(str," ");

i = 0;

while(tk)

{

b[i++] = strtod(tk,NULL);

tk = strtok(NULL," ");

}

}

}

printf("A\n");

Out_A(a, count);

printf("b\n");

Out_B(b, count);

x = new float[count];

memset(x,0,count*sizeof(float) );

iteration( a,count, b, x);

getch();

return 0;

}

6