首页 文章

Matlab重复x轴进行插值

提问于
浏览
3

我必须插入风向 . 每1000 [ft]给出数据 . 例如:

%winddata input in feet en degrees
x=0:1000:10000;
grad=[340 350 360 1 10 20 30 35 34 36 38];

插值效果很好,我使用interp1函数 . (请参阅下面的代码 . )但是,从360度到1度的步骤是个问题 . 我希望Matlab直接(顺时针)从360度到1度插值,而不是逆时针插入从360-359-358 -... 3-2-1脱脂的值 . 插入风向时,这没有意义 .

我怎样才能命令Matlab重复x轴并每360度重复一次?

clear all;clc;
h=2000;

%winddata input in feet en degrees!
x=0:1000:10000;
degrees=[340 350 360 1 10 20 30 35 34 36 38];    

%conversion to SI:
x=0.3048*x;
u=0:1:max(x);

yinterp1 = interp1(x,degrees,u,'linear');

figure(3)
plot(degrees,x,'bo',yinterp1,u,'-r')
xlabel('wind direction [degrees]') 
ylabel('height [m]')
title 'windspeed lineair interpolated with function interp'

wind direction interpolation, the 360->1 degrees jump is the problem

1 回答

  • 2

    问题是Matlab虽然很聪明,却没有使用度数,所以它没有理由认为 360 = 0 . 因此,我相信你的问题不在于找到一种方法每隔360度重复一次绘图,而是用你正在输入 interp1 函数的数据,因为目前你告诉它在点之间有一条直线 (0, 950)(360, 750) .

    最简单但最丑陋的方法是将360添加到较低的值,因此您的度矢量读取:

    degrees = [340 350 360 361 370 380 390 395 394 396 398];
    

    然后从 degreesyinterp1 向量中减去360:

    clear all;clc;
    h=2000;
    
    %winddata input in feet en degrees!
    x=0:1000:10000;
    degrees=[340 350 360 361 370 380 390 395 394 396 398];    
    
    %conversion to SI:
    x=0.3048*x;
    u=0:1:max(x);
    
    yinterp1 = interp1(x,degrees,u,'linear');
    
    figure(3)
    plot(degrees-360,x,'bo',yinterp1-360,u,'-r')
    xlabel('wind direction [degrees]') 
    ylabel('height [m]')
    title 'windspeed lineair interpolated with function interp'
    xlim([-180 180]);
    

    显而易见的问题是它无法应用于所有情况,但如果您只需要一次性,那么它运行良好 .

    对于更通用的解决方案,您可以使用它,以便手动输入一个点,在该点下面添加了360个值:

    clear all;clc;
    h=2000;
    
    % --------------------- Manual cutoff for rectification -------------------
    limitDegrees = 180;
    % -------------------------------------------------------------------------
    %winddata input in feet en degrees!
    x=0:1000:10000;
    degrees=[340 350 360 1 10 20 30 35 34 36 38];   
    
    %conversion to SI:
    x=0.3048*x;
    u=0:1:max(x);
    
    
    indecesTooSmall = find(degrees <= limitDegrees);
    oneVec = zeros(size(degrees));
    
    oneVec(indecesTooSmall) = 1;
    
    vecToAdd = 360*ones(size(degrees));
    vecToAdd = vecToAdd .* oneVec;
    
    newDegrees = degrees + vecToAdd;
    
    yinterp1 = interp1(x,newDegrees,u,'linear');
    
    figure(3)
    plot(newDegrees-360,x,'bo',yinterp1-360,u,'-r')
    xlabel('wind direction [degrees]') 
    ylabel('height [m]')
    title 'windspeed lineair interpolated with function interp'
    xlim([-180 180]);
    

    以上两种解决方案均提供以下内容:

    Correct plot

    编辑:基本上更容易解决方案,只需使用 rad2deg(unwrap(deg2rad(degrees))) ,或尝试找到适用于度数的解包 .

相关问题