Please write program using MATLAB. Thanks! Write a program for Gauss-Seidel iter
ID: 3791814 • Letter: P
Question
Please write program using MATLAB.
Thanks!
Write a program for Gauss-Seidel iteration. Write a program for Gauss-Seidel iteration. Apply the program A(t)x = b, to starting from [0 0 0]^T, where A(t) = [1 t t t 1 t t t 1], b = [2 2 2]. For t = 0.2, 0.5, 0.8, 0.9 determine the number of steps to obtain the exact solution to 6S and the corresponding spectral radius of C. Graph the number of steps and the spectral radius as functions of t and comment. Successive overrelaxation (SOR). Show that by adding and subtracting x^(m) on the right, formula (6) can be written x^(m+1) = x^(m) + b - Lx^(m+1) - (U + I) x^(m) (a_jj = 1). Anticipation of further corrections motivates the introduction of an overrelaxation factor omega > 1 to get the SOR formula for Gauss-Seidel x^(m+1) = x^(m) + omega(b - Lx^(m + 1) -(U + I)x^(m) (a_jj = 1) intended to give more rapid convergence. A recommended value is omega = 2/(1 + Squareroot - rho), where rho is the spectral radius of C in (7). Apply SOR to the matrix in for t = 0.5 and 0.8 and notice the improvement of convergence. (Spectacular gains are made with larger systems.)Explanation / Answer
GAUSS SIEDAL METHOD:-
func[x,i]=gaussseidel(M,n,x0,tot)
x2=x0;
cart = 0;
val=diag(diag(M));
val1=triu(M-val);
disp(val1);
val2=tril(M-val);
disp(val2)
val3=diag(diag(M));
disp(val3);
Inv=inv(val3+val);
error=inf;
while error>tot
x1=x2;
x2=Inv*(n-(val1*x1));
error= max(abs(x2-x1)/abs(x1));
cart=cart+1;
end
x=x2;
i=cart;
end
SOR METHOD:-
func[x,i]=sor(M,n,x0,tot,omega)
[m,n]=size(M);
val = diag(diag(M));
val1=triu(M-val);
val2=(M-val);
cart=1;
xtab=x0;
w=omega;
if size(n) ~=size(x0)
error("the given approximation vector no match");
elseif m~=n
error("the matrix is not a square");
else
xnew = (inv(val+w*val2))*(((1-w)*val-w*val1)*x0+w*n);
relerror=(abs(xnew-x0))/(abs(xnew));
relerrorcol=max(max(relerror));
while relerrorcol>tot
xnew=(inv(val+w*val2))*(((1-w)*val-w*val1)*x0+w*n);
relerror=(abs(xnew-x0))/(abs(xnew));
relerrorcol=max(max(relerror));
x0=xnew;
cart=cart+1;
xtan[xtab,xnew];
end
disp(xtab);
x=xnew;
i=count;
end