Problem 8. Define a function gs1_error(n) that accepts as input a non-negative integer n and outputs the error of the Gauss-Seidel approximation (xn, yn) to the solution of system (1). In other words, it should output the distance between the nth approximation (xn, yn) and the true value (x_val,y_val) of the solution to (1).

Answer :

DayyanKhan

Answer:

The MATLAB code will be:

close all;

clc;

clear all;

 

a = [20, 1, -2; 3, 20, -1; 2,-3, 20];

b = [17; -18; 25];

 

tol = 1e-6;

 

[M,N] = size(a);

if M~=N

   error('A is not a square Matrix');

end

 

x = [0,0,0];

xx(1,:) = x;

n = 100;

error = 0.00001;

 

for k = 2:n

   for i = 1:N

       s = 0;

       for j = 1:N

           if j ~= i

               s = s + a(i,j) * x(j);

           end

       end

       x(i) = (1/a(i,i)*(b(i) - s));

   end

   xx(k,:) = x;kk = k;

   Error = abs(max(xx(kk,:)- xx(k-1,:)))

   if Error<error

       break;

   end

end

 

fprintf ('The roots of equation are: ')

x

 

fprintf ('The number of iterations are: ')

k

 

fprintf ('The error is: ')

Error

Explanation:

Other Questions