Matlab 无法从sym转换为逻辑
我试图计算光线入射到复曲面上的路径长度,但得到以下错误消息 “无法从sym转换为逻辑。” “校准路径错误(第188行) 如果常数>常数上一个“ 有人能提出一种方法来帮助解决这个错误吗? 实际代码如下Matlab 无法从sym转换为逻辑,matlab,Matlab,我试图计算光线入射到复曲面上的路径长度,但得到以下错误消息 “无法从sym转换为逻辑。” “校准路径错误(第188行) 如果常数>常数上一个“ 有人能提出一种方法来帮助解决这个错误吗? 实际代码如下 %Conic Toriodal surface %This calculates the path length of a ray from a point to the surface P=[0 0 40]; X=P(1);
%Conic Toriodal surface
%This calculates the path length of a ray from a point to the surface
P=[0 0 40];
X=P(1); % define the start point
Y=P(2);
Z=P(3);
Dir=Dir/norm(Dir);
S_o=-P(3)/Dir(3);
S_f=0;
S_Prev=-100;
S=0;
k=1;
X=X+S_o*Dir(1); % define the point intersect with the z=0 plane
Y=Y+S_o*Dir(2);
Z=Z+S_o*Dir(3); % here Z=0
X_1=X;
Y_1=Y;
Z_1=Z;
Cx=1/25; % Curvature of surfaces, 1-by-(j-2) vector, c=1/R
Cy=1/10;
syms x y z
f=z-(Cx*x^2+Cy*y^2+(k*Cy-Cx)*(Cy*y^2./(1+sqrt(1-k*Cy^2*y^2)))^2)/(1+sqrt(1- Cx*(Cx*x^2+Cy*y^2+(k*Cy-Cx)*(Cy*y^2./(1+sqrt(1-k*Cy^2*y^2)))^2)));
%the function of the surface F(x,y,z)=0%
diff(f,x); % calculate the differential of x
diff(f,y); % calculate the differential of y
diff(f,z); % calculate the differential of z
N=0;
while abs(S-S_Prev)>=0.0000000001
N=N+1;
Const_Prev=abs(S-S_Prev);
S_Prev=S;
F_x=subs(diff(f,x),[x,y,z],[X,Y,Z]); % the coordinate x of the normal vector of the surface at P
F_y=subs(diff(f,y),[x,y,z],[X,Y,Z]); % the coordinate y of the normal vector of the surface at P
F_z=subs(diff(f,z),[x,y,z],[X,Y,Z]); % the coordinate z of the normal vector of the surface at P
q=(Cy*Y^2./(1+sqrt(1-k*Cy^2*Y^2)));
G=(Cx*X^2+Cy*Y^2+(k*Cy-Cx)*q^2);
F1=z-G/(1+sqrt(1-Cx*G));
F2=F_x*Dir(1)+F_y*Dir(2)+F_z*Dir(3);
S=S-F1/F2;
Const=abs(S-S_Prev);
if Const>Const_Prev
path='Out';
return
end
X=X_1+Dir(1)*S;
Y=Y_1+Dir(2)*S;
Z=Z_1+Dir(3)*S;
abs(S-S_Prev)
if ~(isreal(S_f))
path='Out';
return
end
end
Dir=[01]
if
-语句的一部分进行计算之前,Const
具有以下值:
Const =
abs(z)
这可以通过使用调试器并放置z
没有被替换F_x
、F_y
和F_z
时使用subs
,但定义F1
时仍使用z
。相反,请使用:
F1=Z-G/(1+sqrt(1-Cx*G));
现在,Const
应该有一个值,并且没有符号表达式试试:
if double(Const)>double(Const_Prev)
你能发布一个我可以复制/粘贴的帖子来重现你的问题吗。在生成大量虚拟输入后,我终于收到了您的错误消息。当我查看Const
时,我得到一个表达式,表示为x
和y
——它不会计算为单个值,因此不能使用double
转换,也不能在if
语句中计算。谢谢。这是上面的一个工作示例。