How to resolve the algorithm Averages/Mean angle step by step in the Erlang programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Averages/Mean angle step by step in the Erlang programming language

Table of Contents

Problem Statement

When calculating the average or mean of an angle one has to take into account how angles wrap around so that any angle in degrees plus any integer multiple of 360 degrees is a measure of the same angle. If one wanted an average direction of the wind over two readings where the first reading was of 350 degrees and the second was of 10 degrees then the average of the numbers is 180 degrees, whereas if you can note that 350 degrees is equivalent to -10 degrees and so you have two readings at 10 degrees either side of zero degrees leading to a more fitting mean angle of zero degrees. To calculate the mean angle of several angles:

(Note that, since the mean is the sum divided by the number of numbers, and division by a positive real number does not affect the angle, you can also simply compute the sum for step 2.) You can alternatively use this formula:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Averages/Mean angle step by step in the Erlang programming language

Source code in the erlang programming language

-module( mean_angle ).
-export( [from_degrees/1, task/0] ).

from_degrees( Angles ) ->
	Radians = [radians(X) || X <- Angles],
	Sines = [math:sin(X) || X <- Radians],
	Coses = [math:cos(X) || X <- Radians],
	degrees( math:atan2( average(Sines), average(Coses) ) ).

task() ->
	Angles = [[350, 10], [90, 180, 270, 360], [10, 20, 30]],
	[io:fwrite( "Mean angle of ~p is: ~p~n", [X, erlang:round(from_degrees(X))] ) || X <- Angles].


average( List ) -> lists:sum( List ) / erlang:length( List ).

degrees( Radians ) -> Radians * 180 / math:pi().

radians( Degrees ) -> Degrees * math:pi() / 180.


  

You may also check:How to resolve the algorithm Averages/Root mean square step by step in the Qi programming language
You may also check:How to resolve the algorithm Primes: n*2^m+1 step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Call a function step by step in the SenseTalk programming language
You may also check:How to resolve the algorithm Keyboard input/Keypress check step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Character codes step by step in the Nim programming language