SlideShare a Scribd company logo
1 of 14
Download to read offline
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.pptxsaadhaq6
 
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.pdfmeerobertsonheyde608
 
Numerical Method Assignment
Numerical Method AssignmentNumerical Method Assignment
Numerical Method Assignmentashikul 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.pdfRTEFGDFGJU
 
MATLAB for Technical Computing
MATLAB for Technical ComputingMATLAB for Technical Computing
MATLAB for Technical ComputingNaveed Rehman
 
R/Finance 2009 Chicago
R/Finance 2009 ChicagoR/Finance 2009 Chicago
R/Finance 2009 Chicagogyollin
 
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.docxwhittemorelucilla
 
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-.pdfaartechindia
 
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 (.pdfaartechindia
 
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.pdfaartechindia
 
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).pdfaartechindia
 
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.pdfaartechindia
 
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).pdfaartechindia
 
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.pdfaartechindia
 
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).pdfaartechindia
 
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).pdfaartechindia
 
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.pdfaartechindia
 
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.pdfaartechindia
 
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.pdfaartechindia
 
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.pdfaartechindia
 
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.pdfaartechindia
 
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.pdfaartechindia
 
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.pdfaartechindia
 
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.pdfaartechindia
 
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.pdfaartechindia
 
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.pdfaartechindia
 
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.pdfaartechindia
 

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

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 

Recently uploaded (20)

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 

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