如何插入随机非单调递增数据

时间:2015-07-06 17:23:21

标签: matlab interpolation spline

所以我正在研究论文,我需要计算一个翼型的几何特征。

为此,我需要插入翼型的水平和垂直坐标。这用于一个工具,它将自动计算来自随机翼型几何文件的几何特征。

有时翼型的Y值是非单调的。因此,interp1命令会产生错误,因为Y向量中的某些值会重复。

因此,我的问题是:如何在Matlab中自动识别并随后插入非单调递增数据。

以下是一个示例数据集:

 0.999974    0.002176
 0.994846    0.002555
 0.984945    0.003283
 0.973279    0.004131
 0.960914    0.005022
 0.948350    0.005919
 0.935739    0.006810
 0.923111    0.007691
 0.910478    0.008564
 0.897850    0.009428
 0.885229    0.010282
 0.872617    0.011125
 0.860009    0.011960
 0.847406    0.012783
 0.834807    0.013598
 0.822210    0.014402
 0.809614    0.015199
 0.797021    0.015985
 0.784426    0.016764
 0.771830    0.017536
 0.759236    0.018297
 0.746639    0.019053
 0.734038    0.019797
 0.721440    0.020531
 0.708839    0.021256
 0.696240    0.021971
 0.683641    0.022674
 0.671048    0.023367
 0.658455    0.024048
 0.645865    0.024721
 0.633280    0.025378
 0.620699    0.026029
 0.608123    0.026670
 0.595552    0.027299
 0.582988    0.027919
 0.570436    0.028523
 0.557889    0.029115
 0.545349    0.029697
 0.532818    0.030265
 0.520296    0.030820
 0.507781    0.031365
 0.495276    0.031894
 0.482780    0.032414
 0.470292    0.032920
 0.457812    0.033415
 0.445340    0.033898
 0.432874    0.034369
 0.420416    0.034829
 0.407964    0.035275
 0.395519    0.035708
 0.383083    0.036126
 0.370651    0.036530
 0.358228    0.036916
 0.345814    0.037284
 0.333403    0.037629
 0.320995    0.037950
 0.308592    0.038244
 0.296191    0.038506
 0.283793    0.038733
 0.271398    0.038920
 0.259004    0.039061
 0.246612    0.039153
 0.234221    0.039188
 0.221833    0.039162
 0.209446    0.039064
 0.197067    0.038889
 0.184693    0.038628
 0.172330    0.038271
 0.159986    0.037809
 0.147685    0.037231
 0.135454    0.036526
 0.123360    0.035684
 0.111394    0.034690
 0.099596    0.033528
 0.088011    0.032181
 0.076685    0.030635
 0.065663    0.028864
 0.055015    0.026849
 0.044865    0.024579
 0.035426    0.022076
 0.027030    0.019427
 0.019970    0.016771
 0.014377    0.014268
 0.010159    0.012029
 0.007009    0.010051
 0.004650    0.008292
 0.002879    0.006696
 0.001578    0.005207
 0.000698    0.003785
 0.000198    0.002434
 0.000000    0.001190
 0.000000    0.000000
 0.000258   -0.001992
 0.000832   -0.003348
 0.001858   -0.004711
 0.003426   -0.005982
 0.005568   -0.007173
 0.008409   -0.008303
 0.012185   -0.009379
 0.017243   -0.010404
 0.023929   -0.011326
 0.032338   -0.012056
 0.042155   -0.012532
 0.052898   -0.012742
 0.064198   -0.012720
 0.075846   -0.012533
 0.087736   -0.012223
 0.099803   -0.011837
 0.111997   -0.011398
 0.124285   -0.010925
 0.136634   -0.010429
 0.149040   -0.009918
 0.161493   -0.009400
 0.173985   -0.008878
 0.186517   -0.008359
 0.199087   -0.007845
 0.211686   -0.007340
 0.224315   -0.006846
 0.236968   -0.006364
 0.249641   -0.005898
 0.262329   -0.005451
 0.275030   -0.005022
 0.287738   -0.004615
 0.300450   -0.004231
 0.313158   -0.003870
 0.325864   -0.003534
 0.338565   -0.003224
 0.351261   -0.002939
 0.363955   -0.002680
 0.376646   -0.002447
 0.389333   -0.002239
 0.402018   -0.002057
 0.414702   -0.001899
 0.427381   -0.001766
 0.440057   -0.001656
 0.452730   -0.001566
 0.465409   -0.001496
 0.478092   -0.001443
 0.490780   -0.001407
 0.503470   -0.001381
 0.516157   -0.001369
 0.528844   -0.001364
 0.541527   -0.001368
 0.554213   -0.001376
 0.566894   -0.001386
 0.579575   -0.001398
 0.592254   -0.001410
 0.604934   -0.001424
 0.617614   -0.001434
 0.630291   -0.001437
 0.642967   -0.001443
 0.655644   -0.001442
 0.668323   -0.001439
 0.681003   -0.001437
 0.693683   -0.001440
 0.706365   -0.001442
 0.719048   -0.001444
 0.731731   -0.001446
 0.744416   -0.001443
 0.757102   -0.001445
 0.769790   -0.001444
 0.782480   -0.001445
 0.795173   -0.001446
 0.807870   -0.001446
 0.820569   -0.001446
 0.833273   -0.001446
 0.845984   -0.001448
 0.858698   -0.001448
 0.871422   -0.001451
 0.884148   -0.001448
 0.896868   -0.001446
 0.909585   -0.001443
 0.922302   -0.001445
 0.935019   -0.001446
 0.947730   -0.001446
 0.960405   -0.001439
 0.972917   -0.001437
 0.984788   -0.001441
 0.994843   -0.001441
 1.000019   -0.001441

第一列是X,第二列是Y.注意Y的最后一个值是如何重复的。

也许有人可以为我提供一段代码来执行此操作?或者也欢迎任何建议。

请记住,我需要自动执行此过程。

感谢您的时间和精力,我真的很感激!

2 个答案:

答案 0 :(得分:2)

要插入任何函数,必须定义一个函数。定义y=f(x)时,对于x的两个不同值,您不能使用相同的y因为我们不是在讨论函数。在您的示例数据中,xy都不是单调的,因此无论如何,您都会对其进行切片,您将拥有两个(或更多)" y" s #34; X&#34 ;.如果您希望进行插值,则需要将其分为两个单独的问题,顶部/底部,并定义interp1/2/n使用的正确函数,例如,将x==0水平切片。x。在任何情况下,您都必须提供额外的信息,而不仅仅是yx=0.5,例如:yx位于顶部。

This is your air foil

另一方面,如果您只想在数组中的每个y%// transform your original xy into 3d array where x is in first slice and y in second xy = permute(xy(85:95,:), [3,1,2]); %// 85:95 is near x=0 in your data %// lets say you want to insert three additional points along each line between every two points on given airfoil h = [0, 0.25, 0.5, 0.75].'; %// steps along each line - column vector %// every interpolated h along the way between f(x(n)) and f(x(n+1)) can %// be defined as: f(x(n) + h) = f(x(n)) + h*( f(x(n+1)) - f(x(n)) ) %// this is first order finite differences approximation in 1D. 2D is very %// similar only with gradient (this should be common knowledge, look it up) %// from here it's just fancy matrix play %// 2D gradient of xy curve gradxy = diff(xy, 1, 2); %// diff xy, first order, along the 2nd dimension, where x and y now run h_times_gradxy = bsxfun(@times, h, gradxy); %// gradient times step size xy_in_3d_array = bsxfun(@plus, xy(:,1:end-1,:), h_times_gradxy); %// addition of "f(x)" and there we have it, the new x and y for every step h [x,y] = deal(xy_in_3d_array(:,:,1), xy_in_3d_array(:,:,2)); %// extract x and y from 3d matrix xy_interp = [x(:), y(:)]; %// use Matlab's linear indexing to flatten x and y into columns %// plot to check results figure; ax = newplot; hold on; plot(ax, xy(:,:,1), xy(:,:,2),'o-'); plot(ax, xy_interp(:,1), xy_interp(:,2),'+') legend('Original','Interpolated',0); axis tight; grid; %// The End 之间插入一些值,则可以使用有限差异来执行此操作:

x=0

这些是结果, SELECT cl.LDGR_YEAR, cl.LDGR_MONTH, cl.LDGR_PER_COV_FROM, cl.LDGR_PREV_RDNG, cl.LDGR_PER_COV_TO, cl.LDGR_PRES_RDNG, cl.LDGR_KWH_USED, cl.LDGR_BILL_AMOUNT FROM customer_ledger AS cl INNER JOIN users AS u ON cl.LDGR_CONTRACT_NO=u.LDGR_CONTRACT_NO WHERE u.Username = 'kim' ORDER BY cl.LDGR_PER_COV_TO DESC LIMIT 5 附近为了清晰呈现:

enter image description here

希望有所帮助。 欢呼声。

答案 1 :(得分:2)

如果您不知道定义箔片轮廓的确切功能,则会有快速而肮脏的方法。将您的数据拆分为2组,顶部和底部平面,因此' x'数据为monotonic increasing

首先我将数据表导入变量A,然后:

%// just reorganise your input in individual vectors. (this is optional but
%// if you do not do it you'll have to adjust the code below)
x = A(:,1) ;    
y = A(:,2) ;

ipos = y >  0 ;                             %// indices of the top plane
ineg = y <= 0 ;                             %// indices of the bottom plane

xi = linspace(0,1,500) ;                    %// new Xi for interpolation

ypos = interp1( x(ipos) , y(ipos) , xi ) ;  %// re-interp the top plane
yneg = interp1( x(ineg) , y(ineg) , xi ) ;  %// re-interp the bottom plane

y_new = [fliplr(yneg) ypos] ;               %// stiches the two half data set together
x_new = [fliplr(xi) xi] ;

%% // display
figure
plot(x,y,'o')
hold on
plot(x_new,y_new,'.r')
axis equal

foil

如上所述,它很快而且很脏。从细节图中可以看出,在曲线接近水平方向的区域中,您可以通过这种方式大大提高x分辨率,但是在箔片的绞索上你会松散一点分辨率。轮廓接近垂直方向。

如果它可以接受,那么你已经全部设定了。如果您确实需要在鼻子处进行分辨率,您可以查看x上的插值,但是在绞索附近做一个非常精细的x网格(而不是我提供的常规x网格例如) 如果您将以上xi定义替换为:

xi = [linspace(0,0.01,50) linspace(0.01,1,500)] ;

你的鼻子附近有以下几点:

foildetail

根据您的需要进行调整。