Need help with Matlab code for gauss siedel i get errors, need imediat help

Click For Summary
SUMMARY

The forum discussion addresses an error encountered in MATLAB while implementing the Gauss-Seidel method for solving linear equations. The primary issue arises from supplying five input arguments to the function gauss_sied, which is defined to accept only four parameters: xo, A, b, and lam. The user also faces a subsequent error related to variable assignment within the function. Correcting the function call to match the defined parameters resolves the issue.

PREREQUISITES
  • Familiarity with MATLAB programming language
  • Understanding of the Gauss-Seidel method for solving linear equations
  • Knowledge of function definitions and variable scope in MATLAB
  • Basic concepts of iterative methods in numerical analysis
NEXT STEPS
  • Review MATLAB function definitions and argument passing
  • Study the implementation of the Gauss-Seidel method in numerical analysis
  • Learn about error handling and debugging techniques in MATLAB
  • Explore optimization techniques for iterative methods
USEFUL FOR

Students and professionals in engineering, mathematics, or computer science who are working with numerical methods in MATLAB, particularly those implementing iterative algorithms for linear systems.

fizzkilla
Messages
4
Reaction score
0
Please I need HELP IMEDIATLY

This is the error message that i get

? Error using ==> gauss_sied
Too many input arguments.

Error in ==> HW_14 at 23
[x_wo, cnt, x_w,cntt] = gauss_sied(xo, A, b, es, lam);
clc
clearC = [-8 1 -2 -20
2 -6 -1 -38
-3 -1 7 -34];

A = C(:,1:end-1);
b = C(:,end);nr = length(b);

xo = ones(1,nr); % initial guess

lam = 1.2;
n = 4;
es = .5*10^(2-n);

[x_wo, cnt, x_w,cntt] = gauss_sied(xo, A, b, es, lam);

fprintf(' Solution to [A]{x} = {b}\n')
fprintf('*******************************************\n')
fprintf(' Without Relaxation With Relaxation\n')
fprintf(' ******************** ***************\n')
for i = 1:length(b)
fprintf(' x(i) = %.f x(i) = %.f\n',i,x_wo,i,x_w)
end
fprintf(' ******************** ***************\n')
fprintf(' %i iterations %i iterations\n',cnt,cntt)
fprintf('*******************************************\n')Function program
function [x_wo, cnt,x_w cntt] = gauss_sied(xo, A, b, lam)
%Without Relaxation
nr = length(b);
ea = 1;
cnt = 0;while ea > es
for i = 1 : nr
sm = 0;
for j = 1:nr
if i ~= j
sm = sm + A(i,j) * xo(j);
end
end
x_wo(i) = (b(i) - sm) / A(i,i);
end

ea = 100 * abs((x_wo-xo)./x_wo);

xo = x_wo;

cnt = cnt+1;
end

%With Relaxation

nr = length(b);
ea = 1;
cntt = 0;

while ea > es
for i = 1 : nr
sm = 0;
for j = 1:nr
if i ~= j
sm = sm + A(i,j) * xo(j);
end
end
x_w(i) = (b(i) - sm) / A(i,i);
end

ea = 100 * abs((x_w-xo)./x_w);
x_w = (lam*x_w) + (1 - lam)*xo;

xo = x_w;

cntt = cntt+1;
end
 
Last edited:
Physics news on Phys.org
You're supplying 5 inputs to a function that is defined to have 4 inputs.

Code:
function [x_wo, cnt,x_w cntt] = gauss_sied(xo, A, b, lam)

called as:
Code:
[x_wo, cnt, x_w,cntt] = gauss_sied(xo, A, b, es, lam);
 
Now in my code I get another error

Error in ==> gauss_sied at 8
xo = x_wo;

Error in ==> HW_14 at 23
[x_wo, cnt, x_w,cntt] = gauss_sied(xo, es, lam, A, b);
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 41 ·
2
Replies
41
Views
10K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
7
Views
9K
Replies
2
Views
2K