Why Does Matlab Say Matrix Must Be Square When Using mpower?

  • Context: MATLAB 
  • Thread starter Thread starter MathewsMD
  • Start date Start date
  • Tags Tags
    Code Error Matlab
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 2K views
MathewsMD
Messages
430
Reaction score
7
When running the following code on Matlab:

>> y(i) = y(i-1) + (0.5 * y.^3 * exp(t^2 + t))*h

I get the error:

? Error using ==> mpower
Matrix must be square.

It seems fairly simple but I'm not quite sure what I'm overlooking. Any help with regards to what I may be doing wrong would be greatly appreciated!
 
Physics news on Phys.org
I've gotten to:

>> y(i) = y(i-1) + (0.5 * y.^3 * exp(t.^2 + t))*h
? Error using ==> mtimes
Inner matrix dimensions must agree.

But the code still seems to be invalid.
 
Isn't y.^3 * exp(t.^2 + t) trying to multiply two row vectors y.^3 times exp(t.^2 + t)? You need to transpose one of them or do element wise multiplication.
P.S. To debug equations, try small part calculations and see which ones abort. Also, the size function helps you see what the dimensions are.
 
Use .^ and .* instead of ^ and *. The former perform elementwise operations, which is what you want here I believe.

* works fine if at least one operand is a scalar. But if you have two vectors, or a mixture of vectors and matrices, then they need to have compatible dimensions to use * (common inner dimension, i.e. NxM * MxP)