You are on page 1of 30

BALIUAG UNIVERSITY

Baliwag, Bulacan
COLLEGE OF ENVIRONMENTAL DESIGN AND
ENGINEERING

CE 520
Computer Aided Analysis

COMPILATION OF PROGRAMS
MARCH 17, 2014

Submitted by:
Viola, Randy V.
BSCE-V

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

I. Preparation of Computer Program for:


A.
B.
C.
D.

Matrix Addition
Matrix Multiplication
Inverse of Matrix
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 2010
express in your computer).

A. Matrix Addition
In this program we compute addition of two matrices A and B which has
same 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
1
2

6
3
3

5
1
7

Matrix B:
9
8
4

2
0
1

3
6
2

Resultant Matrix is addition of matrices A and B:


13
9
6

8
3
4

8
7
9

Program code for Matrix Addition:


#include<iostream>
#include <conio.h>
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");
cout<<"\nEnter the number of rows and columns :::\n\n";
cin>>x>>y;
cout<<"\n\nEnter elements for Matrix A :::\n\n";
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
cin>>a[i][j];
}
cout<<"\n";
}
cout<<"\n\nEnter elements for Matrix B :::\n\n";
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
cin>>b[i][j];
}
cout<<"\n";
}

cout<<"\n\nMatrix A :\n\n";
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
cout<<"\t"<<a[i][j];

}
cout<<"\n\n";
}
cout<<"\n\nMatrix B :\n\n";
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
cout<<"\t"<<b[i][j];
}
cout<<"\n\n";
}

cout<<"\n\nAddition of Matrix A and Matrix B :\n\n";


for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
cout<<"\t"<<a[i][j]+b[i][j];
}
cout<<"\n\n";
}
_getch();
return 0;
}

OUTPUT:

B. Matrix Multiplication

It is a Matrix multiplication in Microsoft Visual Basic using 2010 express. A program


to 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 entered
orders of two matrix is such that they can't be multiplied then an error message is
displayed on the screen.
Suppose matrices have order 33.
Matrix A : :
1
0
2

2
1
0

0
1
1

Matrix B : :
1
2
1

1
1
2

2
1
1

Product of Matrix A & Matrix B is : :


5
3
3

3
3
4

4
2
5

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 As
Integer, 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 to
Number of rows of matrix B.")
Else

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 ::")

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

INPUT:

OUTPUT:

C. Inverse of Matrix
Given a matrix A, the inverse A-1 can be multiplied on either side of A to get the
identity. That is, AA1 = A1A = I. Keeping in mind the rules for matrix multiplication, this
says 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 cannot
have 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
0
4

1
0
1

3
2
1

The Inverse of Matrix (A-1) :


-0.5
2
0

0.5
-2.5
0.5

0.5
-1
0

Program Code for Inverse of Matrix:


#include<iostream>
#include <conio.h>
using namespace std;
int main()
{
int i,j,k,n;
float a[10][10]={0},d;
void clrscr();
cout<<"\t\t\t INVERSE OF MATRIX \n"<<endl;
cout<<"
<<---------------------------------------->>
cout<<"
"<<endl;
cout<<"Enter the order of the Matrix :"; cin>>n;
cout<<"Enter the elements of the matrix :"<<endl;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
cin>>a[i][j];
for(i=1;i<=n;i++)

"<<endl;

for(j=1;j<=2*n;j++)
if(j==(i+n))
a[i][j]=1;

for(i=n;i>1;i--)
{
if(a[i-1][1]<a[i][1])
for(j=1;j<=n*2;j++)
{
d=a[i][j];
a[i][j]=a[i-1][j];
a[i-1][j]=d;
}
}
cout<<"pivoted output :"<<endl;
for(i=1;i<=n;i++)
{
for(j=1;j<=n*2;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}

for(i=1;i<=n;i++)
{
for(j=1;j<=n*2;j++)
if(j!=i)
{
d=a[j][i]/a[i][i];
for(k=1;k<=n*2;k++)
a[j][k]-=a[i][k]*d;
}
}
for(i=1;i<=n;i++)
{
d=a[i][i];
for(j=1;j<=n*2;j++)
a[i][j]=a[i][j]/d;
}

cout<<"The inverse of matrix is :"<<endl;


for(i=1;i<=n;i++)
{

for(j=n+1;j<=n*2;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
getch();
return 0;
}

OUTPUT:

D. Gauss Elimination to Solve System of Equations.


Gaussian elimination (also known as row reduction) is an algorithm for
solving systems of linear equations. It is usually understood as a sequence of
operations 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 to
calculate the inverse of an invertible square matrix. The method is named after Carl
Friedrich Gauss.
Suppose matrices have 3 numbers of equations.
3x1 + 6x2 + 1x3 = 16
2x1 + 4x2 + 3x3 = 13
1x1 + 3x2 + 2x3 = 9
Matrix :
3
2
1

6
4
3

1
3
2

:
:
:

16
13
9

The result is :
x1 = 1.00
x2 = 2.00
x3 = 1.00

Program Code for Gauss Elimination to Solve System of Equations:


# include<stdio.h>
# include<conio.h>
# include <iostream>
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("
<<---------------------------------------->>
printf("Enter the number of equations : ");
scanf("%d",&n) ;

\n");

printf("\nEnter the co-efficients of the equations :\n\n");


for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("a[%d][%d]= ",i+1,j+1);
scanf("%f",&a[i][j]);
}
printf("d[%d]= ",i+1);
scanf("%f",&a[i][n]);
}
for(k=0;k<n-1;k++)
{
for(i=k+1;i<n;i++)
{
p = a[i][k] / a[k][k] ;
for(j=k;j<=n+1;j++)
a[i][j]=a[i][j]-p*a[k][j];
}
}
printf("Eliminated matrix as : \n");
for(j=0;j<n;j++)
{
for(i=0;i<=n;i++)
printf("\t%.2f",a[j][i]);
printf("\n");
}
for(j=0;j<=n;j++)
{
if(a[j][j]==0)
{
printf("Since diagonal element become zero\n Hence solution is not possible\n");
exit(1);
}
}
x[n-1]=a[n-1][n]/a[n-1][n-1];
for(i=n-2;i>=0;i--)
{
s=0;
for(j=i+1;j<n;j++)
{
s +=(a[i][j]*x[j]);
x[i]=(a[i][n]-s)/a[i][i];
}
}
printf("\nThe result is:\n");

for(i=0;i<n;i++)
printf("\nx[%d]=%4.2f",i+1,x[i]);
getch();
}

OUTPUT:

II. Areas for Plane Figures:


A.
B.
C.
D.
E.

Square
Rectangle
Trapezoid
Circle
Triangle

Areas of Plane Figures:


(Note: This program name AREA CALCULATOR runs only if there are installed
Microsoft 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 button
on 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 = width height, But
since the width and height are by definition the same, the formula is usually
written as area = s2 where s is the length of one side.
For example:
Length = 6m its width is also 6m.
Area = 36 m2

OUTPUT:

Program Code for Square:


Public Class square
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
TextBox3.Text = TextBox1.Text * TextBox2.Text
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Form1.Show()
End Sub

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


System.EventArgs) Handles TextBox1.TextChanged
End Sub
End Class
B. Rectangle
To find the area of a rectangle, multiply the length by the width. The
formula is:
, where is the area, is the length,
is the width,
and means multiply.
For example:
Length = 6m
Width = 4m.
Answer is:
Area = 24 m2
OUTPUT:

Program Code for Rectangle:


Public Class rectangle
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
TextBox3.Text = TextBox1.Text * TextBox2.Text
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Form1.Show()
End Sub
Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox3.TextChanged
End Sub
End Class
C. Trapezoid
A trapezoid is a 4-sided figure with one pair of parallel sides. To find the
area of a trapezoid, take the sum of its bases, multiply the sum by the height of
the trapezoid, and then divide the result by 2. The formula for the area of a
trapezoid is:
Area = (

For example:
Top Base = 6m
Bottom Base = 8m
Height = 4m
Answer is:
Area = 28 m2

OUTPUT:

Program Code for Trapezoid:


Public Class trapezoid
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox2.TextChanged
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.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 As
System.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 As
System.EventArgs) Handles Label3.Click

End Sub
Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Label4.Click
End Sub
Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox4.TextChanged
End Sub
End 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 you
know the diameter, the radius is 1/2 as large.
For example:
Radius: 6m
Answer is:
Area = 113.0973 m2
OUTPUT:

Program Code for Circle:


Public Class circle
Private 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 As
System.EventArgs) Handles Button1.Click
TextBox3.Text = 3.141592654 * TextBox1.Text * TextBox1.Text
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
TextBox3.Text = ""
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Label1.Click
End Sub
Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox3.TextChanged
End Sub
End Class
E. Triangle
It is easy to find the area of a right-angled triangle, or any triangle where
we 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 = 9m2

OUTPUT:

Program Code for Triangle:


Public Class triangle
Private 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 As
System.EventArgs) Handles Button1.Click
TextBox3.Text = (TextBox1.Text * TextBox2.Text) / 2
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Label1.Click

End Sub
Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox3.TextChanged
End Sub
End Class
III. Solution of Quadratic Equation:
(Note: This program name Quadratic Equation Solver runs only if there are installed
Microsoft Visual Basic 2010 express in your computer.)
A quadratic equation is an equation of the second degree, meaning
it contains at least one term that is squared. The standard form is ax + bx + c = 0
with a, b, and c being constants, or numerical coefficients, and x is an unknown
variable. One absolute rule is that the first constant a cannot be a zero.

For Example number 1:


Find the roots of the given equation:
x + -3x + -4 = 0
Answer is:
x1 = 4 and x2 =-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 x2 = Imaginary
*the answer is both imaginary.
OUTPUT:

Program Code for the Solution of Quadratic Equation:


Public Class Form1
Dim a, b, c, d, x, y, z, real, img, e
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Label1.Click
End
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button2.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.Click
If TextBox1.Text = "" Then
Exit Sub
Else
a = Textbox1.text
b = Textbox2.text
c = Textbox3.text
d = (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 = y
TextBox5.Text = z
ElseIf d = 0 Then
x = -b / (2 * a)
TextBox4.Text = x
Elseif d < 0 Then
TextBox4.Text = "IMAGINARY"
TextBox5.Text = "IMAGINARY"
End If
End If
End Sub
Private Function MsgBox() As String
Throw New NotImplementedException
End Function
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
PictureBox1.Click
End Sub
Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
TextBox4.TextChanged
End Sub
End Class

III. CHOLESKY METHOD


Suppose matrices have 3 numbers of equations.
5x1 +2 + 1x3 = 3
4x1 + 1x2 + -1x3 = -3
-2x1 + 3x2 + -3x3 = 6

Matrix A:
5
2
4
1
-2
3
Matrix b:

1
-1
-3

-3

The solution using Cholesky Method is:


X1 = -1.00
X2 = 3.00
X3 = 2.00

OUTPUT:

Program Code for Cholesky Method:


#include<iostream>
#include<conio.h>
using 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<<"\t\t\t CHOLESKY METHOD \n"<<endl;
cout<<"
<<---------------------------------------->> "<<endl;
cout<<" "<<endl;
cout<<"Determine the dimension of the matrix:";
cin>>n;
cout<<"\nEnter matrix A: \n\n";
for(i=1;i<=n;i++)
{
cout<<"Row"<<i<<":";
for(j=1;j<=n;j++)
cin>>a[i][j];
}
cout<<"\nEnter matrix b:"<<endl;
for(i=1;i<=n;i++)
cin>>b[i];
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];
}
}

for(i=1;i<=n;i++)
{
sum=0;
for(p=1;p<i;p++)
sum+=l[i][p]*z[p];
z[i]=(b[i]-sum)/l[i][i];
}
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];
}
cout<<endl<<"The solution using Cholesky Method is:";

for(i=1;i<=n;i++)
cout<<"\nx[%d]=%4.2f"<<endl<<i,x[i]
getch();
return 0;}

You might also like