Problem using subs function
Show older comments
I am trying to plot Electric vectors of magnetic field.I have f function and then i am trying as the example to use gradient and quiver.My code is:
syms x y;
M=(4*abs(x))/((abs(x) + 1)^2 + y^2);
L=ellipticK(M);
f=(2*L/(pi*((abs(x) + 1)^2 + y^2)^(1/2)));
g = gradient(f, [x, y]);
[X, Y] = meshgrid(-1:.1:1,-1:.1:1);
G1 = subs(g(1), [x y], {X,Y});
G2 = subs(g(2), [x y], {X,Y});
quiver(X, Y, G1, G2)
And i get this error:
Error using symengine
Division by zero.
Error in sym/subs>mupadsubs (line 150)
G = mupadmex('symobj::fullsubs',F.s,X2,Y2);
Error in sym/subs (line 135)
G = mupadsubs(F,X,Y);
Error in E (line 7)
G1 = subs(g(1), [x y], {X,Y});
Any help will be very useful. I am realy noob in matlab. I only need a plot for a homework. thanks in advance.
Answers (1)
Even without being an expert in Matlab, the error should be pretty clear.
Division by zero.
This makes sense because X and Y both contain zeros, look at the meshgrid input
[X, Y] = meshgrid(-1:.1:1,-1:.1:1);
% -1:.1:1 => -1 -.9 -.8 -.7 -.6 -.5 -.4 -.3 -.2 -.1 0 .1 .2 .3 .4 .5 .6 .7 .8 .9 1
So I would remove the zeros from X and Y for it to work
syms x y;
M=(4*abs(x))/((abs(x) + 1)^2 + y^2);
L=ellipticK(M);
f=(2*L/(pi*((abs(x) + 1)^2 + y^2)^(1/2)));
g = gradient(f, [x, y]);
[X, Y] = meshgrid(-1:.1:1,-1:.1:1);
X(X==0)=[]; %removes zeros from X
Y(Y==0)=[]; %removes zeros from Y
G1 = subs(g(1), [x y], {X,Y});
G2 = subs(g(2), [x y], {X,Y});
quiver(X, Y, G1, G2)

Categories
Find more on Vector Fields in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!