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);
댓글 수: 0
채택된 답변
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
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 Center 및 File Exchange에서 Number Theory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!