Professional Documents
Culture Documents
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.
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
8
3
4
8
7
9
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";
}
OUTPUT:
B. Matrix Multiplication
2
1
0
0
1
1
Matrix B : :
1
2
1
1
1
2
2
1
1
3
3
4
4
2
5
MATRIX MULTIPLICATION
Console.WriteLine("
<<---------------------------------------->>
")
")
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
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
0.5
-2.5
0.5
0.5
-1
0
"<<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;
}
for(j=n+1;j<=n*2;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
getch();
return 0;
}
OUTPUT:
6
4
3
1
3
2
:
:
:
16
13
9
The result is :
x1 = 1.00
x2 = 2.00
x3 = 1.00
\n");
for(i=0;i<n;i++)
printf("\nx[%d]=%4.2f",i+1,x[i]);
getch();
}
OUTPUT:
Square
Rectangle
Trapezoid
Circle
Triangle
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:
For example:
Top Base = 6m
Bottom Base = 8m
Height = 4m
Answer is:
Area = 28 m2
OUTPUT:
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:
Area = 9m2
OUTPUT:
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.
Matrix A:
5
2
4
1
-2
3
Matrix b:
1
-1
-3
-3
OUTPUT:
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;}