Computer Aided Analysis Program Print

download Computer Aided Analysis Program Print

of 30

Transcript of Computer Aided Analysis Program Print

  • 8/10/2019 Computer Aided Analysis Program Print

    1/30

    B ALIUAG UNIVERSITYBaliwag, Bulacan

    COLLEGE OF ENVIRONMENTAL DESIGN AND

    ENGINEERING

    CE 520

    Computer Aided Analysis

    COMPILATION OF PROGRAMSMARCH 17, 2014

    Submitted by:Viola, Randy V.BSCE-V

    Submitted to:Engr. Leonardo V. Surio, M. Eng.

  • 8/10/2019 Computer Aided Analysis Program Print

    2/30

    I. Preparation of Computer Program for :

    A. Matrix AdditionB. Matrix MultiplicationC. Inverse of Matrix

    D. Gauss Elimination to Solve System of Equations.

    (Note: These programs of matrix are made using Microsoft Visual (Basic, C++)2010 express. It runs only if there are installed Microsoft Visual Basic 2010express in your computer).

    A. Matrix Addition

    In this program we compute addition of two matrices A and B which hassame order. First we enter order of matrix then enter numbers for matrix A and B.

    After that gives resultant matrix.Suppose matrices have order 33.

    Matrix A:

    4 6 51 3 12 3 7

    Matrix B:

    9 2 38 0 64 1 2

    Resultant Matrix is addition of matrices A and B:

    13 8 89 3 7

    6 4 9

  • 8/10/2019 Computer Aided Analysis Program Print

    3/30

    Program code for Matrix Addition:

    #include#include using namespace std;

    int main(){int a[10][10];int b[10][10];int x,y,i,j;

    printf("\t\t\t MATRIX ADDITION\n");printf(" \n");

    coutx>>y;

    cout

  • 8/10/2019 Computer Aided Analysis Program Print

    4/30

    }cout

  • 8/10/2019 Computer Aided Analysis Program Print

    5/30

    OUTPUT:

  • 8/10/2019 Computer Aided Analysis Program Print

    6/30

    B. Matrix Multiplication

    It is a Matrix multiplication in Microsoft Visual Basic using 2010 express. A programto multiply matrices (two dimensional array), this program multiplies two matrices which

    will be entered by the user. Firstly user will enter the order of a matrix. If the enteredorders of two matrix is such that they can't be multiplied then an error message isdisplayed on the screen.

    Suppose matrices have order 33.

    Matrix A : :

    1 2 00 1 1

    2 0 1

    Matrix B : :

    1 1 22 1 11 2 1

    Product of Matrix A & Matrix B is : :

    5 3 43 3 23 4 5

  • 8/10/2019 Computer Aided Analysis Program Print

    7/30

    Program code for Matrix Multiplication:

    Module Module1

    Sub Main()

    Dim matrix1 As Integer(,), matrix2 As Integer(,)

    Dim row1 As Integer, col1 As Integer, row2 As Integer, col2 As Integer, i AsInteger, j As Integer, _

    k As Integer, temp As Integer = 0

    Console.WriteLine(" MATRIX MULTIPLICATION ")

    Console.WriteLine(" ")

    Console.WriteLine("Enter the number of rows in matrix A :: ")

    row1 = Integer.Parse(Console.ReadLine())

    Console.WriteLine("Enter the number of columns in matrix A :: ")

    col1 = Integer.Parse(Console.ReadLine())

    matrix1 = New Integer(row1 - 1, col1 - 1) {}

    Console.WriteLine("Enter the number of rows in matrix B :: ")

    row2 = Integer.Parse(Console.ReadLine())

    Console.WriteLine("Enter the number of columns in matrix B :: ")

    col2 = Integer.Parse(Console.ReadLine())

    matrix2 = New Integer(row2 - 1, col2 - 1) {}

    If col1 row2 Then

    Console.WriteLine("Multiplication is not applicable!!!")

    Console.WriteLine("Note : Number of columns of matrix A must be equal toNumber of rows of matrix B.")

    Else

  • 8/10/2019 Computer Aided Analysis Program Print

    8/30

    Console.WriteLine("Enter Elements for matrix A")

    For i = 0 To row1 - 1

    For j = 0 To col1 - 1

    matrix1(i, j) = Integer.Parse(Console.ReadLine())

    Next

    Next

    Console.WriteLine("Enter Elements for matrix B")

    For i = 0 To row2 - 1

    For j = 0 To col2 - 1

    matrix2(i, j) = Integer.Parse(Console.ReadLine())

    Next

    Next

    Console.Clear()

    Console.WriteLine("You have entered :: ")

    Console.WriteLine("Matrix A ::")

    For i = 0 To row1 - 1

    For j = 0 To col1 - 1

    Console.Write(matrix1(i, j))

    Console.Write(" ")

    Next

    Console.WriteLine()

    Next

    Console.WriteLine("Matrix B ::")

  • 8/10/2019 Computer Aided Analysis Program Print

    9/30

    For i = 0 To row2 - 1

    For j = 0 To col2 - 1

    Console.Write(matrix2(i, j))

    Console.Write(" ")

    Next

    Console.WriteLine()

    Next

    Console.WriteLine("Product of Matrix A & Matrix B is ::")

    For i = 0 To row1 - 1

    For j = 0 To col2 - 1

    For k = 0 To row2 - 1

    temp = temp + (matrix1(i, k) * matrix2(k, j))

    Next

    Console.Write(temp & " ")

    temp = 0

    Next

    Console.WriteLine()

    Next

    End If

    Console.Read()

    End Sub

    End Module

  • 8/10/2019 Computer Aided Analysis Program Print

    10/30

    INPUT:

    OUTPUT:

  • 8/10/2019 Computer Aided Analysis Program Print

    11/30

    C. Inverse of Matrix

    Given a matrix A, the inverse A -1 can be multiplied on either side of A to get theidentity. That is, AA 1 = A 1 A = I. Keeping in mind the rules for matrix multiplication, thissays that A must have the same number of rows and columns; that is, A must be

    square. (Otherwise, the multiplication wouldn't work.) If the matrix isn't square, it cannothave a (properly two-sided) inverse. However, while all invertible matrices are square,not all square matrices are invertible.

    Suppose matrices have order 33.

    Matrix A :

    2 1 30 0 2

    4 1 1

    The Inverse of Matrix (A -1) :

    -0.5 0.5 0.52 -2.5 -10 0.5 0

    Program Code for Inverse of Matrix:

    #include#include using namespace std;int main(){

    int i,j,k,n;float a[10][10]={0},d;void clrscr();

    cout

  • 8/10/2019 Computer Aided Analysis Program Print

    12/30

    for(j=1;j1;i--){if(a[i-1][1]

  • 8/10/2019 Computer Aided Analysis Program Print

    13/30

    for(j=n+1;j

  • 8/10/2019 Computer Aided Analysis Program Print

    14/30

    D. Gauss Elimination to Solve System of Equations.

    Gaussian elimination (also known as row reduction) is an algorithm forsolving systems of linear equations. It is usually understood as a sequence ofoperations performed on the associated matrix of coefficients. This method can also be

    used to find the rank of a matrix, to calculate the determinant of a matrix, and tocalculate the inverse of an invertible square matrix. The method is named after CarlFriedrich Gauss.

    Suppose matrices have 3 numbers of equations.

    3x1 + 6x 2 + 1x 3 = 162x1 + 4x 2 + 3x 3 = 131x1 + 3x 2 + 2x 3 = 9

    Matrix :

    3 6 1 : 162 4 3 : 131 3 2 : 9

    The result is :

    x1 = 1.00x2 = 2.00x3 = 1.00

    Program Code for Gauss Elimination to Solve System of Equations:

    # include# include# include using namespace std;void main(){int i,j,k,n;float a[10][10],x[10];float s,p;printf("\t\t\t GAUSS ELIMINATION \n");printf(" \n");printf("Enter the number of equations : ");scanf("%d",&n) ;

    http://en.wikipedia.org/wiki/Algorithmhttp://en.wikipedia.org/wiki/System_of_linear_equationshttp://en.wikipedia.org/wiki/Matrix_(mathematics)http://en.wikipedia.org/wiki/Rank_(linear_algebra)http://en.wikipedia.org/wiki/Determinanthttp://en.wikipedia.org/wiki/Invertible_matrixhttp://en.wikipedia.org/wiki/Carl_Friedrich_Gausshttp://en.wikipedia.org/wiki/Carl_Friedrich_Gausshttp://en.wikipedia.org/wiki/Carl_Friedrich_Gausshttp://en.wikipedia.org/wiki/Carl_Friedrich_Gausshttp://en.wikipedia.org/wiki/Invertible_matrixhttp://en.wikipedia.org/wiki/Determinanthttp://en.wikipedia.org/wiki/Rank_(linear_algebra)http://en.wikipedia.org/wiki/Matrix_(mathematics)http://en.wikipedia.org/wiki/System_of_linear_equationshttp://en.wikipedia.org/wiki/Algorithm
  • 8/10/2019 Computer Aided Analysis Program Print

    15/30

    printf("\nEnter the co-efficients of the equations :\n\n");for(i=0;i

  • 8/10/2019 Computer Aided Analysis Program Print

    16/30

    for(i=0;i

  • 8/10/2019 Computer Aided Analysis Program Print

    17/30

    II. Areas for Plane Figures:

    A. SquareB. RectangleC. Trapezoid

    D. CircleE. Triangle

    Areas of Plane Figures:

    (Note: This program name AREA CALCULATOR runs only if there are installedMicrosoft Visual Basic 2010 express in your computer.)

    It is a calculator for finding areas of a plane figures only. There are square,Rectangle, Trapezoid, Circle and Triangle in the program. Just click the icon or buttonon the menu that you want to execute in finding the area of the given plane figures.

    A. Square

    The area of a square is given by the formula area = wi dth heig ht, Butsince the width and height are by definition the same, the formula is usuallywritten as area = s 2 where s is the length of one side.

    For example:Length = 6m its width is also 6m.

    Area = 36 m 2

  • 8/10/2019 Computer Aided Analysis Program Print

    18/30

    OUTPUT:

    Program Code for Square:

    Public Class square

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button1.Click

    TextBox3.Text = TextBox1.Text * TextBox2.Text

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button2.Click

    TextBox1.Text = ""TextBox2.Text = ""

    TextBox3.Text = ""End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs)

    Form1.Show()End Sub

  • 8/10/2019 Computer Aided Analysis Program Print

    19/30

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles TextBox1.TextChanged

    End SubEnd Class

    B. Rectangle

    To find the area of a rectangle, multiply the length by the width. Theformula is: , where is the area, is the length, is the width,and means multiply.

    For example:Length = 6mWidth = 4m.

    Answer is: Area = 24 m 2

    OUTPUT:

    http://popupwindow%28%27rectangle%27%29/http://popupwindow%28%27rectangle%27%29/
  • 8/10/2019 Computer Aided Analysis Program Print

    20/30

    Program Code for Rectangle:

    Public Class rectangle

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As

    System.EventArgs) Handles Button1.ClickTextBox3.Text = TextBox1.Text * TextBox2.Text

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button2.Click

    TextBox1.Text = ""TextBox2.Text = ""TextBox3.Text = ""

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs)

    Form1.Show()End Sub

    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles TextBox3.TextChanged

    End SubEnd Class

    C. Trapezoid

    A trapezoid is a 4-sided figure with one pair of parallel sides. To find thearea of a trapezoid, take the sum of its bases, multiply the sum by the height ofthe trapezoid, and then divide the result by 2. The formula for the area of atrapezoid is:

    Area = ( ) For example:

    Top Base = 6mBottom Base = 8mHeight = 4m

    Answer is:

    Area = 28 m 2

    http://popupwindow%28%27parallel%27%29/http://popupwindow%28%27parallel%27%29/http://popupwindow%28%27parallel%27%29/
  • 8/10/2019 Computer Aided Analysis Program Print

    21/30

    OUTPUT:

    Program Code for Trapezoid:

    Public Class trapezoid

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles TextBox2.TextChanged

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button1.Click

    TextBox4.Text = ((((Val(TextBox1.Text) + Val(TextBox2.Text)) * TextBox3.Text)) *0.5)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button2.Click

    TextBox1.Text = ""TextBox2.Text = ""TextBox3.Text = ""TextBox4.Text = ""

    End Sub

    Private Sub Label3_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Label3.Click

  • 8/10/2019 Computer Aided Analysis Program Print

    22/30

    End Sub

    Private Sub Label4_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Label4.Click

    End SubPrivate Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As

    System.EventArgs) Handles TextBox4.TextChanged

    End SubEnd Class

    D. Circle

    The area of a circle can be found by multiplying pi ( = 3.14) by the

    square of the radius If a circle has a radius of 4, its area is 3.14*4*4=50.24 If youknow the diameter, the radius is 1/2 as large.

    For example:

    Radius: 6m

    Answer is:

    Area = 113.0973 m 2

    OUTPUT:

  • 8/10/2019 Computer Aided Analysis Program Print

    23/30

    Program Code for Circle:

    Public Class circlePrivate Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As

    System.EventArgs)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button1.Click

    TextBox3.Text = 3.141592654 * TextBox1.Text * TextBox1.TextEnd Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button2.Click

    TextBox1.Text = ""TextBox3.Text = ""

    End Sub

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Label1.Click

    End Sub

    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles TextBox3.TextChanged

    End SubEnd Class

    E. Triangle

    It is easy to find the area of a right-angled triangle, or any triangle wherewe are given the base and the height.

    It is simply half of b times Area = bxh

    For example:

    Given:

    Height (h) = 6m

    Base (b) = 3m

    Answer is: Area = 9m 2

  • 8/10/2019 Computer Aided Analysis Program Print

    24/30

    OUTPUT:

    Program Code for Triangle:

    Public Class triangle

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e AsSystem.EventArgs)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button1.Click

    TextBox3.Text = (TextBox1.Text * TextBox2.Text) / 2End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As

    System.EventArgs) Handles Button2.ClickTextBox1.Text = ""TextBox2.Text = ""TextBox3.Text = ""

    End Sub

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Label1.Click

  • 8/10/2019 Computer Aided Analysis Program Print

    25/30

    End Sub

    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles TextBox3.TextChanged

    End SubEnd Class

    III. Solution of Quadratic Equation:

    (Note: This program name Quadratic Equation Solver runs only if there are installedMicrosoft Visual Basic 2010 express in your computer.)

    A quadratic equation is an equation of the second degree, meaningit contains at least one term that is squared. The standard form is ax + bx + c = 0with a, b, and c being constants, or numerical coefficients, and x is an unknownvariable. One absolute rule is that the first constant a cannot be a zero.

    http://www.yourdictionary.com/quadratichttp://www.yourdictionary.com/quadratic
  • 8/10/2019 Computer Aided Analysis Program Print

    26/30

    For Example number 1:Find the roots of the given equation:x + -3x + -4 = 0

    Answer is:x1 = 4 and x 2 =-1

    *there two possible answer for the given equation.For Example 2:

    Find the roots of the given equation:5x + -3x + 3 = 0

    Answer is:x1 = Imaginary and x 2 = Imaginary

    *the answer is both imaginary.

    OUTPUT:

  • 8/10/2019 Computer Aided Analysis Program Print

    27/30

    Program Code for the Solution of Quadratic Equation:

    Public Class Form1

    Dim a, b, c, d, x, y, z, real, img, ePrivate Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles

    Label1.ClickEndEnd Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesButton2.Click

    TextBox1.Text = ""TextBox2.Text = ""TextBox3.Text = ""TextBox4.Text = ""TextBox5.Text = ""

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles

    Button1.ClickIf TextBox1.Text = "" ThenExit Sub

    Elsea = Textbox1.textb = Textbox2.textc = Textbox3.textd = (b ^ 2 - 4 * a * c)If d > 0 Then

    y = ((-b + System.Math.Sqrt(d)) / (2 * a))z = ((-b - System.Math.Sqrt(d)) / (2 * a))TextBox4.Text = yTextBox5.Text = z

    ElseIf d = 0 Then

    x = -b / (2 * a)TextBox4.Text = xElseif d < 0 Then

    TextBox4.Text = "IMAGINARY"TextBox5.Text = "IMAGINARY"

    End IfEnd If

    End SubPrivate Function MsgBox() As String

    Throw New NotImplementedExceptionEnd Function

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesPictureBox1.Click

    End Sub

    Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles

    TextBox4.TextChanged

    End Sub

    End Class

  • 8/10/2019 Computer Aided Analysis Program Print

    28/30

    III. CHOLESKY METHOD

    Suppose matrices have 3 numbers of equations.

    5x1 +2 + 1x 3 = 3

    4x1 + 1x 2 + -1x 3 = -3-2x 1 + 3x 2 + -3x 3 = 6

    Matrix A:

    5 2 14 1 -1-2 3 -3

    Matrix b:

    3 -3 6

    The solution using Cholesky Method is:

    X1 = -1.00X2 = 3.00X3 = 2.00

  • 8/10/2019 Computer Aided Analysis Program Print

    29/30

    OUTPUT:

    Program Code for Cholesky Method:

    #include#includeusing namespace std;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};void clrscr();

    cout

  • 8/10/2019 Computer Aided Analysis Program Print

    30/30

    for(i=k;i