Search code examples
matlabderivativeheading

Calculating difference and direction between two headings


I have a large array with values representing wind direction in degrees(0-360) I am required to calculate the instantaneous rate of change with respect to time, using backwards, forward and then central difference methods.

So far I have managed to calculate the smallest angle difference between elements, but I cant figure out how to also establish a direction, ie, +ve difference for clockwise, -ve for anticlockwise.

for example

winddir =

80
80
70
70 
60
360
330

diff = min((abs(diff(winddir))),(360- abs(diff(winddir))));

diff =

 0
10
 0
10
60
30

This gives the correct values of difference between headings to calculate the derivative, but fails to show direction clockwise and anticlockwise, which I need! I've been trying to figure it out for hours and I'm just going around in circles, could anyone help me out?


Solution

  • First of all, it is very bad practice to use MATLAB's function names as variable names.

    Then, in order to calculate your differences the way your want :

    myDiff=diff(winddir);
    myDiff(myDiff<-180)=myDiff(myDiff<-180)+360;
    myDiff(myDiff>180)=myDiff(myDiff>180)-360;