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

In summary, the user is trying to solve a system of linear equations using the Gaussian Sied algorithm, but are getting an error.
  • #1
fizzkilla
4
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
  • #2
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, [COLOR="Red"]es[/COLOR], lam);
 
  • #3
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);
 

1. Why am I getting errors when trying to run my Gauss-Seidel code in Matlab?

There could be several reasons for this. It could be due to syntax errors, incorrect variable names, or issues with the algorithm itself. It is important to carefully check your code for any mistakes and make sure all variables are properly defined before running it.

2. Can you provide an example of a working Gauss-Seidel code in Matlab?

Yes, there are several resources online that provide sample codes for the Gauss-Seidel algorithm in Matlab. You can also refer to the official Matlab documentation for examples and explanations.

3. How can I optimize my Gauss-Seidel code for better performance?

There are a few techniques you can use to improve the performance of your code. These include vectorization, preallocation of variables, and using built-in Matlab functions instead of loops. You can also try optimizing the convergence criteria for your specific problem.

4. I keep getting incorrect results with my Gauss-Seidel code. What could be the problem?

There are a few potential issues that could be causing this. First, make sure your initial guess for the solution is reasonable. You may also need to adjust your convergence criteria or check for any errors in your code. It is also possible that the Gauss-Seidel method may not be suitable for your specific problem.

5. Can you help me troubleshoot my Gauss-Seidel code in Matlab?

While I cannot directly troubleshoot your code, I can provide some general tips and suggestions. Make sure to carefully check for any mistakes or syntax errors, and consider using the debugging tools in Matlab to pinpoint the source of the problem. You can also seek help from online forums or consult with a colleague or mentor for assistance.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
764
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
9K
Back
Top