problem using mesh. "Matrix is singular to working precision."

16 views (last 30 days)
I'm trying to plot a graident of a function using mesh and meshgrid but im getting the "Warning: Matrix is singular to working precision." when im trying to run the code. the graph is empty. is the problem that im divding with zero in my interval? how can I work around this?
if true
[X,Y] = meshgrid(-3:1:3);
f1=(3*X.^2*Y + 10*X*Y.^2)/(exp(X.^2) + 3*Y.^4) - (2*X*exp(X.^2)*(X.^3*Y + 5*X.^2*Y.^2))/(exp(X.^2) + 3*Y.^4)^2;
mesh(X,Y,f1);
end

Accepted Answer

Birdman
Birdman on 16 Mar 2018
Actually, the problem is that you do matrix operation but instead, you need to do elementwise operation, which means using ./ and .* instead of / and *. Try this:
[X,Y] = meshgrid([-3:0.01:3]);
f1=@(X,Y) (3*X.^2.*Y + 10.*X.*Y.^2)./(exp(X.^2) + 3.*Y.^4) - (2.*X.*exp(X.^2).*(X.^3.*Y + 5.*X.^2*Y.^2))./(exp(X.^2) + 3*Y.^4).^2;
mesh(X,Y,f1(X,Y));

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!