Linear regression analysis program for DM41(L)

 Posts: 498
 Joined: Mon Apr 24, 2017 6:38 pm
Linear regression analysis program for DM41(L)
Had a little fun this morning bashing out a program for the DM41 that I can use in my lab.
EDIT: CODE REMOVED BECAUSE BETTER CODE IS POSTED BELOW.
Edited to add:
I've just written a more sophisticated program for the error estimates. It estimates far lower errors, which might be a good thing by some measures but in a highschool laboratory I actually prefer the errors this program produces, so I'm sticking with it. Yes, it overestimates errors in a rather clunky way, but they are close to estimates made manually from a graph.
EDIT: CODE REMOVED BECAUSE BETTER CODE IS POSTED BELOW.
Edited to add:
I've just written a more sophisticated program for the error estimates. It estimates far lower errors, which might be a good thing by some measures but in a highschool laboratory I actually prefer the errors this program produces, so I'm sticking with it. Yes, it overestimates errors in a rather clunky way, but they are close to estimates made manually from a graph.
Last edited by keithdalby on Thu Feb 01, 2018 11:22 am, edited 1 time in total.

 Posts: 498
 Joined: Mon Apr 24, 2017 6:38 pm
Re: Linear regression analysis program for DM41(L)
A couple of lab programs for your DM41(L), if you're interested. These all use the default stats register numbers. Some of these programs use registers 0104, but do not clear the registers after their use.
GRD finds the gradient of the X and Y data stored in the stats registers
INTRCPT finds the yintercept of the X and Y data stored in the stats registers
PMCC finds the Pearson Product Moment Correlation Coefficient of the X and Y data stored in the stats registers
STERROR estimates the standard error of the X and Y data stored in the stats registers using the PMCC
STATS just executes the programs above in order to display the gradient, standard error, error in gradient, intercept, standard error (again) and error in intercept sequentially, requiring a R/S press betwixt each. Leaves the gradient on the X register, the intercept on the Y register and the standard error of the regression on the Z register
BEEPER plays a tone every X seconds, so that measurements can be taken at regular times without having to keep an eye on a stopwatch or timer. Accuracy is iffy, but it is good enough for a high school lab. Can accept decimal seconds, doesn't need integer values
OSCILLS asks how many oscillations to time for, then each press of the R/S key increases the counter until that number is met. At that point, a tone sounds (so you don't have to watch the screen) to indicate you have counted the appropriate number of oscillations. After this, presses of R/S display the total time expired and the time period of the oscillations. With time periods less than a second it can be iffy. Leaves the time period on the X register and the total time on the Y register
STDEV estimates the standard deviation in X and Y from the stat registers, then leaves the standard deviation of X on the X register and of Y on the Y register
I also think it's handy to ASN the SW to a key too, as well as the above programs, for use in the lab. Hope you find them useful!
EDIT TO ADD: Tweaked some programs
GRD finds the gradient of the X and Y data stored in the stats registers
Code: Select all
LBL "GRD"
RCL 16
RCL 15
*
RCL 11
RCL 13
*

RCL 16
RCL 12
*
RCL 11
X^2

/
RTN
Code: Select all
LBL "INTRCPT"
RCL 13
RCL 12
*
RCL 11
RCL 15
*

RCL 16
RCL 12
*
RCL 11
X^2

/
RTN
Code: Select all
LBL "PMCC"
RCL 16
RCL 15
*
RCL 11
RCL 13
*

RCL 16
RCL 12
*
RCL 11
X^2

RCL 16
RCL 14
*
RCL 13
X^2

*
SQRT
/
RTN
Code: Select all
LBL "STERROR"
XEQ "PMCC"
X^2
CHS
1
+
RCL 16
2

/
SQRT
RTN
Code: Select all
LBL "STATS"
"GRADIENT="
AVIEW
XEQ "GRD"
STO 01
ARCL X
AVIEW
STOP
"%ERROR="
AVIEW
XEQ "STERROR"
STO 03
100
*
ARCL X
AVIEW
STOP
"ERROR="
AVIEW
RCL 03
RCL 01
*
ARCL X
AVIEW
STOP
"INTERCEPT="
AVIEW
XEQ "INTRCPT"
STO 02
ARCL X
AVIEW
STOP
"%ERROR="
AVIEW
RCL 03
100
*
ARCL X
AVIEW
STOP
"ERROR="
AVIEW
RCL 03
RCL 02
*
ARCL X
AVIEW
STOP
RCL 03
RCL 02
RCL 01
"END OF PROG"
AVIEW
CLA
END
Code: Select all
LBL "BEEPER"
"INTERVAL?="
PROMPT
STO 01
TIME ;XROM 26,28
HR
3600
*
STO 02
0
STO 03
LBL 01
RCL 01
RCL 03
*
RCL 02
+
TIME ;XROM 26,28
HR
3600
*
X<=Y?
GTO 01
TONE 06
"N="
ARCL 03
AVIEW
1
ST+ 03
GTO 01
END
Code: Select all
LBL "OSCILLS"
"OSCILLATIONS?="
PROMPT
STO 01
0
STO 02
TIME ;XROM 26,28
HR
3600
*
STO 03
LBL 01
"N="
RCL 02
ARCL 02
AVIEW
STOP
1
ST+ 02
RCL 02
RCL 01
X>Y?
GTO 01
TIME ;XROM 26,28
HR
3600
*
RCL 03

STO 04
TONE 05
"TOTAL TIME="
ARCL 04
AVIEW
STOP
"PERIOD="
AVIEW
RCL 04
RCL 01
/
ARCL X
AVIEW
STOP
RCL 04
RCL 04
RCL 01
/
END
Code: Select all
LBL "STDEV"
RCL 12
RCL 16
1

/
SQRT
STO 01
RCL 14
RCL 16
1

/
SQRT
RCL 01
RTN
EDIT TO ADD: Tweaked some programs
Last edited by keithdalby on Thu Feb 01, 2018 10:42 am, edited 2 times in total.
Re: Linear regression analysis program for DM41(L)
Maybe you could choose a different name for the label, in order to differentiate it from the internal GRAD[ians] command?keithdalby wrote: ↑Thu Feb 01, 2018 7:45 amA couple of lab programs for your DM41(L), if you're interested. These all use the default stats register numbers. Some of these programs use registers 0104, but do not clear the registers after their use.
GRAD finds the gradient of the X and Y data stored in the stats registers
Greetings,
Massimo
+×÷ ↔ left is right and right is wrong
Massimo
+×÷ ↔ left is right and right is wrong

 Posts: 498
 Joined: Mon Apr 24, 2017 6:38 pm
Re: Linear regression analysis program for DM41(L)
Good point. I never use gradians so it doesn't affect me, but feel free to change it if you wish to use it for your own purposesakaTB wrote: ↑Thu Feb 01, 2018 8:27 amMaybe you could choose a different name for the label, in order to differentiate it from the internal GRAD[ians] command?keithdalby wrote: ↑Thu Feb 01, 2018 7:45 amA couple of lab programs for your DM41(L), if you're interested. These all use the default stats register numbers. Some of these programs use registers 0104, but do not clear the registers after their use.
GRAD finds the gradient of the X and Y data stored in the stats registers
EDITED TO ADD: I've gone through and changes it from GRAD to GRD. Thanks for the advice.

 Posts: 447
 Joined: Wed May 03, 2017 5:46 pm
 Location: Malone, NY USA
Re: Linear regression analysis program for DM41(L)
I tried using the DM41 Programming Tool to convert the listing to a file I could send to the 41. I get an error in line 1, LBL GRAD. I tried changing it to LBL GRD but it didn't help. Does anyone know what's going on?
http://dm41.swissmicros.com/
http://dm41.swissmicros.com/
Tom L
Ducator meus nihil agit sine lagunculae leynidae accedunt
DM42 SN: 00025 (Beta)
SN: 00221 (Shipping)
Ducator meus nihil agit sine lagunculae leynidae accedunt
DM42 SN: 00025 (Beta)
SN: 00221 (Shipping)
Re: Linear regression analysis program for DM41(L)
keithdalby wrote: ↑Thu Feb 01, 2018 10:05 am
EDITED TO ADD: I've gone through and changes it from GRAD to GRD. Thanks for the advice.
Thank you for your programs.
Greetings,
Massimo
+×÷ ↔ left is right and right is wrong
Massimo
+×÷ ↔ left is right and right is wrong
Re: Linear regression analysis program for DM41(L)
Just tried with the updated version and it didn't give me any error.toml_12953 wrote: ↑Thu Feb 01, 2018 10:55 amI tried using the DM41 Programming Tool to convert the listing to a file I could send to the 41. I get an error in line 1, LBL GRAD. I tried changing it to LBL GRD but it didn't help. Does anyone know what's going on?
http://dm41.swissmicros.com/
Greetings,
Massimo
+×÷ ↔ left is right and right is wrong
Massimo
+×÷ ↔ left is right and right is wrong

 Posts: 447
 Joined: Wed May 03, 2017 5:46 pm
 Location: Malone, NY USA
Re: Linear regression analysis program for DM41(L)
Thanks for responding so quickly! I found the problem. The converter needs quotes around labels. I didn't add them so all lines referring to labels were in error. Also the listing here uses x for multiply. The converter uses *.akaTB wrote: ↑Thu Feb 01, 2018 10:59 amJust tried with the updated version and it didn't give me any error.toml_12953 wrote: ↑Thu Feb 01, 2018 10:55 amI tried using the DM41 Programming Tool to convert the listing to a file I could send to the 41. I get an error in line 1, LBL GRAD. I tried changing it to LBL GRD but it didn't help. Does anyone know what's going on?
http://dm41.swissmicros.com/
Here's the listing in a format the converter can use:
Code: Select all
LBL "GRD"
RCL 16
RCL 15
*
RCL 11
RCL 13
*

RCL 16
RCL 12
*
RCL 11
X^2

/
RTN
LBL "INTRCPT"
RCL 13
RCL 12
*
RCL 11
RCL 15
*

RCL 16
RCL 12
*
RCL 11
X^2

/
RTN
LBL "PMCC"
RCL 16
RCL 15
*
RCL 11
RCL 13
*

RCL 16
RCL 12
*
RCL 11
X^2

RCL 16
RCL 14
*
RCL 13
X^2

*
SQRT
/
RTN
LBL "STERROR"
XEQ "PMCC"
X^2
CHS
1
+
RCL 16
2

/
SQRT
RTN
LBL "STATS"
CLA
"GRADIENT="
AVIEW
XEQ "GRD"
STOP
CLA
"%ERROR="
AVIEW
XEQ "STERROR"
100
*
STOP
CLA
"ERROR="
AVIEW
XEQ "GRD"
STO 01
XEQ "STERROR"
RCL 01
*
STOP
CLA
"INTERCEPT="
AVIEW
XEQ "INTRCPT"
STOP
CLA
"%ERROR="
AVIEW
XEQ "STERROR"
100
*
STOP
CLA
"ERROR="
AVIEW
XEQ "INTRCPT"
STO 02
XEQ "STERROR"
RCL 02
*
STOP
RCL 02
RCL 01
CLA
"END OF PROG"
AVIEW
CLA
RTN
END
Tom L
Ducator meus nihil agit sine lagunculae leynidae accedunt
DM42 SN: 00025 (Beta)
SN: 00221 (Shipping)
Ducator meus nihil agit sine lagunculae leynidae accedunt
DM42 SN: 00025 (Beta)
SN: 00221 (Shipping)

 Posts: 498
 Joined: Mon Apr 24, 2017 6:38 pm
Re: Linear regression analysis program for DM41(L)
Ah, I see. The first code from the original post was typed by hand. The rest of the code was spat out by the decoder, so the labels are correctly given.toml_12953 wrote: ↑Thu Feb 01, 2018 11:03 amThanks for responding so quickly! I found the problem. The converter needs quotes around labels.
You're very welcome. I'm sorry they are so crudely coded.

 Posts: 498
 Joined: Mon Apr 24, 2017 6:38 pm
Re: Linear regression analysis program for DM41(L)
Right, after a bit of revising statistics and digging deeper, I've conjured up the following two equations to give standard deviations of slopes and intercepts for straight lines:
(Notation, m=gradient  stored in register 01, c=intercept  stored in register 02. Statistics registers have their default numbers)
(Credit to here for the statistics help: http://pages.mtu.edu/~fmorriso/cm3215/U ... resFit.pdf)
EDIT TO ADD: ARRRRGGG, about half way through the second line of the \sigma m function theres a rcl 16 that pushes a number over the top of the four stacks. Need to rewrite it to avoid stack overflow. Sorry for getting your hopes up (he says, shouting into the abyss because nobody really cares)
And RPNed it:
So now I have to throw it at the DM41 and see if it works. I've written a spreadsheet that uses a crude Monte Carlo method to estimate errors in gradients and interprets, and I'm really hoping this will agree. I'll let you know when I've had a chance to key it in.(Notation, m=gradient  stored in register 01, c=intercept  stored in register 02. Statistics registers have their default numbers)
(Credit to here for the statistics help: http://pages.mtu.edu/~fmorriso/cm3215/U ... resFit.pdf)
EDIT TO ADD: ARRRRGGG, about half way through the second line of the \sigma m function theres a rcl 16 that pushes a number over the top of the four stacks. Need to rewrite it to avoid stack overflow. Sorry for getting your hopes up (he says, shouting into the abyss because nobody really cares)
Last edited by keithdalby on Fri Feb 02, 2018 11:50 am, edited 1 time in total.