MATLAB Help with Matlab: Solving System of Equations

AI Thread Summary
The discussion revolves around a user's request for help with a MATLAB exercise involving the Gauss-Seidel method. The user is tasked with two parts: first, setting up a matrix and vector from a given system of equations, and second, applying the Gauss-Seidel method for five iterations. The user expresses confidence in part (b) but struggles with part (a), unsure if they need to represent the equations in a specific format. Another participant confirms that the user’s understanding is correct, stating that they need to create a coefficient matrix (A) and a solution vector (b) to proceed with part (b). This clarification reassures the user that the task is straightforward.
Benjamin#1
Messages
1
Reaction score
0
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!:cry:

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:
Physics news on Phys.org


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. ;-)
 

Similar threads

Replies
4
Views
1K
Replies
2
Views
3K
Replies
5
Views
2K
Replies
8
Views
2K
Replies
1
Views
2K
Back
Top