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

  • Context: MATLAB 
  • Thread starter Thread starter roam
  • Start date Start date
  • Tags Tags
    Matlab Method
Click For Summary
SUMMARY

The correct implementation of the Gauss-Seidel method in MATLAB requires defining the function in a separate file named GaussSeidel.m. The provided code incorrectly attempts to define a function within a script, leading to the error "Function definitions are not permitted in this context." Additionally, the implementation appears to resemble the Jacobi method rather than the Gauss-Seidel method. To determine the number of iterations (N) necessary for convergence, one must analyze the specific problem and the properties of the matrix A.

PREREQUISITES
  • Understanding of MATLAB programming and file structure
  • Familiarity with the Gauss-Seidel method for solving linear equations
  • Knowledge of matrix operations in MATLAB
  • Basic concepts of numerical methods and convergence criteria
NEXT STEPS
  • Create a separate file for the Gauss-Seidel function in MATLAB
  • Research the differences between the Gauss-Seidel and Jacobi methods
  • Learn about convergence criteria for iterative methods in numerical analysis
  • Explore MATLAB's built-in functions for solving linear systems, such as linsolve or mldivide
USEFUL FOR

Mathematics students, engineers, and developers working with numerical methods in MATLAB, particularly those implementing iterative algorithms for solving linear equations.

roam
Messages
1,265
Reaction score
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
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:
[b]function[/b][/color] [/color]x=GaussSeidel[/color](A,b,y,N)
[/color]n = length[/color](y);
[b]for[/b][/color] k = 1:N
    [b]for[/b][/color] i[/color]=1:n
        s=b(i[/color]);
        [b]for[/b][/color] j[/color] =1:i[/color]-[/color]1
            s=s-[/color]A(i[/color],j[/color])*[/color]y(j[/color]);
        [b]end[/b][/color]
        [b]for[/b][/color] j[/color] = i[/color]+[/color]1:n
            s=s-[/color]A(i[/color],j[/color])*[/color]y(j[/color]);
        [b]end[/b][/color]
        x(i[/color])=s/[/color]A(i[/color],i[/color]);
        x(i[/color])=x(k);
    [b]end[/b][/color]
    y = x'[/color]
[b]end[/b][/color]

and a script file that calls the function.
 
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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
5
Views
3K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K