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
Click For Summary

Discussion Overview

The discussion revolves around a coding issue in Matlab related to matrix operations, specifically the error messages encountered when using the mpower function and matrix multiplication in the context of a numerical computation involving vectors and exponentiation. The scope includes technical explanations and debugging strategies.

Discussion Character

  • Technical explanation
  • Debugging

Main Points Raised

  • One participant reports an error stating "Matrix must be square" when using the mpower function in their code, indicating a potential misunderstanding of matrix dimensions.
  • A later post reveals a different error regarding inner matrix dimensions not agreeing, suggesting that the initial code still has issues after modifications.
  • Another participant proposes that the multiplication of two row vectors (y.^3 and exp(t.^2 + t)) may be the source of the problem, recommending the use of transposition or element-wise multiplication.
  • It is suggested that using element-wise operations (.^ and .*) instead of matrix operations (^ and *) may resolve the issues, as the latter require compatible dimensions for matrix multiplication.

Areas of Agreement / Disagreement

Participants express differing views on the appropriate approach to resolve the errors, with some advocating for element-wise operations while others focus on matrix dimensions. No consensus is reached on a single solution.

Contextual Notes

Participants mention the importance of checking dimensions and using the size function to debug, but specific assumptions about the dimensions of the vectors involved remain unaddressed.

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)
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K