5

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 First Function

enter image description here

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.

2
  • Interesting, the data is indeed there, but you can not see it. quiver(x1,x2,u1,u2,'o') will show it. Commented Nov 21, 2016 at 15:02
  • @AnderBiguri Yeah I just tried that and it works but I do want the vector form. It is so weird. Commented Nov 21, 2016 at 15:11

1 Answer 1

6

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

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Oh yes of course, for the first iteration it is finding 1/0^n and hence the Inf values. Don't know why I didn't just have a look at the u1 and u2 arrays. Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.