Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

MATLAB Code for Jacobi-

clc;
clear all;
clearvars;
N = 20;
n = N^2;
count=1;
for i = 1:n
for j =1:n
if(i==j)
D(i,j) = 4;
end
end
end % generated D matrix with all diagonal elements 4 (given)
S=zeros(4,4); % Generating a 4x4 zero matrix S to get A
for t=1:N:(n)-N
for i=t:t+N-1
if(i<t+N-1)
S(i,i+1)=-1;
end
if(i>t)
S(i,i-1)=-1;
end
S(i,i+N)=-1;
S(i+N,i)=-1;
end
end % Making the value "-1" to all the elements above
% and below diagonal elements (as given in Question)

for i=(n)-N+1 : (n)


if(i<n)
S(i,i+1)=-1;
end
if(i>((n)-N)+1)
S(i,i-1)=-1;
end
end
% We have to make 0s at repeatation of (n-1) times
% to the previously generated two '-1' diagonals
A=S+D; % Required A matrix has been generated

%Trying to generate B matrix

B = zeros(n,1); % B matrix of nx1 with all elements zeros


B(1,1) = 2; % 1st element of matrix B must be 2 (given)
B(n,1) = 2; % last element of matrix B must be 2 (given)
for i = 1:(n-2)
b=0.2;
B(i+1,1) = b; %Making all other values of matrix B 0.2
end

%Generating Inverse of matrix D


for i =1:n
for j=1:n
if ((i==j))
D_inv(i,j) = 1/D(i,j);
end
end
end

error = zeros(n,1); % nx1 error Matrix


x0 = zeros(n,1); % initial value of solution x(0) is all zeros
x = x0; %putting initiak values to actual solution matrix
i = 1;
dum = 0; %dummy variable for while loop

while (dum==0)
x = (D_inv)*(B-S*x0); %Mathematical expression for Jacobii
error = x - x0; %Generating error= new value- previous value
error_t = error'; %creating transpose of error matrix

sum(count) = sqrt(error_t*error); %Norm of error matrix by taking inner product with transpose
x0 = x; % Putting newly generated solution matrix to prev value for next iteration
if (sum(count) < 10^-5) % if error matrix stored in sum is 10^-5
dum = 1; %again enter inside while loop
else
dum = 0; %come outside while loop
end
count = count + 1; %increase count value
end
figure(1);
plot(sum);

MATLAB Code for Richardson-

clc;
clear all;
clearvars;
N = 20;
n = N^2;
count = 1;
for i = 1:n
for j =1:n
if(i==j)
D(i,j) = 4;
end
end
end % generated D matrix with all diagonal elements 4 (given)
S=zeros(4,4); % Generating a 4x4 zero matrix S to get A
for t=1:N:(n)-N
for i=t:t+N-1
if(i<t+N-1)
S(i,i+1)=-1;
end
if(i>t)
S(i,i-1)=-1;
end
S(i,i+N)=-1;
S(i+N,i)=-1;
end
end % Making the value "-1" to all the elements above
% and below diagonal elements (as given in Question)

for i=(n)-N+1 : (n)


if(i<n)
S(i,i+1)=-1;
end
if(i>((n)-N)+1)
S(i,i-1)=-1;
end
end
% We have to make 0s at repeatation of (n-1) times
% to the previously generated two '-1' diagonals

A=S+D; % Required A matrix has been generated

%Trying to generate B matrix

B = zeros(n,1); % B matrix of nx1 with all elements zeros


B(1,1) = 2; % 1st element of matrix B must be 2 (given)
B(n,1) = B(1,1); % last element of matrix B must be 2 (given)
for i = 1:(n-2)
b=0.2;
B(i+1,1) = b; %Making all other values of matrix B 0.2
end

error = zeros(n,1); % nx1 error Matrix


x0 = 4*ones(n,1); % initial value of solution x(0) is all zeros
x = x0; %putting initiak values to actual solution matrix
i = 1;
dum = 0; %dummy variable for while loop
w = 0.2; % any number less than 1 (for richardson formula)
while (dum == 0)
x = x0 + w*(B-A*x0); %Mathematical expression for Richardson
error = B - A*x; %Generating error= new value- previous value
error_t = error'; %creating transpose of error matrix
sum(count) = sqrt(error_t*error); %Norm of error matrix by taking inner product with transpose
x0 = x;% Putting newly generated solution matrix to prev value for next iteration
if (sum(count) < 10^-5) % if error matrix stored in sum is 10^-5
dum = 1; %again enter inside while loop
else
dum = 0; %come outside while loop
end
count = count + 1; %increase count value
end
figure();
plot(sum);

You might also like