Inner matrix dimensions must agree.

1 view (last 30 days)
romo76
romo76 on 22 Oct 2015
Commented: dpb on 22 Oct 2015
When I run this code I get Error using * Inner matrix dimensions must agree. Error in gauss_seidel (line 23) X(j)=(B(j)-A(j,1:j-1)*X(1:j-1)-A(j,j+1:N)*P(j+1:N))/A(j,j);
I can't figure out what it is. EDU>>
if true
%
function X=gseid(A,B,P,max1)
A = [0.07 0.18 0.15 0.24;
0.04 0.24 0.10 0.65;
0.54 0.42 0.54 0.10;
0.35 0.16 0.21 0.01];
B = [10.5; 17.5; 28; 14];
P = [0;0;0;0]; % Initial Guess
max1=4; % Max number of iterations
N = length(B);
for k=1:max1
for j=1:N
if j==1
X(1)=(B(1)-A(1,2:N)*P(2:N))/A(1,1);
elseif j==N
X(N)=(B(N)-A(N,1:N-1)*(X(1:N-1))')/A(N,N);
else
%X contains the kth approximations and P the (k-1)st
X(j)=(B(j)-A(j,1:j-1)*X(1:j-1)-A(j,j+1:N)*P(j+1:N))/A(j,j);
end
end
err=abs(norm(X'-P));
relerr=err/norm(X);
P=X'
end
X=X';
end
  1 Comment
dpb
dpb on 22 Oct 2015
Set a breakpoint at that line and inspect
size(B(j))
size(A(j,1:j-1))
Also, NB: pass the arrays A,B,P and max1 instead of defining them inside the function (unless you did that to be self-contained for posting in which case it still won't run there being not enough inputs :) )

Sign in to comment.

Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!