Array indices must be positive integers or logical values..........??
Afficher commentaires plus anciens
What is wrong in following code??
%% Jacobi's method for the solution of system of linear equations
clc
clear all
close all
format short
neq=input('Enter the number of equations i.e. neq:');
a=zeros(neq,neq);
for i=1:neq
for j=1:neq
a(i,j)=input('\nEnter the co-efficent values of the system :');
end
end
for i=1:neq
b(i)=input('\nEnter the constant values b: ');
end
fprintf('Iinitial guess values of the system\n');
x0=0.0; y0=0.0; z0=0.0;
itr=input('\nEnter the total number of iterations :');
for j=1:itr
for i=0:neq
x(i+1)= (b(i)-a(i,i+1)*y0-a(i,i+2)*z0)/a(i,i);
y(i+1)= (b(i+1)-a(i+1,i)*x0-a(i+1,i+2)*z0)/a(i+1,i+1);
z(i+1)= (b(i+2)-a(i+2,i)*x0-a(i+2,i+1)*y0)/a(i+2,i+2);
Ex=abs(x(i+1)-x(i));
Ey=abs(y(i+1)-y(i));
Ez=abs(z(i+1)-z(i));
x0=x(i+1);
y0=y(i+1);
z0=z(i+1);
end
end
Réponses (1)
Walter Roberson
le 7 Mai 2023
for i=1:neq
b(i)=input('\nEnter the constant values b: ')
You store into b(1), b(2) and so on all the way up to b(neq)
for i=0:neq
x(i+1)= (b(i)-a(i,i+1)*y0-a(i,i+2)*z0)/a(i,i);
b(1) to b(neq) are stored into, but you want to retrieve b(i) with i starting from 0 so you want b(0) to b(neq). 0 is not a valid subscript.
You created a as neq by neq but here because of the a(i, i+1) you want to access a(0,1) to a(neq, neq+1)
2 commentaires
wajid
le 7 Mai 2023
Walter Roberson
le 7 Mai 2023
You are accessing up to a(i,i+2) in that code. That is only going to work if i+2 is no more than the number of columns in a . The number of columns in a is neq -- so that code can only work if the upper limit on i is no more than neq-2 .
Catégories
En savoir plus sur Numerical Integration and Differential Equations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!