Post Reply 
[VA] SRC#005- April, 1st Mean Minichallenge
04-03-2019, 11:56 PM
Post: #4
RE: [VA] SRC#005- April, 1st Mean Minichallenge
 
Hi, all:
      
Well, I thought that this would make an interesting April, 1st entry but it seems I was pretty overoptimistic judging from Albert Chan being the only one who showed any interest by posting an amazingly concise solution for the HP-12C no less, complete with all correct solutions and equations to boot. Thanks a lot, Albert !

My original solution is the following 3-line subprogram (to vary a little from main programs and user-defined functions) for the HP-71B, which also deals with the Geometric Mean as well (unlike Albert's HP-12C program which does not, as he states himself):

      60  SUB GMEAN(A(),L,M) @ N=UBND(A,1) @ K=L-2 @ IF K THEN
      70  S=0 @ FOR I=1 TO N @ S=S+A(I)^K @ NEXT I @ M=(S/N)^(1/K) @ ELSE
      80  S=1 @ FOR I=1 TO N @ S=S*A(I)   @ NEXT I @ M=S^(1/N) @ END IF


The subprogram GMEAN (Generalized Mean), which can be called from any program in the RAM filesystem, accepts as parameters the array containing the dataset, the value of k, and returns in the specified variable (passed by reference) the value of the mean Mk. To test it and return the results I asked for, simply use this caller main program:

      10   DESTROY ALL @ OPTION BASE 1 @ DIM A(3),M @ READ A
      20   DATA 4,1,20.19
      30   INPUT K @ CALL GMEAN(A,K,M) @ DISP K;M

      >RUN
            ? 1       ->   2.30852787042    Harmonic mean
            ? 2       ->   4.32247115132    Geometric mean
            ? 3       ->   8.39666666667    Arithmetic mean
            ? 4       ->   11.8972840038    Quadratic mean

            ? 5/2     ->   6.2388855918     "midway" (!) between the Arithmetic Mean and the Geometric Mean
            ? PI      ->   8.98125368883
            ? -2.019  ->   1.31312310009
            ? 0.61    ->   1.97983439045

Notice that the limit of Mk when k tends to -Infinity is the minimum of the values in the dataset (some mean !) while, conversely, the limit of Mk when k tends to +Infinity is the maximum of the values in the dataset (ditto). To approximately check this:

      >RUN
            ? -380       ->   1.00288008791    { M-Inf = Min( x1, x2, ..., xn ) = 1 }
            ?  380       ->   20.1314053436    { M+Inf = Max( x1, x2, ..., xn ) = 20.19 }


Finally, to find the value of  k  which makes  Mk = Pi  we use this wrapper code (which assumes you've run the main program at least once) :

      40   INPUT M @ K=FNROOT(1,5,FNM(FVAR,M)-M) @ DISP K;FNM(K,M)
      50   DEF FNM(K,M) @ CALL GMEAN(A,K,M) @ FNM=M @ END DEF

      >RUN 40
            ? PI
                  1.55425243272      3.14159265359 { = M1.55425243272 }



That's all for now. Thanks again to Albert Chan for his interest and his worthy contribution, much appreciated. Smile

V.
 

  
All My Articles & other Materials here:  Valentin Albillo's HP Collection
 
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: [VA] SRC#005- April, 1st Mean Minichallenge - Valentin Albillo - 04-03-2019 11:56 PM



User(s) browsing this thread: 4 Guest(s)