I tried recently to plot some velocicty fields for a tutorial problem in my fluids problem set. I wrote the following Matlab code
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
It gives me the following
for the first and second functions respectively. I can't quite figure out why it doesn't plot the ones for n is negative... I tried to isolate everything but couldn't quite debug it in the end.
The problem is that for negative n
the matrices u1
and u2
contain infinite values in some entries. quiver
auto-scales the values, so everything gets compressed down to zero and thus does not show in the graph.
A solution is to replace infinite values by 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
This gives
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With