O’ZBEKISTON RESPUBLIKASI AXBOROT TEXNOLOGIYALARI VA TELEKOMMUNIKATSIYALARINI RIVOJLANTIRISH VAZIRLIGI
MUHAMMAD AL-XORAZMIY NOMIDAGI TOSHKENT AXBOROT TEXNOLOGIYALARI UNIVERSITETI
Labaratoriya ishi
Bajardi:To`rayev Davron
Tekshirdi: Ergashev Sirojiddin
Toshkent 2019
Gauss usuli dastur kodi:
package com.company;
import java.util.Scanner;
class Gauss_usuli
{
public void solve(double[][] A, double[] B)
{
int N = B.length;
for (int k = 0; k < N; k++)
{
int max = k;
for (int i = k + 1; i < N; i++)
if (Math.abs(A[i][k]) > Math.abs(A[max][k]))
max = i;
double[] temp = A[k];
A[k] = A[max];
A[max] = temp;
double t = B[k];
B[k] = B[max];
B[max] = t;
for (int i = k + 1; i < N; i++)
{
double factor = A[i][k] / A[k][k];
B[i] -= factor * B[k];
for (int j = k; j < N; j++)
A[i][j] -= factor * A[k][j];
}
}
double[] solution = new double[N];
for (int i = N - 1; i >= 0; i--)
{
double sum = 0.0;
for (int j = i + 1; j < N; j++)
sum += A[i][j] * solution[j];
solution[i] = (B[i] - sum) / A[i][i];
}
printSolution(solution);
}
public void printSolution(double[] sol)
{
int N = sol.length;
System.out.println("\nSolution : ");
for (int i = 0; i < N; i++)
System.out.printf("%.3f ", sol[i]);
System.out.println();
}
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Gaussian Elimination Algorithm Test\n");
Gauss_usuli ge = new Gauss_usuli();
System.out.print("\n Matritsa o`lchami: ");
int N = scan.nextInt();
double[] B = new double[N];
double[][] A = new double[N][N];
System.out.println("\n "+ N +"o`lchamli matritsa koeffitsiyentlari ");
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
A[i][j] = scan.nextDouble();
System.out.println("\n "+ N +"o`lchamli tenglamalar javoblari");
for (int i = 0; i < N; i++)
B[i] = scan.nextDouble();
ge.solve(A,B); } }
Gauss usuli dastur natijasi:
Kramer usuli dastur kodi:
package com.company;
import java.util.Scanner.*;
import java.util.*;
class Kramer_usuli
{
double A[][];
double m[][];
int N;
double B[];
public void input()
{
Scanner s=new Scanner(System.in);
System.out.println("Matritsa o`lchamini kiriting: ");
N=s.nextInt();
A = new double[N][];
for(int i=0;i;i++)
{
A[i]=new double[N];
}
System.out.println("Tenglama koeffitsiyentlarini kiritish");
for(int i=0;i;i++)
{
System.out.println("Tenglama koeffitsiyentlarini kiriting: " + (i+1));
for(int j=0;j;j++)
{
double k=s.nextDouble();
A[i][j]=k;
}
}
B = new double[N];
System.out.println("Tenglamalar javobini kiritish");
for(int i=0;i;i++)
{
System.out.println("Tenglama javobini kiriting: " + (i+1));
double k = s.nextDouble();
B[i] = k;
}
}
public double determinant(double A[][],int N)
{
double det=0;
double res;
if(N == 1)
res = A[0][0];
else if (N == 2)
{
res = A[0][0]*A[1][1] - A[1][0]*A[0][1];
}
else
{
res=0;
for(int j1=0;j1;j1++)
{
m = new double[N-1][];
for(int k=0;k<(N-1);k++)
m[k] = new double[N-1];
for(int i=1;i;i++)
{
int j2=0;
for(int j=0;j;j++)
{
if(j == j1)
continue;
m[i-1][j2] = A[i][j];
j2++;
}
}
res += Math.pow(-1.0,1.0+j1+1.0)* A[0][j1] * determinant(m,N-1);
}
}
return res;
}
public double[] cramers(double A[][],double B[])
{
double temp[][] = new double[N][N];
double x[] = new double[N];
for(int i=0;i;i++)
{
for(int j=0;j;j++){
for(int k=0;k;k++){
if(k == i)
temp[j][k] = B[j];
else
temp[j][k] = A[j][k];
}}
x[i]=determinant(temp,N)/determinant(A,N);
}
for(int i=0;i;i++){
System.out.println(x[i]);}
return x;
}
public static void main(String args[]){
double res;
Kramer_usuli d = new Kramer_usuli();
d.input();
d.cramers(d.A,d.B);} }
Kramer usuli dastur natijasi:
Do'stlaringiz bilan baham: |