我在下面写了代码来显示一个矢量字段:
clear all;
close all;
phi = 90;
[x,y] = meshgrid(-3:0.1:3,-3:0.1:3);
u = (x.*(-1+3.*(x.*cosd(phi)+y.*sind(phi)).^2./(x.^2+y.^2))./(x.^2+y.^2).^(3/2))+...
((x-2).*(-1+3.*((x-2).*cosd(phi)+y.*sind(phi)).^2./((x-2).^2+y.^2))./((x-2).^2+y.^2).^(3/2))+...
((x+2).*(-1+3.*((x+2).*cosd(phi)+y.*sind(phi)).^2./((x+2).^2+y.^2))./((x+2).^2+y.^2).^(3/2));
v = (y.*(-1+3.*(x.*cosd(phi)+y.*sind(phi)).^2./(x.^2+y.^2))./(x.^2+y.^2).^(3/2))+...
(y.*(-1+3.*((x-2).*cosd(phi)+y.*sind(phi)).^2./((x-2).^2+y.^2))./((x-2).^2+y.^2).^(3/2))+...
(y.*(-1+3.*((x+2).*cosd(phi)+y.*sind(phi)).^2./((x+2).^2+y.^2))./((x+2).^2+y.^2).^(3/2));
h = streamslice(x,y,u,v,0.5);
问题是,空间中有三个点u
和v
变为无限:(y=0,x=0)
,(y=0,x=-2)
和(y=0,x=2)
。当v
和u
变为无限时,该区域中没有向量字段,输出为空区域。我希望MATLAB省略v
和u
的无限部分,并绘制v
和u
的其他部分。例如,在(y=0,x=0)
,我想在下面显示向量字段(这不是无限的):
u = ((x-2).*(-1+3.*((x-2).*cosd(phi)+y.*sind(phi)).^2./((x-2).^2+y.^2))./((x-2).^2+y.^2).^(3/2))+...
((x+2).*(-1+3.*((x+2).*cosd(phi)+y.*sind(phi)).^2./((x+2).^2+y.^2))./((x+2).^2+y.^2).^(3/2));
v = (y.*(-1+3.*((x-2).*cosd(phi)+y.*sind(phi)).^2./((x-2).^2+y.^2))./((x-2).^2+y.^2).^(3/2))+...
(y.*(-1+3.*((x+2).*cosd(phi)+y.*sind(phi)).^2./((x+2).^2+y.^2))./((x+2).^2+y.^2).^(3/2));
答案 0 :(得分:0)
您可以创建索引以仅替换非有限值:
u = [1 1 inf NaN 1 1];
v = [1 1 inf NaN 1 1];
u1 = [2 2 2 2 2 2];
v1 = [2 2 2 2 2 2];
%Now, if an element of u or v is not finite it is replaced by the same value of the matrices u1,v1.
u(~isfinite(u))=u1(~isfinite(u));
v(~isfinite(v))=v1(~isfinite(v));
第二个选项:
您还可以使用griddata
:
finite = isfinite(u) & isfinite(v);
u = griddata(x(finite), y(finite), u(finite), x, y);
v = griddata(x(finite), y(finite), v(finite), x, y);
<强> BEFORE:强>
<强> AFTER:强>