You are on page 1of 6

MATLAB Answers

Austin Brockner

Error In Jacobi Iterative Method

Asked by Austin Brockner on 8 Mar 2015

Latest activity Answered by Torsten on 9 Mar 2015

31 views (last 30 days)

I am trying to do part (a) but am having troubles with my code. My solution error is ~300% because my b
matrix (the updated u_h(x_i,y_j) matrix) is not calculating properly.

It ends on b =

0 0 0 0

0 -1.1111 -1.3333 0.1111

0 -1.3333 -1.4444 0.4444

0 0.1111 0.4444 1.0000

where the actual solution matrix is T =

0 0 0 0

0 0.0123 0.0494 0.1111

0 0.0494 0.1975 0.4444

0 0.1111 0.4444 1.0000

I am not sure where I am making my mistake and I've been trying to fix it for hours now, any help is
appreciated. Attached is my code:

function WIP( maxit,n,eps )

h = 1/(n-1); % Grid Spacing

x = (0:h:1); % Define x vector

y = (0:h:1); % Define y vector


tol = eps; % Tolerance

k = 0; % Iteration counter

a = zeros(n,n); % Define residual size

b = zeros(n,n); % Define solution size

T = zeros(n,n); % Define true solution size

f = @(X,Y) X^2*Y^2; % Function

g = @(X,Y) 2*(X^2+Y^2); % Second Derivative of Function

done = false; % Boolean for Iterative Loop

c = 0; % Define the constant c

for k = 1:n %Define Boundary Values

a(1,k) = f(x(1),y(k));

a(n,k) = f(x(n),y(k));

a(k,1) = f(x(k),y(1));

a(k,n) = f(x(k),y(n));

b(1,k) = f(x(1),y(k));

b(n,k) = f(x(n),y(k));

b(k,1) = f(x(k),y(1));

b(k,n) = f(x(k),y(n));

end

for i = 1:n %True Solution

for j = 1:n

T(i,j) = f(x(i),y(j));

end

end

for i = 1:n %INITIAL VALUE OF APPROXIMATION


for j = 1:n

b(i,j) = (1-x(i))*a(1,j) + x(i)*a(n,j) + (1-y(j))*a(i,1) + ...

y(j)*a(i,n) - (1-y(j))*(1-x(i))*a(1,1) - (1-y(j))*x(i)*a(n,1) ...

- y(j)*(1-x(i))*a(1,n) - x(i)*y(j)*a(n,n);

end

end

while ~done %While Loop To Solve Poisson 2D Unit Square

denom = norm((b-a),inf); %Difference in solution before Jacobi

k = k+1; %Increase Iteration Counter

a = b; %Update a matrix every iteration

for j=2:n-1 %Jacobi Method

for i=2:n-1

b(i,j) = (a(i+1,j) + a(i,j+1) + a(i-1,j) + a(i,j-1) - (h^2)*g(i,j))/4;

end

end

numer = norm((b-a),inf); %Difference in solution after Jacobi

if c < (numer/denom) && (numer/denom) < 1 %Constant C calculation

c = numer/denom;

end

if numer < tol %End While Loop If tolerance is reached

done = true;

end;

if k > maxit %End While Loop If Max Iteration is reached

done = true;

end
end

%Print Important Variables

fprintf('\n\nGAUSS-JACOBI METHOD\n\n');

fprintf('N = %d\n\n',n);

fprintf('Number of Iterations = %d\n\n',k);

fprintf('maximum c = %.16f\n\n',c);

est_error = (c/(1-c))*norm((b-a),inf);

true_error = norm((T-b),inf);

fprintf('Estimated Error = %.16f',est_error);

fprintf('\n\nTrue Error = %

%% Jacobi's Iterative Method

function Jacobi_Method()

%This function uses Jacobi's iterative method to solve the variable in a

%system of equations

%% -----INPUT-----

%Input A-matrix (square)

A = [ ];

%Input corresponding b-matrix (column)

b = [];

%Specify number of iterations to run

itr = 15;
%Unless otherwise specified, the intitial guess x_0 will be all zeros

x0 = zeros(1,length(A));

%% -----SOLUTION-----

n = length(A);

%To keep track of the results, we define a matrix (x) as

x = [x0;zeros(itr,n)];

%The iterative method below will run for the specified number of iteration

%above, calculate, and tabulate the values of x^k.

for k = 1:itr

for i = 1:n

%We define a variable "sigma" that will be used to sum the values

%that are known (not on the main diagonal of the matrix) for each

%row of the A-matrix (performing the calculations for each

%equation)

sigma = 0;

for j = 1:n

%Because the coefficient along the main diagonal is paired with

%the x_i we are solving for, it is omitted from the sum

if i~=j

sigma = sigma + A(i,j)*x(k,j);


end

end

%Lastly, the x_i is calculated from the equation and recorded under

%its respective iteration

x(k+1,i) = (b(i)-sigma)/A(i,i);

end

end

%% -----OUTPUT-----

%Output created for 3x3 A-matrix

%For output purposes, 'k' will be defined to show the iteration number

k = [0:itr]';

fprintf('\nSolution of the system is : \nx_1 =%9.5f \nx_2 =%9.5f \nx_3 =%9.5f \n', x(end,:))

table(k,x(:,1),x(:,2),x(:,3),...

'VariableNames',{'k','x_1','x_2','x_3'})

figure(1); hold on

plot(k,x(:,1),k,x(:,2),k,x(:,3))

legend('x_1','x_2','x_3')

title('"x" Value Trends Over Iterations "k"')

xlabel('k')

ylabel('x')

hold of

You might also like