How to input this Gauss-seidel problem into the following code( code might need work)

1 次查看(过去 30 天)
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 %%%%%%%%%%%%%%

回答(1 个)

Pravin Jagtap
Pravin Jagtap 2020-2-26
编辑:Pravin Jagtap 2020-2-26
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.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by