HP Forums
Cosine Regression - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: HP Prime Software Library (/forum-15.html)
+--- Thread: Cosine Regression (/thread-8743.html)



Cosine Regression - Eddie W. Shore - 07-28-2017 10:21 PM

Introduction

The program COSREG attempts to fit data to the curve:

Y = A*cos(C*(X-D)) +B

The user will provide the x-data, y-data, and the period of the data. Radians mode will be used.

HP Prime Program: COSREG
Arguments x list, y list, estimated period
Code:

EXPORT COSREG(L1,L2,P)
BEGIN
// EWS 2017-07-25
// x list, y list, period 
// Estimated Cosine Regression
// Y = A*COS(C(X-D))+B
// Radians mode

HAngle:=0;

LOCAL C:=2*π/P;
LOCAL I:=POS(L2,MAX(L2));
LOCAL D:=L1(I);
LOCAL S:=SIZE(L1);
LOCAL L0:=MAKELIST(1,X,1,S);
L1:=COS(C*L1-D);


LOCAL M1,M2,M3;
M1:=list2mat(CONCAT(L0,L1),S);
M1:=TRN(M1);
M2:=list2mat(L2,S);
M2:=TRN(M2);
M3:=CAS.LSQ(M1,M2);
LOCAL A:=M3[2,1];
LOCAL B:=M3[1,1];

RETURN {"A*COS(C(X-D))+B",
A,C,D,B};

END;

The coefficients are returned in a list.

Cosine Regression: Y = A * COS(C*(X – D)) + B

Example 1

X = {0, π/4, π/2, 3π/4 ,1}
Y = {5, 2, -1, 2, 5}
Period: π

Results:

A = 3
C = 2
D = 0
B = 2

(Y = 2 COS(2X) + 3)