All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
#User Defined Function (UDF) for Jacobi Method ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- function [jac_iter jac_error x] = Jacobi_function (L,d,U,B) x = zeros(3,1); x_old = x; jac_error…
Pratik Ghosh
updated on 19 Jan 2021
#User Defined Function (UDF) for Jacobi Method
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function [jac_iter jac_error x] = Jacobi_function (L,d,U,B)
x = zeros(3,1);
x_old = x;
jac_error = 9e9;
tol = 1e-04;
jac_iter = 1;
T_jac = inv(d)*(L+U);
while (jac_error > tol)
x = (inv(d)*B - T_jac*x_old);
jac_error(jac_iter) = max(abs(x - x_old));
x_old = x;
jac_iter = jac_iter + 1;
endwhile
endfunction
#User Defined Function (UDF) for Gauss seidel Implicit method
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
function [gs_iter gs_error x] = GS_function (L,d,U,B)
x = zeros(3,1);
x_old = x;
gs_error = 9e9;
tol = 1e-04;
gs_iter = 1;
T_gs = (inv(d+L))*U;
while (gs_error > tol)
x = (inv(d+L)*B - T_gs*x_old);
gs_error(gs_iter) = max(abs(x - x_old));
x_old = x;
gs_iter = gs_iter + 1;
endwhile
endfunction
#User Defined Function(UDF)for Successive Over Relaxation (SOR) Implicit Method
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function [sor_iter sor_error x] = SOR_function (L,d,U,B,omega)
x = zeros(3,1);
x_old = x;
sor_error = 9e9;
tol = 1e-04;
sor_iter = 1;
T_sor = (inv(d+(omega*L)))*(omega*U-((1-omega)*d));
while (sor_error > tol)
x = T_sor*x_old + (inv(d+(omega*L))*B);
sor_error(sor_iter) = max(abs(x - x_old));
x_old = x;
sor_iter = sor_iter + 1;
endwhile
endfunction
#Main program code to solve the Linear System using Jacobi, Gauss-seidel & SOR methods
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
clear all
close all
clc
A = [ 5 1 2; -3 9 4; 1 2 -7];
B = [10; -14; -33];
D = diag(diag(A)); %Diagonal Matrix
U = triu(A)-D; %Upper Triangular Matrix
L = tril(A)-D; %Lower Triangular MAtrix
%Calculate Eigen Value manually
%characteristic equation of 3*3 matrix
% -lamba^3 + lamba*trace(A) + (trace(A)^2 - trace(A^2))*lambda + det(A)
% we get [-1*lambda + 7*lambda + 60*lambda - 402*lambda]
co_eff = [-1 7 60 -402];
Eigen_value = roots(co_eff)
ev_check = eig(A);
spectral_radius = max(abs(Eigen_value));
sprintf('Eigen value of matrixc A = %f, %f, %f', Eigen_value(1), Eigen_value(2), Eigen_value(3))
e = 0.2:0.2:1.2; %changinf the diagonal elements (Diagonal Magnification)
%Solve using Different Iterative Methods
for i = 1:length (e)
d = e(i)*D;
sprintf('nn Diagonal Magnification Factor = %f', e(i));
%Jacobi Method
T_jac = inv(d)*(L+U);
jac_spectral(i) = max(abs(eig(T_jac)));
[jac_iter(i) jac_error x] = Jacobi_function (L,d,U,B);
sprintf('Jacobi Method')
sprintf('Spectral Radius = %f', jac_spectral(i))
sprintf('Number of iterations = %f', jac_iter(i))
sprintf('Solution x1 = %f, x2 = %f, x3 = %f', x(1), x(2), x(3))
%Gauss-Sidel (GS) MEthod
T_gs = (inv(d+L))*U;
gs_spectral(i) = max(abs(eig(T_gs)));
[gs_iter(i) gs_error x] = GS_function (L,d,U,B);
sprintf('Gauss Sidel Method')
sprintf('Spectral Radius = %f', gs_spectral(i))
sprintf('Number of iterations = %f', gs_iter(i))
sprintf('Solution x1 = %f, x2 = %f, x3 = %f', x(1), x(2), x(3))
%Successive Over Relaxation (SOR) Method
omega = 2-(2/(1+sqrt(1-(jac_spectral(i)^2))));
T_SOR = (inv(d+(omega*L)))*(omega*U-((1-omega)*d));
SOR_spectral(i) = max(abs(eig(T_SOR)));
[SOR_iter(i) SOR_error x] = SOR_function (L,d,U,B,omega);
sprintf('Successive Over Relaxation (SOR) Method')
sprintf('Spectral Radius = %f', SOR_spectral(i))
sprintf('Number of iterations = %f', SOR_iter(i))
sprintf('Solution x1 = %f, x2 = %f, x3 = %f', x(1), x(2), x(3))
endfor
figure(1)
subplot(2,1,1)
plot(e, jac_spectral,'r')
xlabel('Diagonal Magnification')
ylabel('Spectral Radius')
title('Jacobi Method')
subplot(2,1,2)
plot(e, jac_iter, 'b')
xlabel('Diagonal Magnification')
ylabel('Number of Iteration')
figure(2)
subplot(2,1,1)
plot(e, gs_spectral,'r')
xlabel('Diagonal Magnification')
ylabel('Spectral Radius')
title('Gauss Seidel (GS) Method')
subplot(2,1,2)
plot(e, gs_iter, 'b')
xlabel('Diagonal Magnification')
ylabel('Number of Iteration')
figure(3)
subplot(2,1,1)
plot(e, SOR_spectral,'r')
xlabel('Diagonal Magnification')
ylabel('Spectral Radius')
title('Successive Over Relaxation (SOR) Method')
subplot(2,1,2)
plot(e, SOR_iter, 'b')
xlabel('Diagonal Magnification')
ylabel('Number of Iteration')
Results & Observations:-
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
Visualize 2D Flow Simulation Through A Backward Facing Step For 3 Distinct Mesh Grid Size Using openFOAM & ParaView
Abstract:- The work is focused on simulating fluid flow through a backward-facing step channel that would be subjected to a sudden increase in the cross-sectional area, this would result in flow separation & reattachment zones. The geometry will be designed by creating vertices & then joining them to form blocks.…
19 Jan 2021 06:26 AM IST
Week 12 - Symmetry vs Wedge vs HP equation
Objective:- The work is focused on performing a CFD simulation for a flow through a cylindrical pipe with reference to Hagen Poiseuille's principle that refers to the laminar flow formation inside a smooth cylindrical tube. Since the computation will be carried out on a laptop, we will not be able to consider the entire…
19 Jan 2021 06:26 AM IST
Writing A MATLAB/Octave Code To Perform A 1D Super-Sonic Nozzle Flow Simulation Using Macormack Method.
Abstract:- The work is focused on writing a MATLAB/Octave code to simulate the isentropic flow through a quasi 1D supersonic nozzle using both the conservation and non-conservation forms of the governing equations and solving them using MacCormack's technique and compare their solutions by performing a grid dependency…
19 Jan 2021 06:24 AM IST
Writing A MATLAB/Octave Program To Solve the 2D Heat Conduction Equation For Both Steady & Transient State Using Jacobi, Gauss-Seidel & Successive Over Relaxation (SOR) Schemes.
Problem Statement:- 1. Steady-state analysis & Transient State Analysis Solve the 2D heat conduction equation by using the point iterative techniques that were taught in the class. The Boundary conditions for the problem are as follows; Top Boundary = 600 K Bottom Boundary = 900 K Left Boundary = 400 K Right Boundary…
19 Jan 2021 06:23 AM IST
Related Courses
0 Hours of Content
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.