Post Reply 
Linear Exponential Combination Fit
11-30-2020, 03:45 PM
Post: #1
Linear Exponential Combination Fit
http://edspi31415.blogspot.com/2020/11/h...ation.html

The program LINEXPREG attempts to fit bivariate data to the curve:

y = a + b * x + c * e^x

The LSQ (least square) function is used. The output is a list of three matrices:


* A matrix of coefficients: [ [ a ] [ b ] [ c ] ]

* A matrix of y values entered

* A matrix of predicted y values


HP Prime Program LINEXPREG

Code:
EXPORT LINEXPREG(lx,ly)
BEGIN
// 2020-11-17 EWS
// x list, y list
LOCAL n,lx2,lx3,mx,my,k,mr,mq;
n:=SIZE(lx);
lx2:=e^(lx);
lx3:={};
FOR k FROM 1 TO n DO
lx3:=CONCAT(lx3,{1,lx(k),lx2(k)});
END;
mx:=list2mat(lx3,3);
my:=list2mat(ly,1);
mq:=LSQ(mx,my);
mr:=mq(1,1)+mq(2,1)*lx+
mq(3,1)*e^(lx);
mr:=list2mat(mr,1);
RETURN {mq,my,mr};
END;

Example

x list: {0, 1, 2, 3, 4, 5}
y list: {2, 7, 13, 18, 26, 34}

LINEXPREG({0, 1, 2, 3, 4, 5},{2, 7, 13, 18, 26, 34})

Results: {coefficients, y values, predicted y values}

coefficients:
[ [ 1.74935499143 ]
[ 5.39455446221 ]
[ 3.66584105034E-2 ] ]

y values:
[ [ 2 ]
[ 7 ]
[ 13 ]
[ 18 ]
[ 26 ]
[ 34 ] ]

predicted y values:
[ [ 1.78601340193 ]
[ 7.24355734477 ]
[ 12.8093349675 ]
[ 18.6693222357 ]
[ 25.3290542368 ]
[ 34.1627178129 ] ]

Equation:
y = 1.74935499143 + 5.39455446221 * x + 3.66584105034E-2 * e^x
Visit this user's website Find all posts by this user
Quote this message in a reply
12-03-2020, 01:39 AM (This post was last modified: 08-21-2023 03:42 PM by Albert Chan.)
Post: #2
RE: Linear Exponential Combination Fit
Hi, Eddie

Thanks for the post. I did not know HP Prime had LSQ function.

It might be simpler not to create a flatten list (with CONCAT), then reconstruct the matrix.
We could build the matrix, functional style:

CAS> lx := range(6)
CAS> ly := [2, 7, 13, 18, 26, 34]
CAS> fit := LSQ(transpose([0*lx .+ 1., lx, e^lx]), ly)

[[1.74935499144],[5.39455446221],[3.66584105036e−2]]

CAS> [[1,x,e^x]] * fit

[[ 3.66584105036E−2*e^x + 5.39455446221*x + 1.74935499144 ]]

Comment: Aug 21, 2023

".+" is supposed to apply sum element-by-element.

However, XCas (lx-lx) .+ 1. does not populate matrix with all one's, (0*lx .+ 1.) does.
To bypass the bug, above is adjusted so that both CAS, XCAS produce same results.

Also, if we only need polynomial regression, there is a shortcut

polynomial_regression(lx, ly, degree_needed)
Find all posts by this user
Quote this message in a reply
Post Reply 




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