Математика

Решение линейного уравнения методом Гаусса с выбором главной переменной


                ГОСУДАРСТВЕННЫЙ КОМИТЕТ РОССИЙСКОЙ ФЕДЕРАЦИИ
                 ПО ВЫСШЕМУ И СРЕДНЕСПЕЦИАЛЬНОМУ ОБРАЗОВАНИЮ
            КРАСНОЯРСКИЙ ГОСУДАРСТВЕННЫЙ ТЕХНИЧЕСКИЙ УНИВЕРСИТЕТ



                            Практическое задание

                           по компьютерной алгебре

               тема: метод Гаусса с выбором главной переменной



                                                                   Выполнил:
                                                        студент 2 курса ФИВТ
                                                                 группы 27-4
                                                                  Попов А.В.

                                                                  Проверила:
                                                                 Быкова Е.Г.



                                   1998 г.
                              Текст программы.

#include 
#include 
#include 
#include 

const num = 4;
int i,j,I,J;
int c[num+1];
long double x[num+1];
long double max;
long double A[num][num+1];

// ------------------------------------------------------------------

void max_el(int sr, int st)
{       max = A[num+1-sr][num+2-st];
      I = num+1-sr;
      J = num+2-st;
      for (i = num+1-sr ; i<=num ; i++)
      {
            for (j = num+2-st ; j<=num ; j++)
            {
            if (fabs(A[i][j]) > fabs(max))
                 {
                 max = A[i][j];
                 I = i;
                 J = j;
                 }
            }
      }
      cout << "\n\n Max = " << max << "  I=" <<  I<< "  J=" <<  J;
}

// ------------------------------------------------------------------

void print(int sr,int st)
{
  cout << "\n";
    int i,j;
      for (i = num+1-sr ; i<=num ; i++)
      {
            for (j = num+2-st ; j<=num+1 ; j++)
            {
            if (A[i][j] < 0 ) gotoxy(12*j + j - 1,i+1);
                 else gotoxy(12*j + j,i+1);
            cout << A[i][j];
            }
      }
}

// ------------------------------------------------------------------

void preob(int S)
{
  int i,j;
  long double temp;
   for (j = S; j<=num+1; j++) A[S][j] = A[S][j]/max;
    for (i = S + 1; i <= num; i++)
     {
      temp = A[i][S];
      for (j = S; j<= num+1 ; j++) A[i][j] = A[i][j] - A[S][j]*temp;
     }
}

// ------------------------------------------------------------------

void perestanovka(int sr,int st)
{
    if (J != (num+1-sr))
      {
      for (i = 1; i<=num; i++) {
      A[i][J] = A[i][J] + A[i][num+1-sr];
      A[i][num+1-sr] = A[i][J] - A[i][num+1-sr];
      A[i][J] = A[i][J] - A[i][num+1-sr];
      }
      c[J] = c[J] + c[num+1-sr];
      c[num+1-sr] = c[J] - c[num+1-sr];
      c[J] = c[J] - c[num+1-sr];
      }

   if (I != (num+2-st))
      {
      for (j = 1; j<=num+1; j++) {
      A[I][j] = A[I][j] + A[num+2-st][j];
      A[num+2-st][j] = A[I][j] - A[num+2-st][j];
      A[I][j] = A[I][j] - A[num+2-st][j]; }
      }
}

// ------------------------------------------------------------------

void otvet()
{
float temp;
for (i=num; i>=1; i--)
      {
      temp = A[i][num+1];
      for(j = num; j > i; j--) temp = temp - A[i][j]*x[j];

      x[i] = temp/A[i][i];
      }
}

// ------------------------------------------------------------------

void interface()
{
       clrscr();
      print(num,num+1);
      cout << "\n Массив перестановок столбцов ";
         for (i = 1; i <= num ;i++) cout << " " << c[i];
}

// ------------------------------------------------------------------

void load_file()
{
char ch;
 ifstream in("c:\\gauss\\mat.dat");
 cout << "\n";
      for (i = 1 ; i<=num ; i++)
      {
             c[i] = i;
             while (ch != '|') in >> ch;
             ch = 'q';
             for (j = 1 ; j<=num+1 ; j++) in >> A[i][j];
      }
}

// ------------------------------------------------------------------

void main()
{
clrscr();
load_file();
int g;

for(g = num+1; g >= 3; g--)
{
    interface();  max_el(g-1,g); getch();
       perestanovka(g-1,g); interface(); getch();
            preob(num+2-g);  interface(); getch();
}

clrscr();
      print(num,num+1);
      otvet();
      print(num,num+1);
      cout << "\n\n   ";

   long double X[num];
   for (i=1; i<=num; i++) X[c[i]] = x[i];
      for (i=1; i<=num; i++) cout << " X" << i << " = " << X[i];
 getch();
}


                              Тестовые задания.

Задание №1 (найти неизвестные):
4.24x1 + 2.73x2  - 1.55x3 =  1.87
2.34x1 + 1.27x2 + 3.15x3 =  2.16
3.05x1  - 1.05x2  - 0.63x3  = -1.25

1.1 Результат выполнения программы:

x1 = - 0.025461 x2 = 0.915112 x3 = 0.335678

1.2 Расчёт погрешности вычисления:

4.24*(- 0.025461) + 2.73*0.915112  - 1.55*0.335678 =   1,87000022
погрешность:   2,2*10-7

2.34*(- 0.025461) + 1.27*0.915112 + 3.15*0.335678 =   2,1599992
погрешность:   8,0*10-7

3.05*(- 0.025461)  - 1.05*0.915112  - 0.63*0.335678 =  -1,25000079
погрешность:   7,9*10-7

средняя погрешность вычисления: 6,0*10-7



Задание №2 (найти неизвестные):

       3.81x1 +         0.25x2 +      1.28x3 + (0.75+a)x4 = 4.21
       2.25x1 +         1.32x2 + (4.5+a)x3 +        0.49x4 = 6.47+b
       5.31x1 + (0.28+a) x2 +      0.98x3 +        1.04x4 = 2.38
(9.39+a)x1 +         2.45x2 +      3.35x3 +        2.28x4 = 10.48+b

a = (0,1,2,3,4) b = (0,1,2,3,4,5)



2.1 Таблица значений.


|a |b |Ответы:|
|0 |0 |X1 =   |
|  |  |-12.660|
|  |  |899    |
|  |  |X2 =   |
|  |  |-16.107|
|  |  |649    |
|  |  |X3 =   |
|  |  |5.27389|
|  |  |9      |
|  |  |X4 =   |
|  |  |66.2991|
|  |  |37     |
|  |1 |X1 =   |
|  |  |-12.132|
|  |  |586    |
|  |  |X2 =   |
|  |  |-14.858|
|  |  |407    |
|  |  |X3 =   |
|  |  |5.18694|
|  |  |3      |
|  |  |X4 =   |
|  |  |63.3472|
|  |  |89     |
|  |2 |X1 =   |
|  |  |-11.604|
|  |  |272    |
|  |  |X2 =   |
|  |  |-13.609|
|  |  |166    |
|  |  |X3 =   |
|  |  |5.09998|
|  |  |8      |
|  |  |X4 =   |
|  |  |60.3954|
|  |  |4      |
|  |3 |X1 =   |
|  |  |-11.075|
|  |  |957    |
|  |  |X2 =   |
|  |  |-12.359|
|  |  |925    |
|  |  |X3 =   |
|  |  |5.01303|
|  |  |1      |
|  |  |X4 =   |
|  |  |57.4435|
|  |  |95     |
|  |4 |X1 =   |
|  |  |-10.547|
|  |  |642    |
|  |  |X2 =   |
|  |  |-11.110|
|  |  |685    |
|  |  |X3 =   |
|  |  |4.92607|
|  |  |6      |
|  |  |X4 =   |
|  |  |54.4917|
|  |  |46     |
|  |5 |X1 =   |
|  |  |-10.019|
|  |  |327    |
|  |  |X2 =   |
|  |  |-9.8614|
|  |  |45     |
|  |  |X3 =   |
|  |  |4.83912|
|  |  |1      |
|  |  |X4 =   |
|  |  |51.5399|
|  |  |01     |
|1 |0 |X1 =   |
|  |  |13.9596|
|  |  |32     |
|  |  |X2 =   |
|  |  |-39.106|
|  |  |359    |
|  |  |X3 =   |
|  |  |7.32400|
|  |  |7      |
|  |  |X4 =   |
|  |  |-27.756|
|  |  |765    |
|  |1 |X1 =   |
|  |  |16.6685|
|  |  |62     |
|  |  |X2 =   |
|  |  |-46.672|
|  |  |114    |
|  |  |X3 =   |
|  |  |8.73446|
|  |  |X4 =   |
|  |  |-33.605|
|  |  |312    |
|  |2 |X1 =   |
|  |  |19.3774|
|  |  |89     |
|  |  |X2 =   |
|  |  |-54.237|
|  |  |864    |
|  |  |X3 =   |
|  |  |10.1449|
|  |  |13     |
|  |  |X4 =   |
|  |  |-39.453|
|  |  |861    |
|  |3 |X1 =   |
|  |  |22.0864|
|  |  |16     |
|  |  |X2 =   |
|  |  |-61.803|
|  |  |618    |
|  |  |X3 =   |
|  |  |11.5553|
|  |  |67     |
|  |  |X4 =   |
|  |  |-45.302|
|  |  |41     |
|  |4 |X1 =   |
|  |  |24.7953|
|  |  |47     |
|  |  |X2 =   |
|  |  |-69.369|
|  |  |373    |
|  |  |X3 =   |
|  |  |12.9658|
|  |  |2      |
|  |  |X4 =   |
|  |  |-51.150|
|  |  |959    |
|  |5 |X1 =   |
|  |  |27.5042|
|  |  |76     |
|  |  |X2 =   |
|  |  |-76.935|
|  |  |127    |
|  |  |X3 =   |
|  |  |14.3762|
|  |  |74     |
|  |  |X4 =   |
|  |  |-56.999|
|  |  |508    |
|2 |0 |X1 =   |
|  |  |1.03384|
|  |  |3      |
|  |  |X2 =   |
|  |  |-1.6962|
|  |  |73     |
|  |  |X3 =   |
|  |  |0.99795|
|  |  |1      |
|  |  |X4 =   |
|  |  |-0.2117|
|  |  |27     |
|  |1 |X1 =   |
|  |  |1.19117|
|  |  |6      |
|  |  |X2 =   |
|  |  |-2.0168|
|  |  |45     |
|  |  |X3 =   |
|  |  |1.18317|
|  |  |1      |
|  |  |X4 =   |
|  |  |-0.4867|
|  |  |73     |
|  |2 |X1 =   |
|  |  |1.34850|
|  |  |8      |
|  |  |X2 =   |
|  |  |-2.3374|
|  |  |17     |
|  |  |X3 =   |
|  |  |1.36839|
|  |  |X4 =   |
|  |  |-0.7618|
|  |  |19     |
|  |3 |X1 =   |
|  |  |1.50584|
|  |  |1      |
|  |  |X2 =   |
|  |  |-2.6579|
|  |  |89     |
|  |  |X3 =   |
|  |  |1.55361|
|  |  |X4 =   |
|  |  |-1.0368|
|  |  |65     |
|  |4 |X1 =   |
|  |  |1.66317|
|  |  |4      |
|  |  |X2 =   |
|  |  |-2.9785|
|  |  |61     |
|  |  |X3 =   |
|  |  |1.73883|
|  |  |X4 =   |
|  |  |-1.3119|
|  |  |11     |
|  |5 |X1 =   |
|  |  |1.82050|
|  |  |7      |
|  |  |X2 =   |
|  |  |-3.2991|
|  |  |34     |
|  |  |X3 =   |
|  |  |1.92405|
|  |  |X4 =   |
|  |  |-1.5869|
|  |  |57     |
|3 |0 |X1 =   |
|  |  |0.77297|
|  |  |7      |
|  |  |X2 =   |
|  |  |-0.7947|
|  |  |49     |
|  |  |X3 =   |
|  |  |0.76214|
|  |  |6      |
|  |  |X4 =   |
|  |  |0.13016|
|  |1 |X1 =   |
|  |  |0.87276|
|  |  |5      |
|  |  |X2 =   |
|  |  |-0.9543|
|  |  |03     |
|  |  |X3 =   |
|  |  |0.90268|
|  |  |7      |
|  |  |X4 =   |
|  |  |-0.0085|
|  |  |59     |
|  |2 |X1 =   |
|  |  |0.97255|
|  |  |3      |
|  |  |X2 =   |
|  |  |-1.1138|
|  |  |56     |
|  |  |X3 =   |
|  |  |1.04322|
|  |  |9      |
|  |  |X4 =   |
|  |  |-0.1472|
|  |  |78     |
|  |3 |X1 =   |
|  |  |1.07234|
|  |  |1      |
|  |  |X2 =   |
|  |  |-1.2734|
|  |  |1      |
|  |  |X3 =   |
|  |  |1.18377|
|  |  |X4 =   |
|  |  |-0.2859|
|  |  |98     |
|  |4 |X1 =   |
|  |  |1.17212|
|  |  |9      |
|  |  |X2 =   |
|  |  |-1.4329|
|  |  |64     |
|  |  |X3 =   |
|  |  |1.32431|
|  |  |1      |
|  |  |X4 =   |
|  |  |-0.4247|
|  |  |17     |
|  |5 |X1 =   |
|  |  |1.27191|
|  |  |7      |
|  |  |X2 =   |
|  |  |-1.5925|
|  |  |18     |
|  |  |X3 =   |
|  |  |1.46485|
|  |  |3      |
|  |  |X4 =   |
|  |  |-0.5634|
|  |  |36     |
|4 |0 |X1 =   |
|  |  |0.67512|
|  |  |8      |
|  |  |X2 =   |
|  |  |-0.4768|
|  |  |95     |
|  |  |X3 =   |
|  |  |0.64522|
|  |  |5      |
|  |  |X4 =   |
|  |  |0.19602|
|  |  |1      |
|  |1 |X1 =   |
|  |  |0.75463|
|  |  |4      |
|  |  |X2 =   |
|  |  |-0.5806|
|  |  |42     |
|  |  |X3 =   |
|  |  |0.76313|
|  |  |1      |
|  |  |X4 =   |
|  |  |0.10593|
|  |  |6      |
|  |2 |X1 =   |
|  |  |0.83414|
|  |  |X2 =   |
|  |  |-0.6843|
|  |  |9      |
|  |  |X3 =   |
|  |  |0.88103|
|  |  |7      |
|  |  |X4 =   |
|  |  |0.01585|
|  |  |2      |
|  |3 |X1 =   |
|  |  |0.91364|
|  |  |7      |
|  |  |X2 =   |
|  |  |-0.7881|
|  |  |37     |
|  |  |X3 =   |
|  |  |0.99894|
|  |  |2      |
|  |  |X4 =   |
|  |  |-0.0742|
|  |  |33     |
|  |4 | X1 =  |
|  |  |0.99315|
|  |  |3      |
|  |  |X2 =   |
|  |  |-0.8918|
|  |  |84     |
|  |  |X3 =   |
|  |  |1.11684|
|  |  |8      |
|  |  |X4 =   |
|  |  |-0.1643|
|  |  |17     |
|  |5 |X1 =   |
|  |  |1.07265|
|  |  |9      |
|  |  |X2 =   |
|  |  |-0.9956|
|  |  |31     |
|  |  |X3 =   |
|  |  |1.23475|
|  |  |4      |
|  |  |X4 =   |
|  |  |-0.2544|
|  |  |02     |




смотреть на рефераты похожие на "Решение линейного уравнения методом Гаусса с выбором главной переменной"