Problem using subs function

8 次查看(过去 30 天)
KOZI
KOZI 2018-10-23
编辑: YT 2018-10-23
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.

回答(1 个)

YT
YT 2018-10-23
编辑:YT 2018-10-23
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)

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by