![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/192608/image.jpeg)
Problem using subs function
조회 수: 1 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (1개)
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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/192608/image.jpeg)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!