Division by zero in sym using subs

49 次查看(过去 30 天)
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);

采纳的回答

Stephan
Stephan 2018-10-3
编辑:Stephan 2018-10-3
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
  2 个评论
Ullas Rajvanshi
Ullas Rajvanshi 2018-10-3
Stephan, Thank you so much for the response. Yes this fixed my problem. What a stupid mistake haha. :)
Cheers
Bill Tubbs
Bill Tubbs 2020-5-23
I have the same problem but is removing the values where numerical errors are expected the only solution? Would be nice if there were a way to supress the error and return NaNs whenever a computation error occurred rather than having to anticipate it?
>> syms s
>> F1 = 1/(s-1)
F1 =
1/(s - 1)
>> s_values = linspace(0,10,101);
>> subs(F1,s,s_values)
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);
>> s_values(11) = nan;
>> subs(F1,s,s_values)
ans =
[ -1, -10/9, -5/4, -10/7, -5/3, -2, -5/2, -10/3, -5, -10, NaN, 10, 5, 10/3, 5/2, 2, 5/3, 10/7, 5/4, 10/9, 1, 10/11, 5/6, 10/13, 5/7, 2/3, 5/8, 10/17, 5/9, 10/19, 1/2, 10/21, 5/11, 10/23, 5/12, 2/5, 5/13, 10/27, 5/14, 10/29, 1/3, 10/31, 5/16, 10/33, 5/17, 2/7, 5/18, 10/37, 5/19, 10/39, 1/4, 10/41, 5/21, 10/43, 5/22, 2/9, 5/23, 10/47, 5/24, 10/49, 1/5, 10/51, 5/26, 10/53, 5/27, 2/11, 5/28, 10/57, 5/29, 10/59, 1/6, 10/61, 5/31, 10/63, 5/32, 2/13, 5/33, 10/67, 5/34, 10/69, 1/7, 10/71, 5/36, 10/73, 5/37, 2/15, 5/38, 10/77, 5/39, 10/79, 1/8, 10/81, 5/41, 10/83, 5/42, 2/17, 5/43, 10/87, 5/44, 10/89, 1/9]

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Number Theory 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by