Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Matrix dimensions must agree
Replies: 7   Last Post: Oct 20, 2011 2:30 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
HB

Posts: 42
Registered: 2/26/11
Re: Matrix dimensions must agree
Posted: Oct 20, 2011 11:41 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On 20 Okt., 17:11, "Halley " <halleykhol...@gmail.com> wrote:
> I am trying to write a program that utilizes the inverse iterative method.  I calculate an initial x value and rho value given the matrices A, B, and b.  I calculate a value of x normalized to the matrix B (xnorm) as well as a new value for rho for each iteration.  Given that the relative error of rho is less than my preset tolerance (TOL) the loop ends, otherwise xnorm becomes the new value for x and the rho(i+1) becomes rho(i).  The problem is in calculating rho(i), I am told my matrix dimensions do not agree when I have checked the sizes multiple times.  Any suggestions?  Thanks.
>
> %Declare initial variables
> A = [2 -1;-1 4];
> b = [1;1];
> B = [1 0;0 1];
> Nmax = 20;
> TOL = 1*10^-5;
> k = 1;
> x = zeros(2,21);
> x_a = zeros(21,2);
> rho = zeros(21,1);
> xnorm = zeros(2,21);
> x(:,1) = A\b;
> x_a(1,:) = x(:,1)'
>
> for i =1:Nmax
>
> %Calculate old values  
>   rho(i) = (x_a(i,:).*A.*x(:,i))./(x_a(i,:).*B.*x(:,i))
>
> %Calculate new values
>   xnorm(:,i) = (1/sqrt(x_a(i,:).*B.*x(:,i))).*x(:,i)
>    rho(i+1) = (x_a(i,:).*A.*x(:,i))./(x_a(i,:).*B.*x(:,i))
>
> %Check for convergence
> relative_error = abs((rho(i+1)-rho(i))/rho(i+1))
>
> if ( relative_error ~= 0 && relative_error <= TOL )
> x(i+1) = xnorm(i)
> rho(i+1) = rho(i+1)
> k = k
> return
> elseif (relative_error ~= 0 && relative > TOL)
> x(i+1) = xnorm(i)
> rho(i) = rho(i+1)
> k = k+1
>
> end    
> end


Hi,
if you run your code line by line, or set a breakpoint somewhere and
go into debug-mode, you will see that you have this error already in
the first line of the loop.
The problem is that you're trying to multiply (element-wise) the 2x2
matrices A and B with x_a(i,:), a 1x2 vector and also with x(:,i), a
2x1 vector.

For element-wise multiplication, all factors have to have the same
size.

Depending on how your needs, you may have to "enlarge" your vectors.
For example with repmat, see help repmat.








Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.