0% found this document useful (0 votes)
4 views

Algorithm Programming with C++

The document outlines a lecture on algorithms and programming within a mechatronics study program, focusing on arrays, pointers, functions, and linear algebra methods for matrix inversion. It covers key concepts such as array declaration and initialization, pointer operations, and function definitions and calls in C++. Additionally, it discusses various methods for solving linear algebra problems, including Gauss elimination and LU decomposition.

Uploaded by

Hanny Berchmans
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Algorithm Programming with C++

The document outlines a lecture on algorithms and programming within a mechatronics study program, focusing on arrays, pointers, functions, and linear algebra methods for matrix inversion. It covers key concepts such as array declaration and initialization, pointer operations, and function definitions and calls in C++. Additionally, it discusses various methods for solving linear algebra problems, including Gauss elimination and LU decomposition.

Uploaded by

Hanny Berchmans
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

MECHATRONICS STUDY PROGRAM

ALGORITHM & PROGRAMMING


LECTURE 3:
(Arrays, Pointers, Function; Linear Algebra: Matrix)

Dr. Ir. Hanny J. Berchmans, M.T., M.Sc.


11 April 2015
MECHATRONICS STUDY PROGRAM

OUTLINE

Arrays
Pointers
Functions
Linear Algebra: Inverse of Matrix:
 Adjoint Method
 Gauss Elimination Method
 Gauss-Jordan Elimination Method
 Gauss-Siedel Iteration Method
 LU Decomposition Method
MECHATRONICS STUDY PROGRAM

Arrays (1)
• Arrays
 An array stores a fixed-size sequential collection of elements of the
same type. An array is used to store a collection of data, but it is
often more useful to think of an array as a collection of variables of
the same type.
 A specific element in an array is accessed by an index: numbers[0],
numbers[1], and ..., numbers[99]
 All arrays consist of contiguous memory locations. The lowest
address corresponds to the first element and the highest address to
the last element.
 Declaring arrays:
type arrayName [ arraySize ] ;
MECHATRONICS STUDY PROGRAM

Arrays (2)
• Arrays
 Initializing Arrays
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
 If you omit the size of the array, an array just big enough to hold
the initialization is created. Therefore, if you write:
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};

balance[4] = 50.0;
MECHATRONICS STUDY PROGRAM

Arrays (3)
MECHATRONICS STUDY PROGRAM

Arrays (4)
MECHATRONICS STUDY PROGRAM

Arrays (5)
Multidimensional Arrays
 C++ allows multidimensional arrays. Here is the general form of a
multidimensional array declaration: type name[size1][size2]...[sizeN];
 For example, the following declaration creates a three dimensional 5 . 10
. 4 integer array: int threedim[5][10][4];
 Two-Dimensional Arrays: type arrayName [ x ][ y ]; A two-dimensional
array can be think as a table, which will have x number of rows and y
number of columns. A 2-dimensional array a, which contains three rows
and four columns can be shown as below:
MECHATRONICS STUDY PROGRAM

Arrays (5)
Multidimensional Arrays
 Initializing Two-Dimensional Arrays:

 Accessing Two-Dimensional Array Elements:


MECHATRONICS STUDY PROGRAM

OUTLINE

Arrays
Pointers
Functions
 Linear Algebra: Inverse of Matrix:
 Adjoint Method
 Gauss Elimination Method
 Gauss-Jordan Elimination Method
 Gauss-Siedel Iteration Method
 LU Decomposition Method
MECHATRONICS STUDY PROGRAM

Pointers (1)
• Pointers
 Every variable is a memory location and every memory location
has its address defined which can be accessed using ampersand
(&) operator which denotes an address in memory.
 A pointer is a variable whose value is the address of another
variable. Like any variable or constant, you must declare a pointer
before you can work with it.
 The general form of a pointer variable declaration is:
type *var-name;
MECHATRONICS STUDY PROGRAM

Pointers (2)

Address of variable
can be accessed
through ampersand
(&) operator
MECHATRONICS STUDY PROGRAM

Pointers (3)
• Pointers
 The actual data type of the value of all pointers, whether integer,
float, character, or otherwise, is the same, a long hexadecimal
number that represents a memory address. The only difference
between pointers of different data types is the data type of the
variable or constant that the pointer points to.
 Important operations for Pointers:
1. to define a pointer variables
2. to assign the address of a variable to a pointer
3. to access the value at the address available in the pointer
variable
 This is done by using unary operator * that returns the value of the
variable located at the address specified by its operand. Following
example makes use of these operations:
MECHATRONICS STUDY PROGRAM

Pointers (3)
MECHATRONICS STUDY PROGRAM

Pointers (4)
• Pointers in details:
MECHATRONICS STUDY PROGRAM

Pointers (5)
• NULL pointers:
– A pointer that is assigned NULL is called a null pointer.
– If all unused pointers are given the null value and you avoid the
use of a null pointer, you can avoid the accidental misuse of an
uninitialized pointer. Many times, uninitialized variables hold some
junk values and it becomes difficult to debug the program.
MECHATRONICS STUDY PROGRAM

Pointers (7)
• Pointer arithmetic:
– pointer is an address which is a numeric value; therefore, you can
perform arithmetic operations on a pointer just as you can a
numeric value. There are four arithmetic operators that can be
used on pointers: ++, --, +, and -.
– Incrementing a Pointer:
MECHATRONICS STUDY PROGRAM

Pointers (8)
• Pointer arithmetic:
– Decrementing a Pointer:
MECHATRONICS STUDY PROGRAM

Pointers (9)
• Pointer arithmetic:
– Pointer Comparisons: pointers may be compared by using
relational operators, such as ==, <, and >. If p1 and p2 point to
variables that are related to each other, such as elements of the
same array, then p1 and p2 can be meaningfully compared.
MECHATRONICS STUDY PROGRAM

Pointers (10)
• Pointers vs arrays:
– Pointers and arrays are strongly related. In fact, pointers and
arrays are interchangeable in many cases. For example, a pointer
that points to the beginning of an array can access that array by
using either pointer arithmetic or array-style indexing.
MECHATRONICS STUDY PROGRAM

Pointers (11)
• Pointers vs arrays:
– However, pointers and arrays are not completely interchangeable.
– It is perfectly acceptable to apply the pointer operator * to var but it
is illegal to modify var value. The reason for this is that var is a
constant that points to the beginning of an array and can not be
used as l-value. Because an array name generates a pointer
constant, it can still be used in pointer-style expressions, as long as
it is not modified.
MECHATRONICS STUDY PROGRAM

Pointers (12)
• Array of pointers:
MECHATRONICS STUDY PROGRAM

Pointers (13)
• Array of pointers:
There may be a situation, when we want to maintain an array,
which can store pointers to an int or char or any other data type
available. Following is the declaration of an array of pointers to an
integer: This declares ptr as an array of MAX integer
pointers. Thus, each element in ptr, now holds a
pointer to an int value.
MECHATRONICS STUDY PROGRAM

Pointers (14)
• Array of pointers:
You can also use an array of pointers to character to store a list of
strings as follows:
MECHATRONICS STUDY PROGRAM

Pointers (15)
• Pointer to Pointer (Multiple Indirection):
A pointer to a pointer is a form of multiple indirection or a chain of
pointers. Normally, a pointer contains the address of a variable.
When we define a pointer to a pointer, the first pointer contains the
address of the second pointer, which points to the location that
contains the actual value.

A variable that is a pointer to a pointer must be declared as such. This is done by


placing an additional asterisk in front of its name. For example, following is the
declaration to declare a pointer to a pointer of type int:
MECHATRONICS STUDY PROGRAM

Pointers (16)
• Pointer to Pointer (Multiple Indirection):
When a target value is indirectly pointed to by a pointer to a pointer,
accessing that value requires that the asterisk operator be applied
twice, as is shown below in the example:
MECHATRONICS STUDY PROGRAM

Pointers (15)
• Passing pointers to functions:
C++ allows you to pass a pointer to a function. To do so, simply
declare the function parameter as a pointer type. Following a
simple example where we pass an unsigned long pointer to a
function and change the value inside the function which reflects
back in the calling function:
MECHATRONICS STUDY PROGRAM

Pointers (16)
• Return pointer from functions:
The function which can accept a pointer, can also accept an array
as shown in the following example::
MECHATRONICS STUDY PROGRAM

OUTLINE

Arrays
Pointers
Functions
 Linear Algebra: Inverse of Matrix:
 Adjoint Method
 Gauss Elimination Method
 Gauss-Jordan Elimination Method
 Gauss-Siedel Iteration Method
 LU Decomposition Method
MECHATRONICS STUDY PROGRAM

Functions (1)
• A function is a group of statements that together perform a task. Every
C++ program has at least one function, which is main(), and all the
most trivial programs can define additional functions.
• You can divide up your code into separate functions. How you divide
up your code among different functions is up to you, but logically the
division usually is so each function performs a specific task.
• A function declaration tells the compiler about a function's name,
return type, and parameters. A function definition provides the actual
body of the function.
• The C++ standard library provides numerous built-in functions that
your program can call. For example, function strcat() to concatenate
two strings, function memcpy() to copy one memory location to another
location and many more functions.
• A function is knows as with various names like a method or a sub-
routine or a procedure etc.
MECHATRONICS STUDY PROGRAM

Functions (2)
• Defining a Function.
– The general form of a C++ function definition is as follows:

– A C++ function definition consists of a function header and a function body. Here
are all the parts of a function:
– Return Type: A function may return a value. The return type is the data type of the
value the function returns. Some functions perform the desired operations without
returning a value. In this case, the return type is the keyword void.
– Function Name: This is the actual name of the function. The function name and the
parameter list together constitute the function signature.
– Parameters: A parameter is like a placeholder. When a function is invoked, you
pass a value to the parameter. This value is referred to as actual parameter or
argument. The parameter list refers to the type, order, and number of the
parameters of a function. Parameters are optional; that is, a function may contain no
parameters.
– Function Body: The function body contains a collection of statements that define
what the function does.
MECHATRONICS STUDY PROGRAM

Functions (3)
• Example:.
– Following is the source code for a function called max(). This
function takes two parameters num1 and num2 and returns the
maximum between the two:
MECHATRONICS STUDY PROGRAM

Functions (4)
• Function Declarations:
– A function declaration tells the compiler about a function name and
how to call the function. The actual body of the function can be
defined separately.
– A function declaration has the following parts:
return_type function_name( parameter list );
– For the above defined function max(), following is the function
declaration:
int max(int num1, int num2);
– Parameter names are not importan in function declaration only their
type is required, so following is also valid declaration:
int max(int, int);
– Function declaration is required when you define a function in one
source file and you call that function in another file. In such case,
you should declare the function at the top of the file calling the
function.
MECHATRONICS STUDY PROGRAM

Functions (5)
• Calling a Function:
– When a program calls a function, program control is transferred to the called function.
A called function performs defined task and when its return statement is executed or
when its function-ending closing brace is reached, it returns program control back to
the main program.
– To call a function, you simply need to pass the required parameters along with
function name, and if function returns a value, then you can store returned value. For
example:
MECHATRONICS STUDY PROGRAM

Functions (6)
• Function Arguments:
– If a function is to use arguments, it must declare variables that accept the values of
the arguments. These variables are called the formal parameters of the function.
– The formal parameters behave like other local variables inside the function and are
created upon entry into the function and destroyed upon exit.
– While calling a function, there are two ways that arguments can be passed to a
function:
MECHATRONICS STUDY PROGRAM

Functions (7)
• Default Values for Parameters:
– When you define a function, you can specify a default
value for each of the last parameters. This value will be
used if the corresponding argument is left blank when
calling to the function.
– This is done by using the assignment operator and
assigning values for the arguments in the function
definition. If a value for that parameter is not passed
when the function is called, the default given value is
used, but if a value is specified, this default value is
ignored and the passed value is used instead. Consider
the following example:
MECHATRONICS STUDY PROGRAM

Functions (8)
MECHATRONICS STUDY PROGRAM

Functions (9)
• Function call by value:
– The call by value method of passing arguments to a
function copies the actual value of an argument into the
formal parameter of the function. In this case, changes
made to the parameter inside the function have no effect
on the argument.

– By default, C++ uses call by value to pass arguments. In


general, this means that code within a function cannot
alter the arguments used to call the function. Consider
the function swap() definition as follows.
MECHATRONICS STUDY PROGRAM

Functions (10)
MECHATRONICS STUDY PROGRAM

Functions (11)
• Function call by reference:
– The call by reference method of passing arguments to a
function copies the reference of an argument into the
formal parameter. Inside the function, the reference is
used to access the actual argument used in the call. This
means that changes made to the parameter affect the
passed argument.
– To pass the value by reference, argument reference is
passed to the functions just like any other value. So
accordingly you need to declare the function parameters
as reference types as in the following function swap(),
which exchanges the values of the two integer variables
pointed to by its arguments.
MECHATRONICS STUDY PROGRAM

Functions (12)
MECHATRONICS STUDY PROGRAM

Functions (13)
• Function call by pointer:
– The call by pointer method of passing arguments to a
function copies the address of an argument into the
formal parameter. Inside the function, the address is
used to access the actual argument used in the call. This
means that changes made to the parameter affect the
passed argument.
– To pass the value by pointer, argument pointers are
passed to the functions just like any other value. So
accordingly you need to declare the function parameters
as pointer types as in the following function swap(),
which exchanges the values of the two integer variables
pointed to by its arguments.
MECHATRONICS STUDY PROGRAM

Functions (14)
MECHATRONICS STUDY PROGRAM

OUTLINE

Arrays
Pointers
Functions
 Linear Algebra: Inverse of Matrix:
 Adjoint Method
 Gauss Elimination Method
 Gauss-Jordan Elimination Method
 Gauss-Siedel Iteration Method
 LU Decomposition Method
Matrix Invers
MECHATRONICS STUDY PROGRAM

Adjoint Method (1)


• Adjoint Method is a method to calculate a rectangular Matrix
Invers by using determinant and adjoint of the matrix.
• Adjoint of matrix A is the transpose of the cofactor C of matrix
A.
• C is a Cofactor of Matrix A. Member of Matrix C is defined as:
cij = (-1)i+j .Mij

• Mij is member of cofactor Matrix A. Consider for 3 x 3 matrix


A:
a11 a12 a13 
A  a21 a22 a23 
a31 a32 a33 
Matrix Invers
MECHATRONICS STUDY PROGRAM

Adjoint Method (2)


• Member of Mij is defined as:
a 22 a 23 a 21 a 23 a 21 a 22
M11  M12  M13 
a 32 a 33 a 31 a 33 a 31 a 32

a12 a13 a11 a13 a11 a12


M 21  M 22  M 23 
a 32 a 33 a 31 a 33 a 31 a 32

a12 a13 a11 a13 a11 a12


M 31  M 32  M 33 
a 22 a 23 a 21 a 23 a 21 a 22

Each of this Mij is a determinant, thus:


M11  a 22 .a 33  a 23 .a 32
Matrix Invers
MECHATRONICS STUDY PROGRAM

Adjoint Method (3)


• Thus matrix cofactor C is defined as:

 a22 a23 a21 a23 a21 a22 


   
 a32 a33 a31 a33 a31 a32 
 a12 a13 a11 a13 a a12 
C     11 
 a32 a33 a31 a33 a31 a32 
 
 a12 a13

a11 a13

a11 a12 
 a22 a23 a21 a23 a21 a22 

Matrix Invers
MECHATRONICS STUDY PROGRAM

Adjoint Method (4)


• Thus the adjoint of matrix A is defined as adj(A):

 a22 a23 a12 a13 a12 a13 


   
 a32 a33 a32 a33 a22 a23 
 a21 a23 a11 a13 a a13 
adj(A)  C 
T
  11 
 a31 a33 a31 a33 a21 a23 
 
 a21 a22

a11 a12

a11 a12 
 a31 a32 a31 a23 a21 a22 

Matrix Invers
MECHATRONICS STUDY PROGRAM

Adjoint Method (5)


• Then the invers of matrix A is defined as A-1:
 a22 a23 a12 a13 a12 a13 
   
 a32 a33 a32 a33 a22 a23 
 a21 a23 a11 a13 a11 a13  adj(a ij )
1
A  1 / det(A) 
1
   OR a 
 a31 a33 a31 a33 a21 a23  ij
det aij
 
 a21 a22

a11 a12

a11 a12 
 a31 a32 a31 a23 a21 a22 

• The determinant of matrix A is defined as det (A):

a 22 a 23 a a 23 a a 22
det(A)  a11.  a12 . 21  a13 . 21
a 32 a 33 a 31 a 33 a 31 a 32
MECHATRONICS STUDY PROGRAM

Example:
Calculate the value of x1, x2 and x3 from the linear system below :
invers:
x1 + 0,5x2 = 100
2x1 + x2 + x3 = 200 cij = (-1) . M ij
0,5x1 + 0,5x2 + x3 = 100 c11 = 1(1,0 - 0,5) = 0,5
c12 = -1(2,0 - 0,5) = -1,5
1,0 0,5 0   x1  100  c13 = 1(1,0 - 0,5) = 0,5
2,0 1,0 1,0   x   200 
  2    c21 = -1(0,5 – 0) = -0,5
0,5 0,5 1,0  x3  100  1,0 0,5 0  c22 = 1(1,0 - 0) = 1,0
2,0 1,0 1,0   a c23 = -1(0,5 – 0,25) = -0,25
  ij
Determinant: 0,5 0,5 1,0 c31 = 1(0,5 – 0) = 0,5
c32 = -1(1,0 – 0) = -1,0
1,0 1,0 2,0 1,0
aij  1,0  0,5 0 c33 = 1(1,0 – 1,0) =0
0,5 1,0 0,5 1,0
 0,5 - 1,5 0,5 
= 1,0 (1,0–0,5) – 0,5(2,0–0,5) + 0 cij  - 0,5 1,0 - 0,25
= 0,25  0,5 - 1,0 0 
MECHATRONICS STUDY PROGRAM

c ji  cij  value of xj : aij .x j  bi


T

x j  aij1 .bi
 0,5 - 0,5 0,5
 - 1,5 1,0 1,0 
 2,0 2,0 - 2,0 100 
 0,5 - 0,25 0 
x j   6,0 - 4,0 4,0  200 
 0,5 - 0,5 0,5  - 2,0 1,0 0  100 
1  
aij1  - 1,5 1,0 1,0
 0,25    x1  0 
 0,5 - 0,25 0   x   200 
 2  
 2,0 2,0 - 2,0  x3  0 
  6,0 - 4,0 4,0 
 - 2,0 1,0 0  So : x1 = 0; x2 = 200,
And x3 = 0
Matrix Invers
MECHATRONICS STUDY PROGRAM

Gauss Elimination (1)


• Gaussian elimination (also known as row reduction) is an
algorithm for solving systems of linear equations.
• To perform row reduction on a matrix, one uses a sequence
of elementary row operations to modify the matrix until the
lower triangular of the matrix is filled with zeros, as much as
possible.

a 11 a 12 a 13  x   b1 
A  a 21 a 22 a 23   y   b 2 
a 31 a 32 a 33   z   b3 
Matrix Invers
MECHATRONICS STUDY PROGRAM

Gauss Elimination (2)


• The matrix above must be transformed until members of
lower triangular become all 0 (zero):
L1 a 11 a 12 a 13   x   b1  L2’ = L2 – a21/a11.L1
L 2  a 21 a 22 a 23   y   b 2 
L3’ = L3 – a31/a11.L1
L 3 a 31 a 32 a 33   z   b 3 

L1 a 11 a 12 a 13   x   b1 
L 2'  0 a 22' a 23'   y   b 2'  L3’’ = L3’ – a32’/a22’.L2’
L 3' 0 a 32' a 33'   z   b 3' 

L1 a 11 a 12 a 13   x   b1  z = b3’ /a33’
L 2'  0 a 22' a 23'   y   b 2'  y = (b2’ - a23’).b/(a22. a33’)
L 3' 0 0 a 33'   z   b 3' 
x = (b1 + a12.y - a13.z)
MECHATRONICS STUDY PROGRAM
Matrix Invers
MECHATRONICS STUDY PROGRAM

Gauss-JordanElimination (1)
• Gauss-Jordan elimination is an algorithm for solving systems
of linear equations that developed from Gauss elimination
• To perform row reduction on a matrix, one uses a sequence
of elementary row operations to modify the matrix until lower
and upper triangularof the matrix is filled with zeros.

a 11 a 12 a 13  x   b1 
 
A  a 21 a 22 a 23   y   b 2 
a 31 a 32 a 33   z   b3 
Matrix Invers
MECHATRONICS STUDY PROGRAM

Gauss-Jordan Elimination (2)


• The matrix above must be transformed until members of
lower & upper triangular become all 0 (zero):
L1 a 11 a 12 a 13   x   b1  L2’ = L2 – a21/a11.L1
L 2  a 21 a 22 a 23   y   b 2 
L3’ = L3 – a31/a11.L1
L 3 a 31 a 32 a 33   z   b 3 

L1 a 11 a 12 a 13   x   b1 
L 2'  0 a 22' a 23'   y   b 2'  L3’’ = L3’ – a32’/a22’.L2’
L 3' 0 a 32' a 33'   z   b 3' 

L1 a 11 a 12 a 13   x   b1  L2’’ = L2’ – a23’/a33’.L3’’


L 2'  0 a 22' a 23'   y    b 2' 
L1’ = L1 – a13/a23’.L3’’
L 3'' 0 0 a 33'   z  b 3'' 
Matrix Invers
MECHATRONICS STUDY PROGRAM

Gauss –Jordan Elimination (3)

L1' a 11 a 12 0   x   b1'  L2’’ = L2’ – a23’/a33’.L3’’


L 2''  0 a 22' 0   y   b 2'' 
L1’ = L1 – a13/a23’.L3’’
L 3'' 0 0 a 33'   z   b 3'' 
L1' a 11 a 12 0   x   b1'  L2’’’ = a22/a22.L2’
L 2''  0 a 22' 0   y   b 2'' 
L3’’’ = a33’/a33’.L3’’’
L3'' 0 0 a 33'   z   b3'' 
L1' a 11 a 12 0   x   b1'  L1’’ = L1’ – a12’/a22’’.L3’’’
L 2 '''  0 a 22'' 0   y   b 2 ''' 
L 3''' 0 0 a 33' '  z   b 3'''  L1’’’ = L1’’ – a11’/a11’’.L1’

L1''' a 11' 0 0   x   b1''  x = b1’’ /a11.


L 2 '''  0 a 22'' 0   y   b 2 '''  y = b2’’’ /a22.
L 3''' 0 0 a 33' '  z   b 3'''  z = b1’’ /a33.
Matrix Invers
MECHATRONICS STUDY PROGRAM

Gauss-Siedel Iteration (1)


• The Gauss–Seidel method is an iterative technique for
solving a square system of n linear equations with unknown.
• This method will use need approximation to the value of x, y,
and z. Then the new value of x, y, and z is updated with
better approximation. The iterative proses continues until it
reach convergence.
a 11 x 1 + a 12 x 2 + a 13 x 3 + ... + a 1n x n  b1
a 21 x 1 + a 22 x 2 + a 23 x 3 + ... + a 2n x n  b 2
a 31 x 1 + a 32 x 2 + a 33 x 3 + ... + a 3n x n  b 3
... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
a n1 x 1 + a n2 x 2 + a n3 x 3 + ... + a nn x n  b n
Matrix Invers
MECHATRONICS STUDY PROGRAM

Gauss-Siedel Iteration (2)


1
x 1 = (b 1− a12 x 2 − a 13 x 3 − .. ..− a 1n x n )
a11
1
x 2 = (b 2 − a21 x1 − a 23 x 3 − .. . .− a2n x n )
a2 2
. .. .. ..... .. ...... .. .. . .. .. . .. .. . .. .. ... .. .. ..... . .. ... .. .. . ...
1
x n = (bn − a n1 x 1 − an2 x 2 − . ...− a nn− 1 x n− 1)
ann
Matrix Invers
MECHATRONICS STUDY PROGRAM

Gauss-Siedel Iteration (3)


Example:
Solve the linear system by using Gauss-Siedel Iteration

3 x – 0.1 y – 0.2 z = 7.85


0.1 x + 7 y - 0.3 z = -19.3
0.3 x – 0.2 y + 10 z = 71.4

procedure:
Firstly unknown value are given value zero
The result of calculation are used for the next calculation.

1st iteration
 By assuming y and z = zero, then x can be determined:

7,85  0,1y  0,2 z 7,85


x   2,61667
3 3
Matrix Invers
MECHATRONICS STUDY PROGRAM

Gauss-Siedel Iteration (4)

Thus by assuming z=0 and x with the value above, y can be


determined :
 19,3  0,1x  0,3z  19,3  0,1(2,61667 )
y   2,7945
7 7
With value y and x above, z value can be determined:

71,4  0,3x  0,2 y 71,4  0,3(2,61667 )  0,2(2,7945 )


z 
10 10
z  7,0056
Matrix Invers
MECHATRONICS STUDY PROGRAM

Gauss-Siedel Iteration (5)


 2nd Iteration:

7,85  0,1y  0,2 z 7,85  0,1(2,7945 )  0,2(7,0056 )


x 
3 3
 2,99056

 19,3  0,1x  0,3z  19,3  0,1(2,99056 )  0,3(7,0056 )


y 
7 7
 2,49962

71,4  0,3x  0,2 y 71,4  0,3(2,99056 )  0,2(2,49962 )


z 
10 10
z  7,00029
Matrix Invers
MECHATRONICS STUDY PROGRAM

Gauss-Siedel Iteration (6)


 3rd Iteration:
7,85  0,1y  0,2 z 7,85  0,1(2,49963 )  0,2(7,0029 )
x 
3 3
 3,00032

 19,3  0,1x  0,3z  19,3  0,1(3,00032 )  0,3(7,0029 )


y 
7 7
 2,49999

71,4  0,3x  0,2 y 71,4  0,3(3,00032 )  0,2(2,49999 )


z 
10 10
z  6,99999
Matrix Invers
MECHATRONICS STUDY PROGRAM

Gauss-Siedel Iteration (7)


 4th Iteration:

7,85  0,1y  0,2 z 7,85  0,1(2,499999 )  0,2(6,99999 )


x 
3 3
3

 19,3  0,1x  0,3z  19,3  0,1(3)  0,3(6,99999 )


y 
7 7
 2,5

71,4  0,3x  0,2 y 71,4  0,3(3)  0,2(2,5)


z 
10 10
z7
Matrix Invers
MECHATRONICS STUDY PROGRAM

LU Decomposition (1)
• If Matrix A is a non singulat matrix (matrix that has invers)
then Matrix A can be factorize to become two different
triangular matrix (upper and lower).
• Those changes can be illustrated below:

A
Elementer U

transformation to row L
Matrix Invers
MECHATRONICS STUDY PROGRAM

LU Decomposition (2)
• Pada matriks segitiga bawah, L, semua elemen diagonal utamanya
berharga 1, sedangkan pada matriks segitiga atas, U tidak ada aturan
khusus pada elemen diagonal utamanya. Setelah pemfaktoran matriks A
menjadi matriks L dan matriks U, maka kedua matriks tersebut dapat
digunakan untuk menyelesaikan sistem persamaan linier AX = B, yaitu
sebagai berikut. Tinjau SPL AX = B, kemudian faktorkan A menjadi L dan
U, sehingga A = LU, sehingga LUX = B. Misalkan UX = y, maka Ly = B.
Untuk memperoleh , kita gunakan teknik substitusi maju ( forward
substitution ), sbb,
Matrix Invers
MECHATRONICS STUDY PROGRAM

LU Decomposition (2)
• Pada matriks segitiga bawah, L, semua elemen diagonal utamanya
berharga 1, sedangkan pada matriks segitiga atas, U tidak ada aturan
khusus pada elemen diagonal utamanya. Setelah pemfaktoran matriks A
menjadi matriks L dan matriks U, maka kedua matriks tersebut dapat
digunakan untuk menyelesaikan sistem persamaan linier AX = B, yaitu
sebagai berikut. Tinjau SPL AX = B, kemudian faktorkan A menjadi L dan
U, sehingga A = LU, sehingga LUX = B. Misalkan UX = y, maka Ly = B.
Untuk memperoleh , kita gunakan teknik substitusi maju ( forward
substitution ), sbb,
Matrix Invers
MECHATRONICS STUDY PROGRAM

LU Decomposition (2)
• Example:
Matrix Invers
MECHATRONICS STUDY PROGRAM

LU Decomposition (2)
• Example:
MECHATRONICS STUDY PROGRAM

Selesaikan dengan Eliminasi Gauss


Cari Kofaktornya

3 1 2
 
0 1 4
2  2 1 
 
MECHATRONICS STUDY PROGRAM

Dr. Ir. Hanny J. Berchmans, M.T. M.Sc.


+628775006229
[email protected]

You might also like