Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Error code in Matlab

  1. Oct 4, 2014 #1
    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!
  2. jcsd
  3. Oct 4, 2014 #2
    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.
  4. Oct 4, 2014 #3


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    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.
  5. Oct 7, 2014 #4


    User Avatar
    Gold Member

    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)
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Threads - Error code Matlab Date
Mathematica Errors when plotting but not when evaluating Tuesday at 12:12 PM
Excel Error Bars Nov 26, 2017
Using Maxima to plot error in Fourier series Nov 5, 2017
Matlab 2D plotting error help please Aug 28, 2016
(TexnicCenter) How to locate errors in code Nov 22, 2011