Help with Matlab Code
Hey!
I need some help with some code that i am trying to write.
It's basically trying to solve a system of equations Ax=b for x.
I have this much in a function file:
function [x] = jacobi_iterative(A,b,Nsteps)
Neq=length(A);
x=zeros(1,Neq);
for istep =1:1:Nsteps
for ix = 1:Neq
summat = 0;
for j = 1:Neq
if (j ~=ix)
summat = summat + A(ix+j)*x(j);
end
end
x(ix) = (b(ix) - summat)/A(ix,ix);
end
end
And I have this in another file:
Amatrix=input('Enter the matrix of coefficients of unknowns: ');
Bvector=input(' Enter the right side vector: ');
Niterations=50;
x_vector=jacobi_iterative(Amatrix,Bvector,Niterations);
disp('The answer is: ')
x_vector %#ok<nopts>
The output for the program is a vector (lilke I want), but it is the wrong answer.
I am inputting Amatrix to be: [4 1 1; 2 5 1; 0 2 4] and the right side vector to be [5 -1 6]
The answer should be [1 -1 2]
Any suggestions you can make would be much appreciated!</nopts>
I need some help with some code that i am trying to write.
It's basically trying to solve a system of equations Ax=b for x.
I have this much in a function file:
function [x] = jacobi_iterative(A,b,Nsteps)
Neq=length(A);
x=zeros(1,Neq);
for istep =1:1:Nsteps
for ix = 1:Neq
summat = 0;
for j = 1:Neq
if (j ~=ix)
summat = summat + A(ix+j)*x(j);
end
end
x(ix) = (b(ix) - summat)/A(ix,ix);
end
end
And I have this in another file:
Amatrix=input('Enter the matrix of coefficients of unknowns: ');
Bvector=input(' Enter the right side vector: ');
Niterations=50;
x_vector=jacobi_iterative(Amatrix,Bvector,Niterations);
disp('The answer is: ')
x_vector %#ok<nopts>
The output for the program is a vector (lilke I want), but it is the wrong answer.
I am inputting Amatrix to be: [4 1 1; 2 5 1; 0 2 4] and the right side vector to be [5 -1 6]
The answer should be [1 -1 2]
Any suggestions you can make would be much appreciated!</nopts>
0
Comments
thanks.
in my loop (line 9 of code in my function file), i had part of an equation as A(ix+j) when it should have actually of been A(ix,j)