Sistem Persamaan Linier dengan Eliminasi GAUSS

3/31/2014 07:30:00 PM Salman 0 Comments

1. Membuat program dalam matlab untuk sistem persamaan linier dengan
    eliminasi GAUSS

function x=gaussian(A,b);
n=length(b);        
x=zeros(n,1);
for k=1:n-1
for i=k+1:n
m=A(i,k)/A(k,k);
for j=k:n
A(i,j)=A(i,j)-m*A(k,j);
end
b(i)=b(i)-m*b(k);
end
end
%solusi
x(n)=b(n)/A(n,n);
for i=n-1:-1:1
for j=i+1:n
b(i)=b(i)-A(i,j)*x(j);
end
x(i)=b(i)/A(i,i);
end

2.     Membuat Program dalam Java/C++/C#/PHP untuk sistem persamaan linier dengan eliminasi gauss

/*************** Gauss Jordan method ********************/
#include<iostream.h>
#include<conio.h>
int main()
{
    int i,j,k,n;
    float a[10][10],d;
    clrscr();

    cout<<"No of equations ? ";cin>>n;
    cout<<"Read all coefficients of matrix with b matrix too "<<endl;
    for(i=1;i<=n;i++) // read nxn matrix - cofficients
        for(j=1;j<=n+1;j++)
            cin>>a[i][j];

    /************** partial pivoting **************/
    for(i=n;i>1;i--)
    {
        if(a[i-1][1]<a[i][1])
        for(j=1;j<=n+1;j++)
        {
            d=a[i][j];
            a[i][j]=a[i-1][j];
            a[i-1][j]=d;
        }
    }
    cout<<"pivoted output: "<<endl;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n+1;j++)
            cout<<a[i][j]<<"    ";
        cout<<endl;
    }
    /********** reducing to diagonal  matrix ***********/

    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        if(j!=i)
        {
            d=a[j][i]/a[i][i];
            for(k=1;k<=n+1;k++)
                a[j][k]-=a[i][k]*d;
        }
    }
    /************** reducing to unit matrix *************/
    for(i=1;i<=n;i++)
    {
    d=a[i][i];
        for(j=1;j<=n+1;j++)
            a[i][j]=a[i][j]/d;
    }


    cout<<"your solutions: "<<endl;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n+1;j++)
            cout<<a[i][j]<<"    ";
        cout<<endl;
    }

    getch();
    return 0;

}

You Might Also Like

0 komentar: