MATLAB: Syntax error in matlab?! Help Please

MATLAB

A part of my code is below. I am plotting a direction field of a differential equation. The second line of code S=-(X.^2 + Y^2)./(X*Y); and it is telling me that "Error using ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead.
Error in Practice640 (line 6)
S=-(X.^2 + Y^2)./(X*Y); "
Could anyone help me in how to type it in so it correctly reads it?
[X,Y]=meshgrid(-5:0.1:5, -4:0.1:4);
S=-(X.^2 + Y^2)./(X*Y);
L=sqrt(1+S.^2);
figure
hold on
quiver(X, Y, 1./L, S./L,0.4,'.')

Best Answer

It looks like you are randomly using the elementwise operators in some places, but not in others. That will get you in trouble. For example, I see that you used X.^2, but then tried to use Y^2.
By the way, your next question might be to ask why you still have problems. That will be due to the fact that you try to use X*Y, instead of X.*Y.
If I could suggest that you need to be more careful in the code that you write.
Related Question