在网上搜了下,说关于求解五元一次方程的C#算法很多,源代码也很多,但我却找不到。那位给个C#的源代码,在线等,谢谢。在最好是用高斯消元法。

解决方案 »

  1.   

    http://www.codeproject.com/csharp/matrix.asp
      

  2.   

    不是说了用矩阵么?自己弄个矩阵的class就行, 给出的Link里面有个矩阵类
      

  3.   

    回去翻下<线性代数>就知道了
      

  4.   

    这里面不是有么?http://www.codeproject.com/csharp/matrix.asp
      

  5.   

    上面的看不懂就看这个吧http://www.codeproject.com/cs/library/matrixoperations.asp
      

  6.   

    err...你要用的只是那个Matrix Class而已, 然后在你的Main函数里面调用下面的代码:static void Main()
    {
    double[,] DATA = {{2,1,3,5,9},{2,2,1,10,1},{7,3,5,2,1},{3,7,7,7,9},{12,4,1,23,5}};
    Matrix COF = new Matrix(DATA);
    Matrix CON = new Matrix(5,1);
    CON[0,0]=198;CON[1,0]=166;CON[2,0]=115;CON[3,0]=286;CON[4,0]=429;
    Console.WriteLine ("Determinent of COF matrix = " + 
    COF.Det().ToString() );
    Matrix Sol = COF.Inverse()*CON;
    Sol.Display();
    Console.Read();
    }DATA是五元一次方程的系数, Matrix CON是五元一次方程的结果, 即2X+Y+3Z+5P+9Q=198
    2X+2Y+Z+10P+Q=166
    7X+3Y+5Z+2P+Q=115
    3X+7Y+7Z+7P+9Q=286
    12X+4Y+Z+23P+5Q=429然后, 程序解方程得出的结果是:X = 4
    Y = 5
    Z = 7
    P = 13
    Q = 11
      

  7.   

    楼上,Matrix Class是要下载的吗?下载不了啊!!
      

  8.   

    sub gaussj(a(), n, b())
    '高斯消元法。
        dim ipiv(50), indxr(50), indxc(50)
        for j = 1 to n
            ipiv(j) = 0
        next 
        for i = 1 to n
            big = 0
            for j = 1 to n
                if ipiv(j) <> 1 then
                    for k = 1 to n
                    if ipiv(k) = 0 then
                        if abs(a(j, k)) >= big then
                            big = abs(a(j, k))
                            irow = j
                            icol = k
                        end if
                    elseif ipiv(k) > 1 then
                        havaResult = false
    exit sub
                    end if
                    next 
                end if
            next 
            ipiv(icol) = ipiv(icol) + 1
            if irow <> icol then
                for l = 1 to n
                    dum = a(irow, l)
                    a(irow, l) = a(icol, l)
                    a(icol, l) = dum
                next 
                dum = b(irow)
                b(irow) = b(icol)
                b(icol) = dum
            end if
            indxr(i) = irow
            indxc(i) = icol
            if a(icol, icol) = 0 then 
     havaResult = false
    exit sub
    end if
            pivinv = 1 / a(icol, icol)
            a(icol, icol) = 1
            for l = 1 to n
                a(icol, l) = a(icol, l) * pivinv
            next 
            b(icol) = b(icol) * pivinv
            for ll = 1 to n
                if ll <> icol then
                    dum = a(ll, icol)
                    a(ll, icol) = 0
                    for l = 1 to n
                        a(ll, l) = a(ll, l) - a(icol, l) * dum
                    next 
                    b(ll) = b(ll) - b(icol) * dum
                end if
            next 
        next 
        for l = n to 1 step -1
            if indxr(l) <> indxc(l) then
                for k = 1 to n
                    dum = a(k, indxr(l))
                    a(k, indxr(l)) = a(k, indxc(l))
                    a(k, indxc(l)) = dum
                next 
            end if
        next 
    end sub
      

  9.   

    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <math.h>
    float *GauseSeidel(float *c,int n)
    {
    int i,j,k,t;
    float *x,p;
    x=(float *)malloc(n*sizeof(float));
    for(i=0;i<=n-2;i++)
    {
    k=i;
    for(j=i+1;j<=n-1;j++)
    if(fabs(*(c+j*(n+1)+i))>(fabs(*(c+k*(n+1)+i))))k=j;
    if(k!=i)
    for(j=i;j<=n;j++)
    {
    p=*(c+i*(n+1)+j);
    *(c+i*(n+1)+j)=*(c+k*(n+1)+j);
    *(c+k*(n+1)+j)=p;
    }
    for(j=i+1;j<=n-1;j++)
    {
    p=(*(c+j*(n+1)+i))/(*(c+i*(n+1)+i));
    for(t=i;t<=n;t++)
    *(c+j*(n+1)+t)-=p*(*(c+i*(n+1)+t));
    }
    }
    for(i=n-1;i>=0;i--)
    {
    for(j=n-1;j>=i+1;j--)
    (*(c+i*(n+1)+n))-=x[j]*(*(c+i*(n+1)+j));
    x[i]=*(c+i*(n+1)+n)/(*(c+i*(n+1)+i));
    }
    return x;}
    void main()
    {
    float *x;
    int i;
    float c[10][11]={ 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,   55,
          1,  2,  2,  2,  2,  2,  2,  2,  2,  2,  109,
          1,  2,  3,  3,  3,  3,  3,  3,  3,  3,  161,
      1,  2,  3,  4,  4,  4,  4,  4,  4,  4,  210,
      1,  2,  3,  4,  5,  5,  5,  5,  5,  5,  255,
      1,  2,  3,  4,  5,  6,  6,  6,  6,  6,  294,
      1,  2,  3,  4,  5,  6,  7,  7,  7,  7,  328,
      1,  2,  3,  4,  5,  6,  7,  8,  8,  8,  355,
      1,  2,  3,  4,  5,  6,  7,  8,  9,  9,  374,
      1,  2,  3,  4,  5,  6,  7,  8,  9, 10,  384};
    float *GauseSeidel(float *,int);
    x=GauseSeidel((float *)c,10);
    for(i=0;i<=9;i++)
    printf("x[%d]=%f\n",i,x[i]);
    getch();
    }
      

  10.   

    楼上,Matrix Class是要下载的吗?下载不了啊!!===================居然下载不了? 那你去我的Blog看看http://blog.csdn.net/LeoMaya/archive/2007/05/14/1608612.aspx
      

  11.   

    问题已解决。特别感谢LeoMaya。
      

  12.   

    又有新问题:double[,] DATA = { {0.02404,-0.0025,0,-0.001538,0 }, 
                                   { -0.0025,0.0135,-0.01,0,-0.001},
                                   {0,-0.01,-0.01756,-0.006452,-0.001105 },
                                   { -0.001538,0,-0.006452,0.009323,-0.001333 },
                                   {0,-0.01,-0.001105,-0.001333,0.01882}};
    Matrix COF = new Matrix(DATA);
    Matrix CON = new Matrix(5,1);
    CON[0, 0] = 1; 
    CON[1, 0] = 1; 
    CON[2, 0] = 1; 
    CON[3, 0] = -1; 
    CON[4, 0] = -25.384;
    richTextBox1.AppendText("Determinent of COF matrix = " +
    COF.Det().ToString() + " ");
    Matrix Sol = COF.Inverse() * CON;
    Sol.Display(this.richTextBox1);
    当我输入以上数组时,报错。在 public Matrix Inverse()
           {
                if ((this.IsSquare) && (!this.IsSingular))
               {
                    return new Matrix(INV(this.Data));
                }
                else
               {
                    throw new System.Exception(@"Cannot find inverse for non square /singular matrix");
                }
            }处报错为:
     未处理的“System.Exception”类型的异常出现在 WindowsApplication2.exe 中。其他信息: Cannot find inverse for non square /singular matrix