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...

  • 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.

  • 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...

  • 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

  • Write a program that allows the user to navigate the lines of text in a file....

    Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program...

  • 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

  • 1) Write Single-line comments for each code in programming C++ 2) Prompt the user to enter...

    1) Write Single-line comments for each code in programming C++ 2) Prompt the user to enter a number from 1 to 100, inclusive. If the number is not valid, meaning out of range, then indicate that the number is not valid and that they should try again. This program should test for negative numbers, which are not valid. Prompt the user to indicate that the number must be greater than 0 and that they should try again. If the number...

  • 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).

  • Write an interactive C++ program that asks a user to input the color code of a...

    Write an interactive C++ program that asks a user to input the color code of a resistor and determines its value and tolerance Tell the user how to input the color rings “Instructions” Tell the user to input the colors one after the other After the program receives the information it will display the resistance and tolerance Output an error message if the user enters an invalid color code (no garbage values should be displayed), and ask for another input...

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