Post Reply 
Cosine Regression
07-28-2017, 10:21 PM (This post was last modified: 07-28-2017 10:22 PM by Eddie W. Shore.)
Post: #1
Cosine Regression
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)
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




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