Gauss-Seidel Method (Matlab)


by roam
Tags: gaussseidel, matlab, method
roam
roam is offline
#1
Mar15-12, 05:55 PM
P: 884
I need to solve the following problem using Matlab



This is my code so far:

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:

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:

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.
Phys.Org News Partner Science news on Phys.org
Internet co-creator Cerf debunks 'myth' that US runs it
Astronomical forensics uncover planetary disks in Hubble archive
Solar-powered two-seat Sunseeker airplane has progress report
jhae2.718
jhae2.718 is offline
#2
Mar15-12, 07:53 PM
PF Gold
jhae2.718's Avatar
P: 1,153
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:
functionx=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
and a script file that calls the function.
beezey
beezey is offline
#3
Nov13-12, 03:53 PM
P: 5
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.


Register to reply

Related Discussions
Solving a linear system with Gauss-Seidel's method Programming & Computer Science 0
gauss seidel method of convergence General Math 2
Gauss-Seidel Method General Math 0
SOlving power flow using gauss seidel and matlab, HELP Engineering, Comp Sci, & Technology Homework 1
Simple intuitive/graphical interpretation of the Gauss-Seidel method Linear & Abstract Algebra 8