Matlab代码不会绘制函数的一部分
问题描述:
我最近尝试在我的流体问题集中绘制一些教程问题的velocicty字段。我写了下面Matlab代码Matlab代码不会绘制函数的一部分
clear;
h_theta_multiple=0.01;
h_theta=h_theta_multiple*2*pi;
h_rho=0.1;
[theta,rho] = meshgrid(0:h_theta:2*pi,0:h_rho:5);
[x1,x2] = pol2cart(theta,rho);
N=[1 2 3 -1 -2 -3];
figure
for i=1:length(N)
n=N(i);
u1 = rho.^n .* cos(n.*theta);
u2 = rho.^n .* sin(n.*theta);
subplot(2,3,i);
quiver(x1,x2,u1,u2);
end
figure
for i=1:length(N)
n=N(i);
u1 = -rho.^n .* sin(n.*theta);
u2 = rho.^n .* cos(n.*theta);
subplot(2,3,i);
quiver(x1,x2,u1,u2);
end
第一和第二功能。我无法弄清楚为什么它不会为n绘制阴性...我试图隔离一切,但最终还是无法完全调试它。
答
问题是,对于负数n
,矩阵u1
和u2
在某些条目中包含无限值。 quiver
自动缩放值,因此所有内容都被压缩为零,因此不会显示在图形中。
一种解决方案是通过NaN
更换无限值:
clear;
h_theta_multiple=0.01;
h_theta=h_theta_multiple*2*pi;
h_rho=0.1;
[theta,rho] = meshgrid(0:h_theta:2*pi,0:h_rho:5);
[x1,x2] = pol2cart(theta,rho);
N=[1 2 3 -1 -2 -3];
figure
for i=1:length(N)
n=N(i);
u1 = rho.^n .* cos(n.*theta);
u2 = rho.^n .* sin(n.*theta);
u1(isinf(u1)) = NaN; % replace infinite values by NaN
u2(isinf(u2)) = NaN;
subplot(2,3,i);
quiver(x1,x2,u1,u2);
end
figure
for i=1:length(N)
n=N(i);
u1 = -rho.^n .* sin(n.*theta);
u2 = rho.^n .* cos(n.*theta);
u1(isinf(u1)) = NaN; % replace infinite values by NaN
u2(isinf(u2)) = NaN;
subplot(2,3,i);
quiver(x1,x2,u1,u2);
end
这给
+0
哦,当然,第一次迭代是找到1/0^n因此Inf值。不知道为什么我没有看看u1和u2阵列。非常感谢! – thephysicsguy
有趣的是,该数据确实存在,但你不能看到它。 '颤抖(x1,x2,u1,u2,'o')'会显示出来。 –
@AnderBiguri是的,我刚刚尝试过,它的工作原理,但我确实想要矢量形式。这太奇怪了。 – thephysicsguy