Question

Write a C++ code to solve linear system using triangular decomposition method. The user will enter the input which will be 3 lines, in each line 4 values.

Write a C++ code to solve linear system using triangular decomposition method.

The user will enter the input which will be 3 lines, in each line 4 values.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
/************** LU Decomposition for solving linear equations ***********/
#include<iostream.h>
#include<conio.h>
int main()
{
    int n,i,k,j,p;
    float a[10][10],l[10][10]={0},u[10][10]={0},sum,b[10],z[10]={0},x[10]={0};
    clrscr();
    cout<<"Enter the order of matrix ! ";
    cin>>n;
    cout<<"Enter all coefficients of matrix : ";
    for(i=1;i<=n;i++)
    {
        cout<<"\nRow "<<i<<"  ";
        for(j=1;j<=n;j++)
            cin>>a[i][j];
    }
    cout<<"Enter elements of b matrix"<<endl;
    for(i=1;i<=n;i++)
        cin>>b[i];
    //********** LU decomposition *****//
    for(k=1;k<=n;k++)
    {
        u[k][k]=1;
        for(i=k;i<=n;i++)
        {
            sum=0;
            for(p=1;p<=k-1;p++)
                sum+=l[i][p]*u[p][k];
            l[i][k]=a[i][k]-sum;
        }

        for(j=k+1;j<=n;j++)
        {
            sum=0;
            for(p=1;p<=k-1;p++)
                sum+=l[k][p]*u[p][j];
            u[k][j]=(a[k][j]-sum)/l[k][k];
        }
    }
    //******** Displaying LU matrix**********//
    cout<<endl<<endl<<"LU matrix is "<<endl;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
            cout<<l[i][j]<<"  ";
        cout<<endl;
    }
    cout<<endl;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
            cout<<u[i][j]<<"  ";
        cout<<endl;
    }

    //***** FINDING Z; LZ=b*********//

    for(i=1;i<=n;i++)
    {                                        //forward subtitution method
        sum=0;
        for(p=1;p<i;p++)
        sum+=l[i][p]*z[p];
        z[i]=(b[i]-sum)/l[i][i];
    }
    //********** FINDING X; UX=Z***********//

    for(i=n;i>0;i--)
    {
        sum=0;
        for(p=n;p>i;p--)
            sum+=u[i][p]*x[p];
        x[i]=(z[i]-sum)/u[i][i];
    }
    //*********** DISPLAYING SOLUTION**************//
    cout<<endl<<"Set of solution is"<<endl;
    for(i=1;i<=n;i++)
        cout<<endl<<x[i];

    getch();
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
Write a C++ code to solve linear system using triangular decomposition method. The user will enter the input which will be 3 lines, in each line 4 values.
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
  • LU Decomposition Gauss Method EX4: Solve the same problem using the Gauss method. Example 4-6: MATLAB...

    LU Decomposition Gauss Method EX4: Solve the same problem using the Gauss method. Example 4-6: MATLAB user-defined function for solving a system of equations using LU decomposition with Crout's method. ( Y Suggestions Use the code from the Crout's method. Discard the LUdecomp Crout module and leave the rest. Modify Gauss Pivot to store all the ratios Create the lower triangular matrix Confirm that L.U = A. Solve the problem by the LU double substitution Determine the currents ij, in,...

  • Using C# Create a “Main” method that will take user input one line at a time...

    Using C# Create a “Main” method that will take user input one line at a time until they enter the phrase “done”. Store each line entered into an ArrayList, so that one element of the ArrayList is one line of user input. You do NOT need to write your own ArrayList class, please use the standard library implementation of an ArrayList. After the user finishes typing in input, ask the user for a location and file name. Save the contents...

  • I want to change this C code so that instead of prompting the user to enter...

    I want to change this C code so that instead of prompting the user to enter the argument value, or requiring the argument be entered after by pressing the “enter” key, instead I want the code changed so that the program opens a file, (which include the input values) read in the elements and pass in the input file as a command line argument. (the input file includes the size of the matrix and the matrix values) Here's an example...

  • Take any system of 3x3 matrix equation which is linear Solve the system using generic algorithm...

    Take any system of 3x3 matrix equation which is linear Solve the system using generic algorithm for the Gauss Seidel method The code must take the matrix input by the user It should be solvable for any nxn system Give only a working proper MATLAB code if you dont know do not answer or i will dislike badly compare with original solution and then break.

  • Write a main method that will request the user to enter Strings using a JOptionPane input...

    Write a main method that will request the user to enter Strings using a JOptionPane input dialog. The method should continue accepting strings until the user types “STOP”. Then, using a JOptionPane message dialog, tell the user how many of the strings contained only letters. Hint: there is a method isLetter in the wrapper class Character. in JAVA

  • Q.1 Using the method of Triangular Decomposition solve the set of equations. Xı - 2x2 +...

    Q.1 Using the method of Triangular Decomposition solve the set of equations. Xı - 2x2 + 3x3 - X4 = -3 3x1 + x2-3x3 +2x4 = 14 5xi +3x2+2x3 + 3x4 = 21 2x1 - 4x2 – 2x3 + 4x4 = -10 If Ax = 2x, determine the eigenvalues and corresponding eigenvectors of -3 0 6 4 10 - 8 A 4 5 3 B= 1 2 1 1 2 1 -1 2 3 Q.2

  • Python Write a program which asks the user keep inputting values on a line followed by...

    Python Write a program which asks the user keep inputting values on a line followed by enter until they enter a blank line, at which point the program should print the number of lines entered (excluding the blank).

  • 1. Write a code snippet Write a code snippet that asks the user to enter a...

    1. Write a code snippet Write a code snippet that asks the user to enter a name and a major one at a time and creates a dictionary with keys corresponding to names and values corresponding to majors. Assume that the user enters “alice”, “bio”, “mark”, “engineering”. >>>d = {} >>> your code starts here 2.Based on your code above: >>> print(d) 3.Write a code snippet Write a code snippet that creates a dictionary (records) using keys from a frozenset...

  • Using MATLAB Write a MATLA code to solve the following system of linear equations: -x +...

    Using MATLAB Write a MATLA code to solve the following system of linear equations: -x + y + 4z = -2 20y + 3x - 1z = 3 3z + 2y + 6z - 3 = 0

  • Write C code to repeatedly ask the user for a number, then once the user enters...

    Write C code to repeatedly ask the user for a number, then once the user enters 0, the code displays the min, max and average of all values entered. To get proper credit, name the variables as listed below and follow the directions carefully. Do not use any break or similar type of statements. To implement the above code, you will need to count the entries - name the counter variable num_count. You will also need to use a variable,...

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