Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please using matlab and show your code 6. (30 points) (COMPUTATIONAL PROBLEM) Co

ID: 2966977 • Letter: P

Question

Please using matlab and show your code

6. (30 points) (COMPUTATIONAL PROBLEM) Consider the sparse linear system AF where (1 0 0 0 0 0) -2/h2 -2/h2 b ERN, NxN. 2/h 2/h nh) nh(1 R. IN-1 IN (a) (10 points) solve this linear system with the Jacobi Method for N 10. How many iterations are required to realize a relative forward error (in the infinity norm) of T 10-4? (b) (10 points) Repeat part (a) with the Gauss-Seidel Method. (c) (0 points) Repeat part (a) with the SOR Method using the seventeen (17 values 1.6, 1.7, 1.8 w 0.2, 0.3, 0.4

Explanation / Answer

% -------------------Jacobian-------------------------
clear all;
clc;
N = 10;
% create diagonal vector - v
v = [1; -2; -2; -2; -2; -2; -2; -2; -2; 1];
% create upper and lower diagonal vectors
v1 = [0; 1; 1; 1; 1; 1; 1; 1; 1];
v2 = [1; 1; 1; 1; 1; 1; 1; 1; 0];
% create diagonal matrix - D and remainder matrix - R
D = diag(v);
R = diag(v1,1) + diag(v2,-1);
% create b
h = 1/(N-1);
b = (-2*h^2) * ones(10,1);
b(1) = 0;
b(10) = 0;
% create solution vector
x_sol = zeros(10,1);
for i = 1:9
    x_sol(i+1) = i*h*(1-i*h);
end
% create an initial guess
x0 = zeros(10,1);
% create another vector which will store updated solution
x1 = zeros(10,1);
% counter for number of iterations
iterations = 0;
while 1
    iterations = iterations+1;
    x1 = D(b - R*x0);
    % check for relative forward error
    if norm(x1 - x_sol,Inf) < 1e-4
        break;
    end
    % update the previous estimate x0
    x0 = x1;
  
end

% ------------------gauss siedel------------------------
clear all;
clc;
N = 10;
% create A
A = zeros(10,10);
A(1,1) = 1;
A(10,10) = 1;
for j = 2:9
    A(j,j) = -2;
end
for j = 1:8
    A(j+1,j) = 1;
end
for j = 2:9
    A(j,j+1) = 1;
end
% create b
h = 1/(N-1);
b = (-2*h^2) * ones(10,1);
b(1) = 0;
b(10) = 0;
% create solution vector
x_sol = zeros(10,1);
for i = 1:9
    x_sol(i+1) = i*h*(1-i*h);
end
% create an initial guess
x = zeros(10,1);

% counter for number of iterations
iterations = 0;
while 1
    iterations = iterations+1;
    for i = 1:10
        sum = 0;
        for j = 1:10
            if j ~= i
                sum = sum + A(i,j)*x(j);
            end
        end
        x(i) = (b(i) - sum)/A(i,i);
    end
    % check for relative forward error
    if norm(x - x_sol,Inf) < 1e-4
        break;
    end
  
  
end

% ----------------------SOR----------------------------
clear all;
clc;
N = 10;
w = 1.5;
% create A
A = zeros(10,10);
A(1,1) = 1;
A(10,10) = 1;
for j = 2:9
    A(j,j) = -2;
end
for j = 1:8
    A(j+1,j) = 1;
end
for j = 2:9
    A(j,j+1) = 1;
end
% create b
h = 1/(N-1);
b = (-2*h^2) * ones(10,1);
b(1) = 0;
b(10) = 0;
% create solution vector
x_sol = zeros(10,1);
for i = 1:9
    x_sol(i+1) = i*h*(1-i*h);
end
% create an initial guess
x = zeros(10,1);

% counter for number of iterations
iterations = 0;
while 1
    iterations = iterations+1;
    for i = 1:10
        sum = 0;
        for j = 1:10
            if j ~= i
                sum = sum + A(i,j)*x(j);
            end
        end
        x(i) = (1-w)*x(i) + w*(b(i) - sum)/A(i,i);
    end
    % check for relative forward error
    if norm(x - x_sol,Inf) < 1e-4
        break;
    end
  
    end