1
clear all

syms s w 

G = 1/((s)*(s+1)*(s+2)); %transfer function
G_w = subs(G,s,j*w); 
W= [-100:0.01:100]; %[min_range:step size:max_range]
nyq = eval(subs(G_w,w,W)); 

x = real(nyq)
y = imag(nyq)

plot(x,y)

我似乎无法运行此代码,并且它一直在第 100++ 行显示错误,而我只有不到 20 行。

Error using symengine (line 59)
Division by zero.
Error in sym/subs>mupadsubs (line 139)
G = mupadmex('symobj::fullsubs',F.s,X2,Y2);
Error in sym/subs (line 124)
    G = mupadsubs(F,X,Y);
Error in nyquist2 (line 8)
nyq = eval(subs(G_w,w,W)); %replace W with w in equation G_w

这是显示的错误,任何专家都可以帮助我吗?

4

2 回答 2

5

错误是因为您正在G_w使用数组进行计算,W并且该数组包含0导致除以零的值,因此是错误。

使用 symengine 时出错(第 59 行)
除以零。

您可以做些什么来解决这个问题,将0inW替换为eps.

% Replace zeros with epsilon
W(W == 0) = eps;

nyq = eval(subs(G_w,w,W)); 

x = real(nyq)
y = imag(nyq)

plot(x,y)

作为旁注,该错误不是抱怨代码第 100 行以上的问题而是堆栈跟踪表明该错误实际上源自您正在调用函数

堆栈跟踪是从发生错误的位置到您调用创建它的代码排序的

Error using symengine (line 59)             <--- WHERE THE ERROR HAPPENED
Division by zero.                           <--- THIS IS THE ERROR MESSAGE

Error in sym/subs>mupadsubs (line 139)      <--- THIS FUNCTION CALLED symengine
G = mupadmex('symobj::fullsubs',F.s,X2,Y2); <--- THIS IS THE LINE THAT CALLED symengine

Error in sym/subs (line 124)                <--- THIS FUNCTION CALLED mupadsubs
G = mupadsubs(F,X,Y);                       <--- THIS IS THE LINE THAT CALLED mupadsubs

Error in nyquist2 (line 8)                  <--- THIS FUNCTION (YOURS) CALLED subs
nyq = eval(subs(G_w,w,W))                   <--- THIS IS THE LINE THAT CALLED subs
于 2017-01-15T18:02:00.510 回答
0

在必须计算大量值的情况下,@Suever 的答案绝对是一个更好的解决方案,但是如果您希望仅以一个值 (0) 评估函数并希望避免除零错误,则另一种解决方案, 你可以这样做:

>> limit(G_w,w,0)
 
ans =
 
NaN

不过,这在计算上是相当密集的,因此仅当您期望被零除(例如 at w = 0)时才值得使用。

于 2021-04-27T03:24:11.993 回答