What is the correct implementation of the Gauss-Seidel method in Matlab?

I'm curious now.In summary, the conversation discusses using Matlab to solve a problem with the Gauss-Seidel method. The code provided contains an error due to defining a function in a script file, which can be fixed by creating a separate function file. The inputs and the necessary number of iterates for the problem are also mentioned. There is also a correction that the code provided may actually be using the Jacobi method.
  • #1
roam
1,271
12
I need to solve the following problem using Matlab

http://img641.imageshack.us/img641/6448/matlabe.jpg

This is my code so far:

Code:
clear all
clc
clf
function x=GaussSeidel(A,b,y,N)
n = length(y);
for k = 1:N
    for i=1:n
        s=b(i);
        for j =1:i-1
            s=s-A(i,j)*y(j);
        end
        for j = i+1:n
            s=s-A(i,j)*y(j);
        end
        x(i)=s/A(i,i);
        x(i)=x(k);
    end
    y = x'
end

But I keep getting the following error:

Code:
Error: File: Untitled.m Line: 4 Column: 1
Function definitions are not permitted in this context.

Why am I getting this error? What do I need to do?

And my inputs would be:

Code:
A=[-5 0 2 0 -1 ;
    0 9 0 3 0 ; 
    2 0 5 0 2 ;
    0 -2 0 4 0 ;
    -1 0 7 0 7]
b = [8;4;-8;-4;0]
x0=[8;4;-8;-4;0]

For N, how do I know many iterates N are necessary for this problem?

Any help with the code is greatly appreciated.
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
You're defining a function in a script file. In MATLAB, functions are defined in a separate file with the same name as the function.

Create a file GaussSeidel.m with the following:
Code:
[color=#008000][b]function[/b][/color][color=#bbbbbb] [/color]x=[color=#0000FF]GaussSeidel[/color](A,b,y,N)[color=#bbbbbb]
[/color]n = [color=#008000]length[/color](y);
[color=#008000][b]for[/b][/color] k = 1:N
    [color=#008000][b]for[/b][/color] [color=#008000]i[/color]=1:n
        s=b([color=#008000]i[/color]);
        [color=#008000][b]for[/b][/color] [color=#008000]j[/color] =1:[color=#008000]i[/color][color=#666666]-[/color]1
            s=s[color=#666666]-[/color]A([color=#008000]i[/color],[color=#008000]j[/color])[color=#666666]*[/color]y([color=#008000]j[/color]);
        [color=#008000][b]end[/b][/color]
        [color=#008000][b]for[/b][/color] [color=#008000]j[/color] = [color=#008000]i[/color][color=#666666]+[/color]1:n
            s=s[color=#666666]-[/color]A([color=#008000]i[/color],[color=#008000]j[/color])[color=#666666]*[/color]y([color=#008000]j[/color]);
        [color=#008000][b]end[/b][/color]
        x([color=#008000]i[/color])=s[color=#666666]/[/color]A([color=#008000]i[/color],[color=#008000]i[/color]);
        x([color=#008000]i[/color])=x(k);
    [color=#008000][b]end[/b][/color]
    y = x[color=#666666]'[/color]
[color=#008000][b]end[/b][/color]

and a script file that calls the function.
 
  • #3
I'm not familiar with matlab, but I believe this is an incorrect implementation of the Gauss-Seidel method. This looks like the Jacobi method to me.

I'm going to fool around with this and get back.
 

FAQ: What is the correct implementation of the Gauss-Seidel method in Matlab?

What is the Gauss-Seidel Method?

The Gauss-Seidel Method is an iterative numerical method used for solving a system of linear equations. It is a modification of the Jacobi method and is often used for larger systems of equations as it converges faster.

How does the Gauss-Seidel Method work?

The Gauss-Seidel Method works by using a set of initial guesses and iteratively improving these guesses until a desired level of accuracy is achieved. In each iteration, the method uses the most up-to-date values to calculate the next set of guesses, which leads to faster convergence compared to the Jacobi method.

What are the advantages of using the Gauss-Seidel Method?

The advantages of using the Gauss-Seidel Method include faster convergence compared to the Jacobi method, and it is relatively easy to implement in computer programs. It also has the ability to handle larger systems of equations, making it a popular choice for solving many real-world problems.

What are the limitations of the Gauss-Seidel Method?

The main limitation of the Gauss-Seidel Method is that it may not converge if the system of equations is ill-conditioned or if the initial guesses are not chosen carefully. In these cases, the method may oscillate or diverge instead of converging to a solution.

How can the Gauss-Seidel Method be implemented in Matlab?

In Matlab, the Gauss-Seidel Method can be implemented using a loop that updates the guesses for each variable in each iteration. The convergence criteria can be specified and the process can be stopped when the desired accuracy is achieved. A built-in function, "gsolve", also exists in Matlab for solving linear systems using the Gauss-Seidel Method.

Similar threads

Replies
2
Views
3K
Replies
8
Views
845
Replies
2
Views
3K
Replies
5
Views
2K
Replies
3
Views
4K
Replies
10
Views
2K
Replies
6
Views
2K
Back
Top