Scilab/C4/Linear-equations-Iterative-Methods/English
Title of script: Solving System of Linear Equations using Iterative Methods
Author: Shamika
Keywords: System of linear equations, Iterative Methods
|
|
---|---|
Slide 1 | Dear Friends,
Welcome to the Spoken Tutorial on “Solving System of Linear Equations using Iterative Methods” |
Slide 2 -Learning Objective Slide | At the end of this tutorial, you will learn how to:
|
Slide 3-System Requirement slide | To record this tutorial, I am using Ubuntu 12.04 as the operating system with Scilab 5.3.3 version |
Slide 4- Prerequisites slide | Before practising this tutorial, a learner should have basic knowledge of Scilab and Solving Linear Equations
|
Slide 5- Jacobi Method | * The first iterative method we will be studying is Jacobi method.
|
Slide 6- Example | * Let us solve this example using Jacobi Method
|
Switch to Scilab and open JacobiIteration_final.sci | * Let us look at the code for Jacobi Method.
|
Highlight
format('e',20) |
* We use format method to specify the format of the displayed answers on the Scilab console.
|
Highlight
A=input("Enter the coeffiecient matrix of nxn: ") b=input("Enter the right-hand side matrix nx1: ") x0=input("Initial approximation nx1: ") MaxIter=input("Maximum no. of iterations: ") tol=input("Enter the convergence tolerance :")//stop if norm change in x < tol |
* Then we use input function to get the values for the matrices coefficient matrix, right hand side matrix, initial values matrix, maximum number of iteration and convergence tolerance
|
Highlight
[m, n] = size( A ) |
* Then we use size function to check if A matrix is a square matrix.
|
Highlight
for i=1:1:m sum=0; for j=1:1:m sum=sum+abs(A(i,j)); end if 2*abs(A(i,i))<sum then error("the matrix is not diagonally dominant") end end |
* We then check if matrix A is diagonally dominant.
|
Highlight
function [ solution ] = JacobiIteration( A, b, x0, MaxIter, tol ) |
* Then we define the function Jacobi Iteration with input arguments A, b , x zero, maximum iteration and tolerance level.
|
Highlight
if ( length(x0) ~= n ) error( "Sizes of input matrix and input vector do not match" ) end |
* We check if the size of A matrix and initial values matrix are compatible with each other.
|
Highlight
xk = x0 for k = 1 : 1 : MaxIter for i = 1 : n xkp1( i ) = (b(i) - A(i, 1:i-1)*xk(1:i-1) - A(i, i+1:n)*xk(i+1:n)) / A(i,i) //Computes the jacobian updates end
printf( "iter = %d, Relative Error = %g\n", k, RelativeError ) xk = xkp1
break end end
|
* We calculate the value for x k p one and then check if the relative error is lesser than tolerance level.
|
Click on Execute and select Save and Execute | * Let us save and execute the function
|
Switch to Scilab console | * Switch to Scilab console
|
Enter these values on Scilab console for each prompt
Enter the coeffiecient matrix of nxn: [2 1;5 7] |
* Let us enter the values at each prompt.
|
Type
Enter the right-hand side matrix nx1: [11; 13] |
* Then we type
|
Type
Initial approximation nx1: [1;1] |
* The initial values matrix is
|
Type
Maximum no. of iterations: 25 |
* The maximum number of iterations is twenty five
|
Type
Enter the convergence tolerance :0.00001
|
* Let the convergence tolerance level be zero point zero zero zero zero one
|
Type:
JacobiIteration( A, b, x0, MaxIter, tol ) |
* We call the function by typing Jacobi Iteration open paranthesis A comma b comma x zero comma M a x I t e r comma t o l close paranthesis
|
Slide 7- Gauss Seidel Method | * Let us now study Gauss Seidel method.
|
Slide 8- Gauss Seidel | * In Jacobi method, for the computation of x of i k plus one, every element of x of i k is used except x of i k plus one
|
Slide 9- Example | * Let us solve this example using Gauss Seidel Method
|
Switch to Scilab editor and open GaussSeidel.sci
|
* Let us look at the code for Gauss Seidel Method
|
Highlight
format('e',20)
|
* The first line specifies the format of the displayed answer on the console using format function.
|
Highlight
A=input("Enter the coeffiecient matrix of nxn: ") b=input("Enter the right-hand side matrix nx1: ") x0=input("Initial approximation nx1: ") MaxIter=input("Maximum no. of iterations: ") tol=input("Enter the convergence tolerance :")//stop if norm change in x < tol |
* Then we use input function to get the values of coefficient matrix, right hand side matrix, initial values of the variables matrix, maximum number of iteration and tolerance level
|
Highlight
function [ solution ] = GaussSeidel( A, b, x0, MaxIter, tol ) |
* Then we define the function Gauss Seidel with input arguments A comma b comma x zero comma Max Iterations and tolerance level and output argument solution
|
Highlight
// Check that the matrix is square [m, n] = size( A ) if ( m ~= n ) error( "The input matrix is not square" ) end
if ( length(x0) ~= n ) error( "Sizes of input matrix and input vector do not match" ) end |
* We check if matrix A is square and the sizes of initial vector and matrix A are compatible using size and length function.
|
Highlight
xk = x0 xkp1 = zeros( xk ) |
* Then we start the iterations. We equate the initial values vector x zero to x k.
|
Highlight
for k = 1 : 1 : MaxIter
for i = 1 : n xkp1( i ) = (b(i) - A(i, 1:i-1)*xkp1(1:i-1) - A(i,i+1:n)*xk(i+1:n)) / A(i,i) // x^{k+1} end // Applies the relaxation RelativeError = norm( xkp1 - xk, 'inf' ) / norm( xkp1, 'inf' ) printf( "iter = %d, Relative Error = %g\n", k, RelativeError ) xk = xkp1 if ( RelativeError < tol ) break end end
|
* We solve for each equation to get the value of the unknown variable for that equation using x k p one.
|
Click on Execute and select Save and Execute | * Let us save and execute the function
|
Switch to Scilab console | * Switch to Scilab console
|
Type the following on Scilab console:
For first prompt [2 1;5 7] |
* For the first prompt we type matrix A.
|
Second prompt
[11; 13] |
* For the next prompt, type
|
For third prompt
[1;1] |
* We provide the values of initial value vector by typing
|
For fourth prompt
25 |
* Then we specify the maximum number of iterations to be twenty five
|
For fifth prompt
0.00001 |
* Let us define tolerance level to be zero point zero zero zero zero one
|
Then type:
GaussSeidel( A, b, x0, MaxIter, tol )
|
* Finally we call the function by typing G a u s s S e i d e l open paranthesis A comma b comma x zero comma M a x I t e r comma t o l close paranthesis
|
Slide 13- Solve | * Solve this problem on your own using Jacobi and Gauss Seidel methods
|
Slide 14- Summary | In this tutorial, we have learnt to:
|
Show Slide 15
Title: About the Spoken Tutorial Project
|
* About the Spoken Tutorial Project
|
Show Slide 16
Title: Spoken Tutorial Workshops The Spoken Tutorial Project Team
|
The Spoken Tutorial Project Team
|
Show Slide 17
Title: Acknowledgement
|
* Spoken Tutorial Project is a part of the Talk to a Teacher project
|
* This is Ashwini Patil signing off. Thank you for joining.
|