2

I spent over an hour chasing a bug in my code which was leading to a precision error. It turned out that in one of my equations, I had forgotten to divide two vectors element-wise; I had written / instead of ./. Usually Matlab gives an error in these cases, e.g. if you try to multiply two vectors with * instead of .*. But in this case it's happily returning a scalar value! Is this supposed to happen, and does this value have any meaning?

For example,

x = 0 : 0.01 : 1;
y = x/exp(x);

sets y=0.3132.

MGA
  • 1,658
  • 15
  • 28

1 Answers1

2

Yes, this is supposed to happen. You used the matrix right division operator /, and in this particular case it found a scalar value of y that solved the following system of equations in a least-squares sense:

y*exp(x) = x;
gnovice
  • 125,304
  • 15
  • 256
  • 359
  • Thank you. Seems like very error-prone, unfortunate syntax. – MGA May 10 '17 at 04:49
  • @MGA: It might seem that way, but it's very much intended. You just have to get used to the difference between [array and matrix operations](https://www.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html): dot notation for element-wise operations, no dot for linear algebra operations. – gnovice May 10 '17 at 04:55