Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 16.01.2012, 21:59   #1
kefirkamm
 
Регистрация: 22.12.2011
Сообщений: 5
По умолчанию перевести код c++ в c#. решение СЛУ

умные люди, сможете ли вы мне помочь? пожалуйста!!!

Код:
//////////////////////////////////////////////////////////////////////////////
//
//  Solving system of linear equations (orthogonalization method)
//  (c) Johna Smith, 1996
//
//  Method description:
//    Given system of linear equations:
//      a11 x1 + a12 x2 + ... + a1n xn + a(1n+1) = 0
//      a21 x1 + a22 x2 + ... + a2n xn + a(2n+1) = 0
//
//      an1 x1 + an2 x2 + ... + ann xn + a(nn+1) = 0
//
//    Left part of each equation is a result of scalar multiplication of
//    two vectors: ai=(ai1,ai2,...,ain,a(in+1)) and x=(x1,x2,..,xn,1)
//    So to solve this system we need only to build vector x which will be
//    orthogonal to each of ai vectors.
//                            n+1
//    Let u1=a1,   b1=u1/sqrt(SUM u(1j)^2)
//                            j=1
//
//    Other ui we can get from the following iterative formula:
//                      k
//       u(k+1)=u(k+1)-SUM {u(k+1), bj}bj, where {..} means scalar multiplication
//                     j=1
//                          n+1
//       b(k+1)=u(k+1)/sqrt(SUM u(k+1,j)^2)
//                          j=1
//    And finally we can obtain roots of this system from this formula:
//
//    xi=b(n+1,i)/b(n+1n+1)
//
//////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define N     3     // size of matrix
#define N1    N+1
double matrix[N1][N1]=
    {{3,1,-1,-2},
  {1,4,-2,-3},
  {2,-1,5,-15}
    };
void ShowMatrix(void)
{
  for (int i=0;i<N;i++)
  {
    for (int j=0;j<N;j++)
      printf("%+f*x%d",matrix[i][j],i+1);
    printf("%+f=0\n",matrix[i][N]);
  }
}
void main(void)
{
  // Variables declaration
  double c[N],x[N],s;
  int i,j,k,l,m;
  ShowMatrix();
  // expanding system by vector (0,0,0,...,0,1)
  for (i=0;i<N;i++) matrix[N][i]=0;
  matrix[N][N]=1;
  // for all equations
  for (i=0;i<N1;i++)
  {
    if (i>0)
    {
      k=0;
      // make some iterations to increase accuracy of calculations
      while (k<=3)
      {
        for (l=0;l<i;l++)
        {
          c[l]=0;
          for(m=0;m<N1;m++) c[l]+=matrix[l][m]*matrix[i][m];
        }
        for (j=0;j<N1;j++)
        {
          s=0;
          for(l=0;l<i;l++) s+=c[l]*matrix[l][j];
          matrix[i][j]-=s;
        }
        k++;
      }
    }
     // normalizing vector
     s=0;
     for (k=0;k<N1;k++) s+=matrix[i][k]*matrix[i][k];
     s=sqrt(s);
     for (j=0;j<N1;j++) matrix[i][j]/=s;
  }
  s=matrix[N][N];
  for (j=0;j<N;j++) x[j]=matrix[N][j]/s;
  // Printing solution
  printf("\nSolution:\n");
  for (i=0;i<N;i++)
    printf("x%d=%f\n",i+1,x[i]);
}
kefirkamm вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
перевести код c++ в c#. решение СЛУ kefirkamm Помощь студентам 0 25.12.2011 15:21
решение слу методом квадратного корня dmitrialenitchev Общие вопросы C/C++ 1 03.10.2011 00:08
Метод скорейшего спуска решение СЛУ Yulian@ Помощь студентам 11 31.03.2011 11:07
Конкурс для программистов - Решение целочисленной СЛУ Zealint Свободное общение 1 11.02.2011 17:17
Решение СЛУ методом Гаусса-Джордана Жизнь Помощь студентам 2 05.10.2008 16:23