Question

1. Write a program to do the Gauss Elimination procedure for a set of simultaneous linear equations. The input is a comma separated file of numbers. Write the program in the following phases. Ensure each phase runs correctly before you proceed to the next phase. a. Write a computer program to open a.csv file, and read the contents an augmented matrix of n columns and n +1 rows. The program should display the matrix a on the screen. b. Write a program to do the forward elimination for the Gauss Elimination process. You must exchange equations so that the equation with the largest element is the pivot row. Then you must complete the forward elimination by subtracting the pivot equation from the remaining equations so that the elements in the pivot column below the pivot element are all zero. Display the triangulated augmented matrix a. . Y c. Write a program to do the complete Gauss Elimination process must do the back substitution operation from the previous step. Print the array of unknown x.

Solve using visual basic, c#, or c++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution :-

Program for to do gauss Elimination for a set of simultaneous linear eqations......

import java.util.Scanner;


/* Class GaussianElimination*/
public class GaussianElimination
{
public void solve(double[][] A, double[] B)
{

int N = B.length;
for (int k = 0; k < N; k++)
{
/* find pivot row */
int max = k;
for (int i = k + 1; i < N; i++)
if (Math.abs(A[i][k]) > Math.abs(A[max][k]))
max = i;


/* swap row in A matrix */
double[] temp = A[k];
A[k] = A[max];
A[max] = temp;


/* swap corresponding values in constants matrix*/
double t = B[k];
B[k] = B[max];
B[max] = t;


/* pivot within A and B*/
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];
}
}


/* Print row echelon form*/
printRowEchelonForm(A, B);


/* back substitution*/
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];
}
/*print solution */
printSolution(solution);
}
/* funtion to print in row echelon form */
public void printRowEchelonForm(double[][] A, double[] B)
{
int N = B.length;
System.out.println("\nRow Echelon form : ");
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
System.out.printf("%.3f ", A[i][j]);
System.out.printf("| %.3f\n", B[i]);
}
System.out.println();
}
/* function to print 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();
}
/* main function*/
public static void main (String[] args)
{

Scanner scan = new Scanner(System.in);
System.out.println("Gaussian Elimination Algorithm Test\n");
/* make an object of gaussianElimination class */
GaussianElimination ge = new GaussianElimination();


System.out.println("\nEnter number of variables");
int N = scan.nextInt();


double[] B = new double[N];
double[][] A = new double[N][N];


System.out.println("\nEnter "+ N +" equations coefficients ");
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
A[i][j] = scan.nextDouble();


System.out.println("\nEnter "+ N +" solutions");
for (int i = 0; i < N; i++)
B[i] = scan.nextDouble();

ge.solve(A,B);

}
}

Add a comment
Know the answer?
Add Answer to:
Solve using visual basic, c#, or c++ 1. Write a program to do the Gauss Elimination...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT