Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab code would not plot part of the function

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.

like image 229
thephysicsguy Avatar asked Nov 21 '16 14:11

thephysicsguy


1 Answers

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

like image 159
Luis Mendo Avatar answered Oct 19 '22 03:10

Luis Mendo