Sunteți pe pagina 1din 6

Experiment No:- 05

Name of the Experiment:MATLAB program for matrix operations, definite integration of a function and
Gauss elimination method.
Objectives:Objectives of this experiment are 1. To get familiar with different matrix operations such as- matrix addition, subtraction,
multiplication & division.
2. Finding out the value of the definite integral of a function and
3. Finding out the roots of simultaneous linear equations using Gauss elimination
method.
Theory:MATLAB is the precise form of Matrix Laboratory. From the name itself it is
easily understood that MATLAB performs matrix operations. All kind of matrix operations
can be performed through MATLAB. Value of the definite integral of any function can be
obtained by proper coding in MATLAB. Gauss elimination method has also been performed
in the laboratory. Brief description of all of these are given belowMatrix operation:We can represent a matrix in MATLAB as followingS=[ r1 ; r2 ; . ; rn]
Where r represents the row of a given matrix & the subscript 1, 2, . ,n represents the
number of row. r is expressed as followsr = a1, a2, . , an
Where a is the element of a particular row. An example of this form is given belowS=[ 1, 2, 3; 6, 5, 7; 10, 9, 8 ]
Here S is a 3*3 matrix whose elements are shown as described in above format.
Matrix arithmetic operations are done easily. For adding, subtracting, multiplying or dividing
two matrix we will have to just express those matrix as above format and then use the
required arithmetic operators between them such as + for addition, - for subtraction etc. The
arithmetic operations will be shown below in the MATLAB program section.

Definite integral of a function:We can find out the value of the definite integral of a function using quad. Quad is
the precise form of quadrature. Quadrature is a numerical method used to find the area under
the graph of a function, that is, to compute a definite integral. But for integrating a function
we will have to hanle it first using function handling symbol @(x) . After calling the function
properly we will have to use quad. Let the called function has been kept in k. Now quad is
used as followsintegral= quad( k, a, b)
Where a and b are the lower and upper limit of that function respectively.
An example of using these are given belowk= @(x) sin(x);
integral= quad( k, 0, pi )
Here the function sin(x) has been kept in k and 0 & pi have been taken as lower and upper
limit. In this way we can obtain the definite integral value of any function.
Gauss elimination method:Gauss elimination is one of the methods of solving simultaneous lineaer
equations. This method is also known as row reduction method. For solving a system of
linear equatios using Gauss elimination method we need to follow some steps. Fisrtly we
need to convert the system into an equivalent upper triangular system. Then we have to solve
it using back substitution method. This solution process is described belowLet the system of n linear equations with n unknowns be given bya11x1 + a12x2 + a13x3 + +a1nxn = b1
a21x1 + a22x2 + a23x3 + +a2nxn = b2
a31x1 + a32x2 + a33x3 + +a3nxn = b2
.
.
.

an1x1 + an2x2 + an3x3 + +annxn = bn


The unknowns are eliminated to obtain an upper-triangular system. To eliminate x 1 from the
second equation, first equation must be multiplied by (-a21/a11) and we obtain
-a21x1 a12(a21/a11)x2 a13(a21/a11)x3 - -a1n(a21/a11)xn = -b1(a21/a11)
Adding the abve equation to the second equation we get(a22 - a12(a21/a11))x2 + (a23a13(a21/a11))x3 + +(a2n-a1n(a21/a11))xn = b2 b1(a21/a11)
The above equation can be written as-

a22`x2 + a23`x3 + + a2n`xn = b2`


where a`22 = a22 a12(a21/a11), etc. The primes indicate that the original element has changed
its value. Similarly, The first equation can be multiplied by a 31/a11 and adding it to the third
equation above. This eliminates x1 from the third equation above and stand as the following
form
a32`x2 + a33`x3 + +a3n`xn = b3`
Simiarly, eliminating x1 up to the last equation, the system becomesa11x1 + a12x2 + a13x3 + .+a1nxn = b1
a22`x2 + a23`x3 + + a2n`xn = b2`
a32`x2 + a33`x3 +...+a3n`xn = b3`
.
.
.
an2`x2 + an3`x3 + .+ann`xn= bn`
Similarly, x2 is eliminated from the last (n-2) equations. For example, to eliminate x 2 from the
third equation, the second equation is multiplied by (-a` 32/a`22) and added to the third
equation.
Repeating this process with remaining equations, we system obtained becomes like belowa11x1 + a12x2 +a13x3 + +a1nxn = b1
a22`x2 + a23`x3 + + a2n`xn = b2`
a33``x3+ + a3n``xn = b3``
.
.
an3``x3+ + ann``xn= bn``
Here, the double primes indicate that the elements have been changed twice. In this way, x 3
can be eliminated from fourth equation, x 4 can be eliminated from fifth equation and so on till
the upper-triangular form is obtained
a11x1 + a12x2 +a13x3 + ..+ a1nxn = b1
a22`x2 + a23`x3 + + a2n`xn = b2`
a33``x3+ + a3n``xn = b3``
.
.
ann(n-1)xn= bn(n-1)
Then the required solution from the last system has to be obtained. From the last equation
xn = bn(n-1)/ann(n-1)
This is then substituted in the (n-1)th equation to obtain x n-1 and the process is repeated to
compute the other unknowns. In this process, xnis computed first, then xn-1, xn-2x2,x1, in that
order. Due to this reason, this process is called back substitution.

MATLAB program:Matrix arithmetic operation:clc


clear all
a=[1,3,5;2,4,6;7,6,8];
b=[2,4,6;1,3,5;8,6,7];
sum=a+b
subtraction=a-b
multiplication=a*b
division=a/b
Output:sum =
3
3
15

7 11
7 11
12 15

subtraction =
-1
1
-1

-1
1
0

-1
1
1

multiplication =
45
56
84

43 56
56 74
94 128

division =
0
1.0000
1.0000
0
-1.5000 2.0000

0
0
1.0000

Definite integration of the function f(x)= sin(x)^2 * cos(x) :clc


clear all

pi=3.1416;
f=@(x) sin(x).^2.*cos(x);
i=quad(f,(-pi/2),(pi/2));
disp(' integrated value of the function is - ')
disp(i)
Output:integrated value of the function is 0.6667
Gauss elimination method:We will have to solve the following system using Gauss elimination method5x-2y+z = 4
7x+y-5z = 8
3x+7y+4z = 10
MATLAB program to solve this is given belowclc
clear all
a = [5 -2 1 4; 7 1 -5 8; 3 7 4 10];
[r c] = size(a);
for k= 1:(r-1)
for i = k:(r-1)
b = a(i+1,k)/a(k,k);
for j = 1:c
a(i+1,j) = a(i+1,j) - a(k,j)*b;
end
end
end
x=zeros(r,1);
a
x(r)= a(r,c)/a(r,r);
for i=r-1:-1:1
x(i)=(a(i,c)-a(i,i+1:r)*x(i+1:r))/a(i,i);
end
x'

Output:a=

5.0000 -2.0000 1.0000 4.0000


0
3.8000 -6.4000 2.4000
0
0 17.2105 2.4211
ans =
1.1193

0.8685

0.1407

Conclusion:The equations were written in the matrix form while performing Gauss
elimination method in MATLAB. Both the upper triangular form and the solutions had been
shown in output of Gauss elimination method which justified the theory described above.
The zeros function was used just to create a matrix of zeros at the beginning of the back
substitution. The concept of matrix arithmatic operation which had also been performed
before Gauss elimination method, had been utilized here. The definite integration can also
been performed using integral instead of quad , the rest procedure would be the same. We
can use any one between integral and quad.

S-ar putea să vă placă și