SlideShare a Scribd company logo
Incorporate the SOR method in the multigridTest.m and apply the multigridTest.m, to
determine the effects of changes in
Pre-smoothing iterations
Post-smoothing iterations
Multigrid cycle type
Iterative method
Number of grids (or grid hierarchy)
% SOR (Successive Over-Relaxation)
n = input('Enter number of equations, n: ');
A = zeros(n,n+1);
x1 = zeros(1,n);
A=[5 -2 3 -1;-3 9 1 2;2 -1 -7 3];
x1 = [0 0 0];
tol = input('Enter tolerance, tol: ');
m = input('Enter maximum number of iterations, m: ');
w = input('Enter the parameter w (omega): ');
k = 1;
while k <= m
err = 0;
for i = 1 : n
s = 0;
for j = 1 : n
s = s-A(i,j)*x1(j);
end
s = w*(s+A(i,n+1))/A(i,i);
if abs(s) > err
err = abs(s);
end
x1(i) = x1(i)+s;
end
if err <= tol
break;
else
k = k+1;
end
end
fprintf('The solution vector after %d iterations is :n', k);
for i = 1 : n
fprintf(' %11.8f n', x1(i));
end
%% Multigrid test case with visulization
% Author: Ben Beaudry
clc; clear; close all
load A_b.mat;
A = full(A); % Unsparce matrix to show full power of Multigrid
pre = 2; % Number of presmoothing iterations
post = 2; % Number of postsmoothing iterations
% cycle = 1; % Type of multigrid cycle (1=V-cycle, 2=W-cycle, 3=F-cycle)
smooth = 1; % Smoother type (1=Jacobi, 2=Gauss-Seidel)
grids = 5; % Max grids in cycle
maxit = 10; % Max iterations of solver
tol = 1e-02; % Tolerance of solver
%% Solvers
% solve directly
t=cputime;
x_D = Ab;
fprintf('Direct Solve Time: %g Secondsn',cputime-t)
% V-Cycle
t=cputime;
[x_V,res_V,it_V] = multigrid(A,b,pre,post,1,smooth,grids,maxit,tol);
fprintf('V-Cycle Solve Time: %g Secondsn',cputime-t)
% W-Cycle
t=cputime;
[x_W,res_W,it_W] = multigrid(A,b,pre,post,2,smooth,grids,maxit,tol);
fprintf('W-Cycle Solve Time: %g Secondsn',cputime-t)
% F-Cycle
t=cputime;
[x_F,res_F,it_F] = multigrid(A,b,pre,post,3,smooth,grids,maxit,tol);
fprintf('F-Cycle Solve Time: %g Secondsn',cputime-t)
% max it for iterative methods
it = 100;
% Gauss-Siedel
t=cputime;
L = tril(A);
U = triu(A,1);
x_G = zeros(length(b),1);
res_G= zeros(it,1);
for g = 1:it
x_G = L(b-U*x_G);
r = b - A * x_G;
res_G(g) = norm(r);
if res_G(g) < tol
break;
end
end
fprintf('Gauss-Seidel Solve Time: %g Secondsn',cputime-t)
% Jacobi
t=cputime;
d = diag(A);
dinv = 1./d;
LU = tril(A,-1)+triu(A,1);
U = triu(A,1);
x_J = zeros(length(b),1);
res_J= zeros(it,1);
for j = 1:it
x_J = dinv.*(b-(LU*x_J));
r = b - A * x_J;
res_J(j) = norm(r);
if res_J(j) < tol
break;
end
end
fprintf('Jacobi Solve Time: %g Secondsn',cputime-t)
%% Plotting
figure;
hold on
plot(1:g,res_G(1:g),'o-','DisplayName','Guass-Seidel')
plot(1:j,res_J(1:j),'o-','DisplayName','Jacobi')
plot(1:it_V,res_V(1:it_V),'o-','DisplayName','V-Cycle Multigrid')
plot(1:it_F,res_F(1:it_F),'o-','DisplayName','F-Cycle Multigrid')
plot(1:it_W,res_W(1:it_W),'o-','DisplayName','W-Cycle Multigrid')
set(gca,'XLim',[0 100]);
grid on
legend('location','best')
ylabel('Relative Convergance')
xlabel('Iterations')
title('Convergance of Numerical System')
hold off
MULTIGRID FUNCTION
function [x,res,ii,grids] = multigrid(A,b,pre,post,cycle,smooth,grids,maxit,tol,x0,dat)
% Recursive Multigrid Algorithm, Solves Ax = b for Symmetric Diagonally
% Dominant A Matrix
%
% Author: Ben Beaudry
%
% INPUTS:
% A = A matrix (n x n)
% b = Right hand side vector (n x 1)
% pre = Number of presmoothing iterations
% post = Number of postsmoothing iterations
% cycle = Type of multigrid cycle (1=V-cycle, 2=W-cycle, 3=F-cycle)
% smooth = Smoother type (1=Jacobi, 2=Gauss-Seidel)
% grids = Max grids in cycle, used for grids level during recursion
% maxit = Max iterations of solver
% tol = Tolerance of solver
% OPTIONAL:
% x0 = Solution Guess
% NOT USER INPUT:
% dat = grids data for recursion
%
% OUTPUTS:
% x = Solution vector
% res = Residual vector
% ii = Number of top level iterations
% grids = Max grids in cycle, used for grids level during recursion
if grids==0
% solve exactly at coarsest grids
x = Ab;
res = 0;
ii = 0;
else
% inital guess
if nargin<10
x = b*0;
else
x = x0;
end
% create grids data
if nargin<11
dat = CreateGridHierarchy(A,grids,smooth);
end
% pre allocate
res = zeros(maxit,1);
% number of grids
n = length(dat.RAI);
for ii = 1:maxit
% presmoothing
x = smoothing(A,b,x,dat,smooth,grids,n,pre);
% compute residual error and retrict to coarse grids
rh = restrict(A,b,x,dat,grids,n);
% inital coarse grids guess
if ii == 1
eh = rh*0;
end
% coarse grids recursion
[eh,~,~,grids] = multigrid(dat.RAI{n-grids+1},rh,pre,post,...
cycle,smooth,grids-1,1,tol,eh,dat);
if cycle == 2 && grids ~= 1 && grids ~= n % W cycle
% prolongation / interpolate error and correct solution
x = prolong(x,dat,grids,n,eh);
% resmoothing
x = smoothing(A,b,x,dat,smooth,grids,n,1);
% compute residual error and restrict to coarse grids
rh = restrict(A,b,x,dat,grids,n);
% coarse grids recursion
[eh,~,~,grids] = multigrid(dat.RAI{n-grids+1},rh,pre,post,...
cycle,smooth,grids-1,1,tol,eh,dat);
end
if cycle == 3 && grids ~= 1 % F cycle
% prolongation / interpolate error and correct solution
x = prolong(x,dat,grids,n,eh);
% resmoothing
x = smoothing(A,b,x,dat,smooth,grids,n,1);
% compute residual error and restrict to coarse grids
rh = restrict(A,b,x,dat,grids,n);
% coarse grids recursion V cycle
[eh,~,~,grids] = multigrid(dat.RAI{n-grids+1},rh,pre,post,...
1,smooth,grids-1,1,tol,eh,dat);
end
% prolongation / interpolate error and correct solution
x = prolong(x,dat,grids,n,eh);
% postsmoothing
x = smoothing(A,b,x,dat,smooth,grids,n,post);
% compute residual
res(ii) = norm(b-A*x);
% check for convergance
if res(ii)<tol
break;
end
end
end
grids = grids+1;
end
% Function calls
function dat = CreateGridHierarchy(A,grids,smooth)
for i = 1:grids
if i == 1
if smooth == 1
% create Jacobi
dat.d{i} = diag(A);
dat.dinv{i} = 1./dat.d{i};
elseif smooth == 2
% create Gauss-Seidel
dat.L{i} = tril(A);
dat.U{i} = triu(A,1);
end
[m,~] = size(A);
else
if smooth == 1
% create Jacobi
dat.d{i} = diag(dat.RAI{i-1});
dat.dinv{i} = 1./dat.d{i};
elseif smooth == 2
% create Gauss-Seidel
dat.L{i} = tril(dat.RAI{i-1});
dat.U{i} = triu(dat.RAI{i-1},1);
end
[m,~] = size(dat.RAI{i-1});
end
% create interpolation matrices
dat.I{i} = spdiagsSpecial([1 2 1],-2:0,m);
dat.I{i} = dat.I{i}/2;
% create restriction matrices
dat.R{i} = dat.I{i}'/2;
% coarse grid matrix
if i == 1
dat.RAI{i} = dat.R{i}*A*dat.I{i};
else
dat.RAI{i} = dat.R{i}*dat.RAI{i-1}*dat.I{i};
end
end
end
function x = smoothing(A,b,x,dat,smooth,grids,n,iters)
for i = 1:iters
if smooth == 1
% Jacobi
x = x + dat.dinv{n-grids+1}.*(b-A*x);
elseif smooth == 2
% Guass-Siedel
x = dat.L{n-grids+1}(b-dat.U{n-grids+1}*x);
end
end
end
function rh = restrict(A,b,x,dat,grids,n)
r = b-A*x; % residual
rh = dat.R{n-grids+1}*r;
end
function x = prolong(x,dat,grids,n,eh)
x = x+dat.I{n-grids+1}*eh;
end
function [I] = spdiagsSpecial(v,d,n)
% modified version of spdiags to cut down on matrix size and improve
% performance
if rem(n,2)
m = (n-1)+1;
nn = (n-3)/2+1;
B = ones((n-3)/2+1,1)*v;
else
m = (n);
nn = (n-2)/2;
B = ones((n-2)/2,1)*v;
end
d = d(:);
p = length(d);
% Compute lengths of diagonals:
len = max(0, min(m, nn-d) - max(1, 1-d) + 1);
len = [0; cumsum(len)];
a = zeros(len(p+1), 3);
for k = 1:p
i = (max(1,1-d(k)):min(m,nn-d(k)))';
a((len(k)+1):len(k+1),:) = [i i+d(k) B(i+(m>=nn)*d(k),k)];
end
a(:,1) = a(:,1)+(a(:,2)-1);
I = sparse(a(:,1),a(:,2),a(:,3),m,nn);
end
Incorporate the SOR method in the multigridTest-m and apply the multig.pdf

More Related Content

Similar to Incorporate the SOR method in the multigridTest-m and apply the multig.pdf

NUMERICAL METHODS WITH MATLAB : bisection,mueller's,newton-raphson,false poin...
NUMERICAL METHODS WITH MATLAB : bisection,mueller's,newton-raphson,false poin...NUMERICAL METHODS WITH MATLAB : bisection,mueller's,newton-raphson,false poin...
NUMERICAL METHODS WITH MATLAB : bisection,mueller's,newton-raphson,false poin...
Parhamsagharchi
 
2. Fixed Point Iteration.pptx
2. Fixed Point Iteration.pptx2. Fixed Point Iteration.pptx
2. Fixed Point Iteration.pptx
saadhaq6
 
Dsp manual
Dsp manualDsp manual
Dsp manual
pramod naik
 
Consider a 4-Link robot manipulator shown below. Use the forward kine.pdf
Consider a 4-Link robot manipulator shown below. Use the forward kine.pdfConsider a 4-Link robot manipulator shown below. Use the forward kine.pdf
Consider a 4-Link robot manipulator shown below. Use the forward kine.pdf
meerobertsonheyde608
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
Lakshmi Sarvani Videla
 
scientific computing
scientific computingscientific computing
scientific computing
saurabhramteke7
 
Numerical Method Assignment
Numerical Method AssignmentNumerical Method Assignment
Numerical Method Assignment
ashikul akash
 
reservoir-modeling-using-matlab-the-matalb-reservoir-simulation-toolbox-mrst.pdf
reservoir-modeling-using-matlab-the-matalb-reservoir-simulation-toolbox-mrst.pdfreservoir-modeling-using-matlab-the-matalb-reservoir-simulation-toolbox-mrst.pdf
reservoir-modeling-using-matlab-the-matalb-reservoir-simulation-toolbox-mrst.pdf
RTEFGDFGJU
 
MATLAB for Technical Computing
MATLAB for Technical ComputingMATLAB for Technical Computing
MATLAB for Technical Computing
Naveed Rehman
 
R/Finance 2009 Chicago
R/Finance 2009 ChicagoR/Finance 2009 Chicago
R/Finance 2009 Chicago
gyollin
 
gptips1.0concrete.matConcrete_Data[1030x9 double array]tr.docx
gptips1.0concrete.matConcrete_Data[1030x9  double array]tr.docxgptips1.0concrete.matConcrete_Data[1030x9  double array]tr.docx
gptips1.0concrete.matConcrete_Data[1030x9 double array]tr.docx
whittemorelucilla
 
Comparisons
ComparisonsComparisons
ComparisonsZifirio
 
Comparisons
ComparisonsComparisons
ComparisonsZifirio
 
Comparisons
ComparisonsComparisons
ComparisonsZifirio
 
Comparisons
ComparisonsComparisons
ComparisonsZifirio
 
Comparisons
ComparisonsComparisons
ComparisonsZifirio
 
Comparisons
ComparisonsComparisons
ComparisonsZifirio
 
Comparisons
ComparisonsComparisons
ComparisonsZifirio
 
Comparisons
ComparisonsComparisons
ComparisonsZifirio
 

Similar to Incorporate the SOR method in the multigridTest-m and apply the multig.pdf (20)

NUMERICAL METHODS WITH MATLAB : bisection,mueller's,newton-raphson,false poin...
NUMERICAL METHODS WITH MATLAB : bisection,mueller's,newton-raphson,false poin...NUMERICAL METHODS WITH MATLAB : bisection,mueller's,newton-raphson,false poin...
NUMERICAL METHODS WITH MATLAB : bisection,mueller's,newton-raphson,false poin...
 
2. Fixed Point Iteration.pptx
2. Fixed Point Iteration.pptx2. Fixed Point Iteration.pptx
2. Fixed Point Iteration.pptx
 
Dsp manual
Dsp manualDsp manual
Dsp manual
 
Consider a 4-Link robot manipulator shown below. Use the forward kine.pdf
Consider a 4-Link robot manipulator shown below. Use the forward kine.pdfConsider a 4-Link robot manipulator shown below. Use the forward kine.pdf
Consider a 4-Link robot manipulator shown below. Use the forward kine.pdf
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
 
scientific computing
scientific computingscientific computing
scientific computing
 
Numerical Method Assignment
Numerical Method AssignmentNumerical Method Assignment
Numerical Method Assignment
 
reservoir-modeling-using-matlab-the-matalb-reservoir-simulation-toolbox-mrst.pdf
reservoir-modeling-using-matlab-the-matalb-reservoir-simulation-toolbox-mrst.pdfreservoir-modeling-using-matlab-the-matalb-reservoir-simulation-toolbox-mrst.pdf
reservoir-modeling-using-matlab-the-matalb-reservoir-simulation-toolbox-mrst.pdf
 
MATLAB for Technical Computing
MATLAB for Technical ComputingMATLAB for Technical Computing
MATLAB for Technical Computing
 
R/Finance 2009 Chicago
R/Finance 2009 ChicagoR/Finance 2009 Chicago
R/Finance 2009 Chicago
 
gptips1.0concrete.matConcrete_Data[1030x9 double array]tr.docx
gptips1.0concrete.matConcrete_Data[1030x9  double array]tr.docxgptips1.0concrete.matConcrete_Data[1030x9  double array]tr.docx
gptips1.0concrete.matConcrete_Data[1030x9 double array]tr.docx
 
Adaptive signal processing simon haykins
Adaptive signal processing simon haykinsAdaptive signal processing simon haykins
Adaptive signal processing simon haykins
 
Comparisons
ComparisonsComparisons
Comparisons
 
Comparisons
ComparisonsComparisons
Comparisons
 
Comparisons
ComparisonsComparisons
Comparisons
 
Comparisons
ComparisonsComparisons
Comparisons
 
Comparisons
ComparisonsComparisons
Comparisons
 
Comparisons
ComparisonsComparisons
Comparisons
 
Comparisons
ComparisonsComparisons
Comparisons
 
Comparisons
ComparisonsComparisons
Comparisons
 

More from aartechindia

Indique si cada uno de los siguientes se identifica con (1) un activo-.pdf
Indique si cada uno de los siguientes se identifica con (1) un activo-.pdfIndique si cada uno de los siguientes se identifica con (1) un activo-.pdf
Indique si cada uno de los siguientes se identifica con (1) un activo-.pdf
aartechindia
 
Indicate whether each statement applies to the equilibrium potential (.pdf
Indicate whether each statement applies to the equilibrium potential (.pdfIndicate whether each statement applies to the equilibrium potential (.pdf
Indicate whether each statement applies to the equilibrium potential (.pdf
aartechindia
 
Indirect Transfers (through an investment hankino firm) Select the thr.pdf
Indirect Transfers (through an investment hankino firm) Select the thr.pdfIndirect Transfers (through an investment hankino firm) Select the thr.pdf
Indirect Transfers (through an investment hankino firm) Select the thr.pdf
aartechindia
 
Indigo- Inc- had the following equity investment portfolio at January (1).pdf
Indigo- Inc- had the following equity investment portfolio at January (1).pdfIndigo- Inc- had the following equity investment portfolio at January (1).pdf
Indigo- Inc- had the following equity investment portfolio at January (1).pdf
aartechindia
 
Indicate whether each of the following is a revenue center- cost cente.pdf
Indicate whether each of the following is a revenue center- cost cente.pdfIndicate whether each of the following is a revenue center- cost cente.pdf
Indicate whether each of the following is a revenue center- cost cente.pdf
aartechindia
 
Increasing the amount of cholesterol in a lipid bilayer would likely Q (1).pdf
Increasing the amount of cholesterol in a lipid bilayer would likely Q (1).pdfIncreasing the amount of cholesterol in a lipid bilayer would likely Q (1).pdf
Increasing the amount of cholesterol in a lipid bilayer would likely Q (1).pdf
aartechindia
 
Indicate the sexual phenotype of Drosophila melanogaster (fruit fly) a.pdf
Indicate the sexual phenotype of Drosophila melanogaster (fruit fly) a.pdfIndicate the sexual phenotype of Drosophila melanogaster (fruit fly) a.pdf
Indicate the sexual phenotype of Drosophila melanogaster (fruit fly) a.pdf
aartechindia
 
Indicate the amount of revenue that Beanville should recognize in its (4).pdf
Indicate the amount of revenue that Beanville should recognize in its (4).pdfIndicate the amount of revenue that Beanville should recognize in its (4).pdf
Indicate the amount of revenue that Beanville should recognize in its (4).pdf
aartechindia
 
Indicate the amount of revenue that Beanville should recognize in its (1).pdf
Indicate the amount of revenue that Beanville should recognize in its (1).pdfIndicate the amount of revenue that Beanville should recognize in its (1).pdf
Indicate the amount of revenue that Beanville should recognize in its (1).pdf
aartechindia
 
India's major change in development strategy occurred in 1991 because.pdf
India's major change in development strategy occurred in 1991 because.pdfIndia's major change in development strategy occurred in 1991 because.pdf
India's major change in development strategy occurred in 1991 because.pdf
aartechindia
 
Incubator operators try to pick those businesses that have the best Fi.pdf
Incubator operators try to pick those businesses that have the best Fi.pdfIncubator operators try to pick those businesses that have the best Fi.pdf
Incubator operators try to pick those businesses that have the best Fi.pdf
aartechindia
 
Indexes and registries allow health information to be maintained and r.pdf
Indexes and registries allow health information to be maintained and r.pdfIndexes and registries allow health information to be maintained and r.pdf
Indexes and registries allow health information to be maintained and r.pdf
aartechindia
 
Incomplete (missing items) vs Delinquent (fallen beyond the record tim.pdf
Incomplete (missing items) vs Delinquent (fallen beyond the record tim.pdfIncomplete (missing items) vs Delinquent (fallen beyond the record tim.pdf
Incomplete (missing items) vs Delinquent (fallen beyond the record tim.pdf
aartechindia
 
Increasingly consumers are using social media to voice complaints that.pdf
Increasingly consumers are using social media to voice complaints that.pdfIncreasingly consumers are using social media to voice complaints that.pdf
Increasingly consumers are using social media to voice complaints that.pdf
aartechindia
 
income seatement The folowing account balanoes were taken trom the adj.pdf
income seatement The folowing account balanoes were taken trom the adj.pdfincome seatement The folowing account balanoes were taken trom the adj.pdf
income seatement The folowing account balanoes were taken trom the adj.pdf
aartechindia
 
In _________ as the mitotic spindles move to opposite sides of the cel.pdf
In _________ as the mitotic spindles move to opposite sides of the cel.pdfIn _________ as the mitotic spindles move to opposite sides of the cel.pdf
In _________ as the mitotic spindles move to opposite sides of the cel.pdf
aartechindia
 
Inatech is contemplating two different projects and decides to perform.pdf
Inatech is contemplating two different projects and decides to perform.pdfInatech is contemplating two different projects and decides to perform.pdf
Inatech is contemplating two different projects and decides to perform.pdf
aartechindia
 
in xv6 system i have a program in c that shows process state- but afte.pdf
in xv6 system i have a program in c that shows process state- but afte.pdfin xv6 system i have a program in c that shows process state- but afte.pdf
in xv6 system i have a program in c that shows process state- but afte.pdf
aartechindia
 
In which of the following situations is a worker eligible to receive u.pdf
In which of the following situations is a worker eligible to receive u.pdfIn which of the following situations is a worker eligible to receive u.pdf
In which of the following situations is a worker eligible to receive u.pdf
aartechindia
 
In your own words- explain what a lahar is- Question 2 (1 point) Compa.pdf
In your own words- explain what a lahar is- Question 2 (1 point) Compa.pdfIn your own words- explain what a lahar is- Question 2 (1 point) Compa.pdf
In your own words- explain what a lahar is- Question 2 (1 point) Compa.pdf
aartechindia
 

More from aartechindia (20)

Indique si cada uno de los siguientes se identifica con (1) un activo-.pdf
Indique si cada uno de los siguientes se identifica con (1) un activo-.pdfIndique si cada uno de los siguientes se identifica con (1) un activo-.pdf
Indique si cada uno de los siguientes se identifica con (1) un activo-.pdf
 
Indicate whether each statement applies to the equilibrium potential (.pdf
Indicate whether each statement applies to the equilibrium potential (.pdfIndicate whether each statement applies to the equilibrium potential (.pdf
Indicate whether each statement applies to the equilibrium potential (.pdf
 
Indirect Transfers (through an investment hankino firm) Select the thr.pdf
Indirect Transfers (through an investment hankino firm) Select the thr.pdfIndirect Transfers (through an investment hankino firm) Select the thr.pdf
Indirect Transfers (through an investment hankino firm) Select the thr.pdf
 
Indigo- Inc- had the following equity investment portfolio at January (1).pdf
Indigo- Inc- had the following equity investment portfolio at January (1).pdfIndigo- Inc- had the following equity investment portfolio at January (1).pdf
Indigo- Inc- had the following equity investment portfolio at January (1).pdf
 
Indicate whether each of the following is a revenue center- cost cente.pdf
Indicate whether each of the following is a revenue center- cost cente.pdfIndicate whether each of the following is a revenue center- cost cente.pdf
Indicate whether each of the following is a revenue center- cost cente.pdf
 
Increasing the amount of cholesterol in a lipid bilayer would likely Q (1).pdf
Increasing the amount of cholesterol in a lipid bilayer would likely Q (1).pdfIncreasing the amount of cholesterol in a lipid bilayer would likely Q (1).pdf
Increasing the amount of cholesterol in a lipid bilayer would likely Q (1).pdf
 
Indicate the sexual phenotype of Drosophila melanogaster (fruit fly) a.pdf
Indicate the sexual phenotype of Drosophila melanogaster (fruit fly) a.pdfIndicate the sexual phenotype of Drosophila melanogaster (fruit fly) a.pdf
Indicate the sexual phenotype of Drosophila melanogaster (fruit fly) a.pdf
 
Indicate the amount of revenue that Beanville should recognize in its (4).pdf
Indicate the amount of revenue that Beanville should recognize in its (4).pdfIndicate the amount of revenue that Beanville should recognize in its (4).pdf
Indicate the amount of revenue that Beanville should recognize in its (4).pdf
 
Indicate the amount of revenue that Beanville should recognize in its (1).pdf
Indicate the amount of revenue that Beanville should recognize in its (1).pdfIndicate the amount of revenue that Beanville should recognize in its (1).pdf
Indicate the amount of revenue that Beanville should recognize in its (1).pdf
 
India's major change in development strategy occurred in 1991 because.pdf
India's major change in development strategy occurred in 1991 because.pdfIndia's major change in development strategy occurred in 1991 because.pdf
India's major change in development strategy occurred in 1991 because.pdf
 
Incubator operators try to pick those businesses that have the best Fi.pdf
Incubator operators try to pick those businesses that have the best Fi.pdfIncubator operators try to pick those businesses that have the best Fi.pdf
Incubator operators try to pick those businesses that have the best Fi.pdf
 
Indexes and registries allow health information to be maintained and r.pdf
Indexes and registries allow health information to be maintained and r.pdfIndexes and registries allow health information to be maintained and r.pdf
Indexes and registries allow health information to be maintained and r.pdf
 
Incomplete (missing items) vs Delinquent (fallen beyond the record tim.pdf
Incomplete (missing items) vs Delinquent (fallen beyond the record tim.pdfIncomplete (missing items) vs Delinquent (fallen beyond the record tim.pdf
Incomplete (missing items) vs Delinquent (fallen beyond the record tim.pdf
 
Increasingly consumers are using social media to voice complaints that.pdf
Increasingly consumers are using social media to voice complaints that.pdfIncreasingly consumers are using social media to voice complaints that.pdf
Increasingly consumers are using social media to voice complaints that.pdf
 
income seatement The folowing account balanoes were taken trom the adj.pdf
income seatement The folowing account balanoes were taken trom the adj.pdfincome seatement The folowing account balanoes were taken trom the adj.pdf
income seatement The folowing account balanoes were taken trom the adj.pdf
 
In _________ as the mitotic spindles move to opposite sides of the cel.pdf
In _________ as the mitotic spindles move to opposite sides of the cel.pdfIn _________ as the mitotic spindles move to opposite sides of the cel.pdf
In _________ as the mitotic spindles move to opposite sides of the cel.pdf
 
Inatech is contemplating two different projects and decides to perform.pdf
Inatech is contemplating two different projects and decides to perform.pdfInatech is contemplating two different projects and decides to perform.pdf
Inatech is contemplating two different projects and decides to perform.pdf
 
in xv6 system i have a program in c that shows process state- but afte.pdf
in xv6 system i have a program in c that shows process state- but afte.pdfin xv6 system i have a program in c that shows process state- but afte.pdf
in xv6 system i have a program in c that shows process state- but afte.pdf
 
In which of the following situations is a worker eligible to receive u.pdf
In which of the following situations is a worker eligible to receive u.pdfIn which of the following situations is a worker eligible to receive u.pdf
In which of the following situations is a worker eligible to receive u.pdf
 
In your own words- explain what a lahar is- Question 2 (1 point) Compa.pdf
In your own words- explain what a lahar is- Question 2 (1 point) Compa.pdfIn your own words- explain what a lahar is- Question 2 (1 point) Compa.pdf
In your own words- explain what a lahar is- Question 2 (1 point) Compa.pdf
 

Recently uploaded

Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
JEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questionsJEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questions
ShivajiThube2
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 

Recently uploaded (20)

Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
JEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questionsJEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questions
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 

Incorporate the SOR method in the multigridTest-m and apply the multig.pdf

  • 1. Incorporate the SOR method in the multigridTest.m and apply the multigridTest.m, to determine the effects of changes in Pre-smoothing iterations Post-smoothing iterations Multigrid cycle type Iterative method Number of grids (or grid hierarchy) % SOR (Successive Over-Relaxation) n = input('Enter number of equations, n: '); A = zeros(n,n+1); x1 = zeros(1,n); A=[5 -2 3 -1;-3 9 1 2;2 -1 -7 3]; x1 = [0 0 0]; tol = input('Enter tolerance, tol: '); m = input('Enter maximum number of iterations, m: '); w = input('Enter the parameter w (omega): '); k = 1; while k <= m err = 0; for i = 1 : n s = 0; for j = 1 : n s = s-A(i,j)*x1(j); end
  • 2. s = w*(s+A(i,n+1))/A(i,i); if abs(s) > err err = abs(s); end x1(i) = x1(i)+s; end if err <= tol break; else k = k+1; end end fprintf('The solution vector after %d iterations is :n', k); for i = 1 : n fprintf(' %11.8f n', x1(i)); end %% Multigrid test case with visulization % Author: Ben Beaudry clc; clear; close all load A_b.mat; A = full(A); % Unsparce matrix to show full power of Multigrid pre = 2; % Number of presmoothing iterations post = 2; % Number of postsmoothing iterations
  • 3. % cycle = 1; % Type of multigrid cycle (1=V-cycle, 2=W-cycle, 3=F-cycle) smooth = 1; % Smoother type (1=Jacobi, 2=Gauss-Seidel) grids = 5; % Max grids in cycle maxit = 10; % Max iterations of solver tol = 1e-02; % Tolerance of solver %% Solvers % solve directly t=cputime; x_D = Ab; fprintf('Direct Solve Time: %g Secondsn',cputime-t) % V-Cycle t=cputime; [x_V,res_V,it_V] = multigrid(A,b,pre,post,1,smooth,grids,maxit,tol); fprintf('V-Cycle Solve Time: %g Secondsn',cputime-t) % W-Cycle t=cputime; [x_W,res_W,it_W] = multigrid(A,b,pre,post,2,smooth,grids,maxit,tol); fprintf('W-Cycle Solve Time: %g Secondsn',cputime-t) % F-Cycle t=cputime; [x_F,res_F,it_F] = multigrid(A,b,pre,post,3,smooth,grids,maxit,tol); fprintf('F-Cycle Solve Time: %g Secondsn',cputime-t) % max it for iterative methods
  • 4. it = 100; % Gauss-Siedel t=cputime; L = tril(A); U = triu(A,1); x_G = zeros(length(b),1); res_G= zeros(it,1); for g = 1:it x_G = L(b-U*x_G); r = b - A * x_G; res_G(g) = norm(r); if res_G(g) < tol break; end end fprintf('Gauss-Seidel Solve Time: %g Secondsn',cputime-t) % Jacobi t=cputime; d = diag(A); dinv = 1./d; LU = tril(A,-1)+triu(A,1); U = triu(A,1); x_J = zeros(length(b),1);
  • 5. res_J= zeros(it,1); for j = 1:it x_J = dinv.*(b-(LU*x_J)); r = b - A * x_J; res_J(j) = norm(r); if res_J(j) < tol break; end end fprintf('Jacobi Solve Time: %g Secondsn',cputime-t) %% Plotting figure; hold on plot(1:g,res_G(1:g),'o-','DisplayName','Guass-Seidel') plot(1:j,res_J(1:j),'o-','DisplayName','Jacobi') plot(1:it_V,res_V(1:it_V),'o-','DisplayName','V-Cycle Multigrid') plot(1:it_F,res_F(1:it_F),'o-','DisplayName','F-Cycle Multigrid') plot(1:it_W,res_W(1:it_W),'o-','DisplayName','W-Cycle Multigrid') set(gca,'XLim',[0 100]); grid on legend('location','best') ylabel('Relative Convergance') xlabel('Iterations')
  • 6. title('Convergance of Numerical System') hold off MULTIGRID FUNCTION function [x,res,ii,grids] = multigrid(A,b,pre,post,cycle,smooth,grids,maxit,tol,x0,dat) % Recursive Multigrid Algorithm, Solves Ax = b for Symmetric Diagonally % Dominant A Matrix % % Author: Ben Beaudry % % INPUTS: % A = A matrix (n x n) % b = Right hand side vector (n x 1) % pre = Number of presmoothing iterations % post = Number of postsmoothing iterations % cycle = Type of multigrid cycle (1=V-cycle, 2=W-cycle, 3=F-cycle) % smooth = Smoother type (1=Jacobi, 2=Gauss-Seidel) % grids = Max grids in cycle, used for grids level during recursion % maxit = Max iterations of solver % tol = Tolerance of solver % OPTIONAL: % x0 = Solution Guess % NOT USER INPUT: % dat = grids data for recursion
  • 7. % % OUTPUTS: % x = Solution vector % res = Residual vector % ii = Number of top level iterations % grids = Max grids in cycle, used for grids level during recursion if grids==0 % solve exactly at coarsest grids x = Ab; res = 0; ii = 0; else % inital guess if nargin<10 x = b*0; else x = x0; end % create grids data if nargin<11 dat = CreateGridHierarchy(A,grids,smooth); end % pre allocate
  • 8. res = zeros(maxit,1); % number of grids n = length(dat.RAI); for ii = 1:maxit % presmoothing x = smoothing(A,b,x,dat,smooth,grids,n,pre); % compute residual error and retrict to coarse grids rh = restrict(A,b,x,dat,grids,n); % inital coarse grids guess if ii == 1 eh = rh*0; end % coarse grids recursion [eh,~,~,grids] = multigrid(dat.RAI{n-grids+1},rh,pre,post,... cycle,smooth,grids-1,1,tol,eh,dat); if cycle == 2 && grids ~= 1 && grids ~= n % W cycle % prolongation / interpolate error and correct solution x = prolong(x,dat,grids,n,eh); % resmoothing x = smoothing(A,b,x,dat,smooth,grids,n,1); % compute residual error and restrict to coarse grids rh = restrict(A,b,x,dat,grids,n); % coarse grids recursion
  • 9. [eh,~,~,grids] = multigrid(dat.RAI{n-grids+1},rh,pre,post,... cycle,smooth,grids-1,1,tol,eh,dat); end if cycle == 3 && grids ~= 1 % F cycle % prolongation / interpolate error and correct solution x = prolong(x,dat,grids,n,eh); % resmoothing x = smoothing(A,b,x,dat,smooth,grids,n,1); % compute residual error and restrict to coarse grids rh = restrict(A,b,x,dat,grids,n); % coarse grids recursion V cycle [eh,~,~,grids] = multigrid(dat.RAI{n-grids+1},rh,pre,post,... 1,smooth,grids-1,1,tol,eh,dat); end % prolongation / interpolate error and correct solution x = prolong(x,dat,grids,n,eh); % postsmoothing x = smoothing(A,b,x,dat,smooth,grids,n,post); % compute residual res(ii) = norm(b-A*x); % check for convergance if res(ii)<tol break;
  • 10. end end end grids = grids+1; end % Function calls function dat = CreateGridHierarchy(A,grids,smooth) for i = 1:grids if i == 1 if smooth == 1 % create Jacobi dat.d{i} = diag(A); dat.dinv{i} = 1./dat.d{i}; elseif smooth == 2 % create Gauss-Seidel dat.L{i} = tril(A); dat.U{i} = triu(A,1); end [m,~] = size(A); else if smooth == 1 % create Jacobi dat.d{i} = diag(dat.RAI{i-1});
  • 11. dat.dinv{i} = 1./dat.d{i}; elseif smooth == 2 % create Gauss-Seidel dat.L{i} = tril(dat.RAI{i-1}); dat.U{i} = triu(dat.RAI{i-1},1); end [m,~] = size(dat.RAI{i-1}); end % create interpolation matrices dat.I{i} = spdiagsSpecial([1 2 1],-2:0,m); dat.I{i} = dat.I{i}/2; % create restriction matrices dat.R{i} = dat.I{i}'/2; % coarse grid matrix if i == 1 dat.RAI{i} = dat.R{i}*A*dat.I{i}; else dat.RAI{i} = dat.R{i}*dat.RAI{i-1}*dat.I{i}; end end end function x = smoothing(A,b,x,dat,smooth,grids,n,iters) for i = 1:iters
  • 12. if smooth == 1 % Jacobi x = x + dat.dinv{n-grids+1}.*(b-A*x); elseif smooth == 2 % Guass-Siedel x = dat.L{n-grids+1}(b-dat.U{n-grids+1}*x); end end end function rh = restrict(A,b,x,dat,grids,n) r = b-A*x; % residual rh = dat.R{n-grids+1}*r; end function x = prolong(x,dat,grids,n,eh) x = x+dat.I{n-grids+1}*eh; end function [I] = spdiagsSpecial(v,d,n) % modified version of spdiags to cut down on matrix size and improve % performance if rem(n,2) m = (n-1)+1; nn = (n-3)/2+1; B = ones((n-3)/2+1,1)*v;
  • 13. else m = (n); nn = (n-2)/2; B = ones((n-2)/2,1)*v; end d = d(:); p = length(d); % Compute lengths of diagonals: len = max(0, min(m, nn-d) - max(1, 1-d) + 1); len = [0; cumsum(len)]; a = zeros(len(p+1), 3); for k = 1:p i = (max(1,1-d(k)):min(m,nn-d(k)))'; a((len(k)+1):len(k+1),:) = [i i+d(k) B(i+(m>=nn)*d(k),k)]; end a(:,1) = a(:,1)+(a(:,2)-1); I = sparse(a(:,1),a(:,2),a(:,3),m,nn); end