How to input this Gauss-seidel problem into the following code( code might need work)
1 view (last 30 days)
Show older comments
4. Use Gauss-Seidel iteration to solve the following band system.
12x1 − 2x2 + x3 = 5
− 2x1 + 12x2 − 2x3 + x4 = 5
x1 − 2x2 + 12x3 − 2x4 + x5 = 5
x2 − 2x3 + 12x4 − 2x5 + x6 = 5
...
...
...
...
...
...
x46 − 2x47 + 12x48 − 2x49 + x50 = 5
x47 − 2x48 + 12x49 − 2x50 = 5
x48 − 2x49 + 12x50 = 5
clc; % helps in clearing screen
clear; % helps in clearing history
clear all; % helps in closing files if any
%Function for page 167 question 4.
function X=gseid(A,B,P,delta, max1)
%Input - A is an N x N nonsingular matrix
%- B is an N x 1 matrix
%- P is an N x 1 matrix; the initial guess
%- delta is the tolerance for P
%- max1 is the maximum number of iterations
%Output - X is an N x 1 matrix: the Gauss-Seidel
%approximation to the solution of AX = B
N = length(B);
for k=1:max1
for j=1:N
if j==1
X(1)=(B(1)-A(1,2:N)*P(2:N))/A(1,1);
elseif j==N
X(N)=(B(N)-A(N,1:N-1)*(X(1:N-1))')/A(N,N);
else
%X contains the kth approximations and P the (k-1)st
X(j)=(B(j)-A(j,1:j-1)*X(1:j-1)'-A(j,j+1:N)*P(j+1:N))/A(j,j);
end
end
err=abs(norm(X'-P));
relerr=err/(norm(X)+eps);
P=X';
if(err<delta)|(relerr<delta)
break
end
end
X=X';
%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%
0 Comments
Answers (1)
Pravin Jagtap
on 26 Feb 2020
Edited: Pravin Jagtap
on 26 Feb 2020
Hello Steven,
I am assuming that you are using the code written by someone else for your homework question. For your problem, construct the input arguments(A, B, P, delta, max1) from your system of equations. Representation of input arguments is given in the code. Refer the following link for understanding how to use functions and how to pass the arguments in MATLAB.
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!