# Help with Matlab

1. Aug 6, 2008

### Benjamin#1

Help with Matlab!!!

Hi,
I've just started learning how to use the Matlab and came across this question in the textbook:
Consider the following system of equations:
[-1 2 4 1 0]
[ 5 4 0 0 0]
[ 0 6 1 0 4] x =
[ 0 -1 0 -1 4]
[ 1 0 -2 -5 1]

[ 1 ]
[ 1 ]
[ 1 ]
[ 1 ]
[ 1 ]

Write a Matlab script file, using the Matlab function gauss seidel that:
(a) Sets up the matrix and the vector in the system of equations
(b) Applies 5 iterations of the gauss seidel method using x(0)=[0,0,0,0,0]T

Now I know how to part (b) but can't seem to get my head around part (a). I have attached a copy of the gauss seidel function I wrote. I know for part (b) I have to put in the values of the inputs: A,b,y,N and then simply execute the function. I really don't know how to solve part (a). Help Please!!!

Edit to say: I honestly don't really understand part (a) of the question. If they are simply asking me to write up the matrix and the vector to calculate the x vector then I know how to do that but I don't know if the question is asking me to actually set it up as x1+x2+x3+...=some no.
If so, do I simply create an M-file like the one below???
A=[-1 2 4 1 0;5 4 0 0 0;0 6 1 0 4;0 -1 0 -1 4;1 0 -2 -5 1]
b=[1;1;1;1;1]
x = A\b

Gauss Seidel function:
function x = gs (A,b,y,N)

n = length(y);

x = y;

for k=1:N
for i=1:n
sum = b(i);
for j=1:i-1
sum = sum - A(i,j)*x(j);
end
for j = i+1:n
sum = sum - A(i,j)*y(j);
end
x(i) = sum/A(i,i);
end
y = x';
end

Last edited: Aug 6, 2008
2. Aug 12, 2008

### MATLABdude

Re: Help with Matlab!!!

What an opportune moment to have joined!

From my reading of part a, your interpretation is correct. Make a matrix of coefficients (what you have as A) and a vector of equation solutions (what you have as b). This allows you to use it with part b. Sometimes questions really are as easy as you think they are. ;-)