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
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
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!
 
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)