Error Matrix dimensions must agree

1 view (last 30 days)
advaita vedanta
advaita vedanta on 11 Sep 2014
Commented: Pierre Benoit on 11 Sep 2014
Dear friends, Please help me with this. I got error message at:
soln=A\(rhs); %pressure head at each nodal points
in the for loop, A is 500by500 matrix and rhs is 500by1 vector. Even after transposing rhs I had got the error as: Error Matrix dimensions must agree
clear all clc
alpha=(17*pi)/180; %slope angle
dz=200; %depth of water table from the suface
dlz=400; % toltal depth of slope
fi=(25*pi)/180; %friction angle
C=0.4; %4 kpa=0.4 N/cm^2
gsat=0.021; %21 kN/m^3
gw=0.0098; %9.81 Kn/m^3
Dzero=4; %0.0004m^2/s
ksat=1e-3; %e-5 m/s
iz=2e-4; %iz=7.2 mm/h= 2e-4 cm/s
deltat=10; %time step
deltaz=0.8; %space step
T=12*60*60; %time duration of rainfall= 12h to seconds
e=(deltat/deltaz^2)*Dzero*cos(alpha)*cos(alpha);
n=500; %time step, based on T
Z=0.8:500; % depth in cm
%Setup sparse matrix
si=(Z-dz)*cos(alpha)*cos(alpha); %Initial condition ...i.e.
%pressure head @t=0 all along the depth
si=si';
b= sparse(1:n,1:n,116.314,n,n); % element b... 1...n
c= sparse(1:n-1,2:n,-e,n,n); % element c...
a= sparse(2:n,1:n-1,-e,n,n); % element a... 2...n-1
c(1)=-114.314; a(n)=-114.314; %Boundary condition
A=a+b+c;
A(1,2)=-114.314; A(500,499)=-114.314;
% full (A)
w=ones*(1:n);
rhs=ones*(n-2:1);
for t=deltat:deltat:3600;
for j=2:499
rhs(j)=e*si(j-1)-112.314*si(j)+e*si(j+1); % Rhs vector
end
if t>T
rhs(1)=-112.314*si(1)+114.314*si(2)-130.686;
else
rhs(1)=-112.314*si(1)+114.314*si(2)-167.726; % t<=T
end
rhs(500)=114.314*si(499)-112.314*si(500)+167.726; % Boundary condition...
%at the bottom of the slope at all values of t
% rhs=rhs'; %difference scheme and subtituting the eqn in crank-nicolson equ
% we get rhs(n)
soln=A\(rhs); %pressure head at each nodal points
% disp([t soln'])
w(1:n)=soln; %first column is time and subsequent columns ...
% are solution at an interval deltaz=0.8
end
  1 Comment
Pierre Benoit
Pierre Benoit on 11 Sep 2014
There is something that is puzzling me about your code, in your for-loop with t, you always re-write soln and w without saving the value, is that intended ?
Also, in regard of my first point, transposing rhs in the loop will not work because in the second iteration of the loop, you will transpose it again to it's original state so the problem is just delayed from one iteration. You better try
soln = A\rhs'
Or you can also define rhs before the loop as a column vector.

Sign in to comment.

Answers (2)

Adam
Adam on 11 Sep 2014
I'm not at all familiar with using the \ operator, but to my surprise it gives no error in my R2014a with a random 500*500 matrix divided by a 500*1 array.
That aside, the suggestion I was going to make is to use bsxfun:
soln = bsxfun( @ldivide, A, rhs );

advaita vedanta
advaita vedanta on 11 Sep 2014
\ operator performs gauss elimination of linear equations. Au=d, instead of inverting the matrix it performs elimination. here soln=u, A=A, rhs=d.
Thank you brother I will also try bsxfun and let you know.
Thank you for the help

Categories

Find more on Operating on Diagonal Matrices 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!