MATLAB: Division by zero in sym using subs

division by 0MATLAB and Simulink Student Suitematrixsubssymbolic

Hey there, I am trying to use the subs() function in a sym matrix. However when I try to run it I run into an error which is :
Error using symengine
Division by zero.
Error in sym/subs>mupadsubs (line 160)
G = mupadmex('symobj::fullsubs',F.s,X2,Y2);
Error in sym/subs (line 145)
G = mupadsubs(F,X,Y);
Error in script (line 64)
Vg3=subs(Vg2,x,-L/2:1000:L/2);
I know I am getting this because my elements are dividing by zero, but I tried to avoid everything by making all the x and y elements where they are 0 to be equal to NaN, however it still gives me an error. Would appreciate some help from the expert. The code is as follows:
clear all
po=1010*10^2;
Dp=6*10^2;
L=1000*10^3;
f=10^(-4);
rho=1.2;
syms x y
P=po+Dp*cos((pi*sqrt(x^2+y^2))/L);
parderx=diff(P,x);
pardery=diff(P,y);
Vg=(1/(f*rho))*parderx;
Ug=(-1/(f*rho))*pardery;
Vg2=subs(Vg,y,0);
Vg3=subs(Vg2,x,-L/2:1000:L/2);
Vg3=double(Vg3);

Best Answer

Hi,
remove the 0 in the vector
-L/2:1000:L/2
for example this way:
po=1010*10^2;
Dp=6*10^2;
L=1000*10^3;
f=10^(-4);
rho=1.2;
syms x y
P=po+Dp*cos((pi*sqrt(x^2+y^2))/L);
parderx=diff(P,x);
pardery=diff(P,y);
Vg=(1/(f*rho))*parderx;
Ug=(-1/(f*rho))*pardery;
Vg2=subs(Vg,y,0);
L_new = -L/2:1000:L/2;
L_new((numel(L_new)-1)/2+1) = [];
Vg3=subs(Vg2,x,L_new);
Vg3=double(Vg3);
Note that this will only work if the value in:
-L/2:1000:L/2
is even. For 999 or 1001 you will get an error. You could also avoid the zero by leaving the code like it is and Change the 1000 to 999 or 1001 which would be an easier way:
po=1010*10^2;
Dp=6*10^2;
L=1000*10^3;
f=10^(-4);
rho=1.2;
syms x y
P=po+Dp*cos((pi*sqrt(x^2+y^2))/L);
parderx=diff(P,x);
pardery=diff(P,y);
Vg=(1/(f*rho))*parderx;
Ug=(-1/(f*rho))*pardery;
Vg2=subs(Vg,y,0);
Vg3=subs(Vg2,x,-L/2:999:L/2);
Vg3=double(Vg3);
If you need a value near zero you can try something like this:
L_new = -L/2:1000:L/2;
L_new(501) = 0.001;
Best regards
Stephan
Related Question