今天要处理一些极坐标的数据,把他们转化成为直角坐标,本来这事不复杂,在python里面就一个命令的事情就可以把储存在极坐标下(矩阵大小M*N)的数据转化为任意大小的矩阵来储存直角坐标下的数据,其原理就是简单的线性插值。
http://liao.cpython.org/scipytutorial11/
但是坑爹的matlab的插值命令girddata(x,y,u,X,Y)要求x,y,u必须是一维数组,X,Y必须是二维数组,导致不像python一样直接能转换。写了一个简单的循环算法来把极坐标数据转换为直角坐标,用了循环嵌套和计数器,所以代码效率极低,但是也不是很想去优化,能用就行了。放在这里给大家借鉴,欢迎提出代码修改意见。
clear all
data = csvread('REP_2Dvelocity.csv');
r1 = reshape(data(:,1),[201,51])';
ux1 = reshape(data(:,3),[201,51])';
r1 = reshape(data(:,1),[201,51])';
z1 = reshape(data(:,2),[201,51])';
ux1 = reshape(data(:,3),[201,51])';
uz1 = reshape(data(:,4),[201,51])';
r2 = -flip(r1,2);
z2 = flip(z1,2);
ux2 = -flip(ux1,2);
uz2 = flip(uz1,2);
r = cat(2,r2,r1(:,2:201));
z = cat(2,z2,z1(:,2:201));
ux = cat(2,ux2,ux1(:,2:201));
uz = cat(2,uz2,uz1(:,2:201));
r = r(41,:);
ux = ux(41,:);
n = 0;
for theta = 0:pi/360:pi
for i = 1:401
n = n+1;
R = r(i);
x(n) = r(i)*cos(theta);
y(n) = r(i)*sin(theta);
U(n) = ux(i)*cos(theta);
V(n) = ux(i)*sin(theta);
end
end
[X,Y] = meshgrid(-150:150,-150:150);
uq = griddata(x,y,U,X,Y);
vq = griddata(x,y,V,X,Y);
quiver(X,Y,uq,vq);