Easy way to calculate function values, solve and integrate

Post here to share useful tips and tricks, to ask questions about using your DM42 or to report software-related problems
Post Reply
User avatar
deetee
Posts: 54
Joined: Mon Jul 22, 2019 8:01 am
Location: Vienna

Easy way to calculate function values, solve and integrate

Post by deetee »

Hello all!

Actually this is a HP42S topic too: Is there an easy way to implement a function (only once) and calculate function values respective solve and integrate this function?

For example I would like to implement the function Y=X*X-2.
Now I want to calculate the value of Y for X=5, solve Y=0 (x=1.41 and x=-1,41), solve Y=23 (x=5 and x=-5) and integrate Y (from 0 to 1.41, which equals -1.88).

When designing a program I always struggle between taking values from the stack (X=5), input values while in the solver (ie Y=23 and solve X) or integrator (limits from stack or while in integrator). Sometimes I have to define MVAR and sometimes I must not.

But I want to implement the function (program) once only. For my HP35s I can define the function in EQN-Editor (AOL) - which is very comfortable, but not an HP42S feature.

Thanks for every hint.
Regards
deetee
whuyse
Posts: 198
Joined: Thu Dec 21, 2017 1:23 pm

Re: Easy way to calculate function values, solve and integrate

Post by whuyse »

When you want a single definition, it will have to be SOLVE/INTEG style, so

Code: Select all

00 { 16-Byte Prgm }
01▸LBL "FX"
02 MVAR "X"
03 RCL "X"
04 X^2
05 2
06 -
07 END
Then, to evaluate the function, use VARMENU. You provide a value for X and press 'X' twice: once to store the value, once to evaluate. (this is the same for SOLVE BTW)

Cheers, Werner
41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE, DM15L
User avatar
deetee
Posts: 54
Joined: Mon Jul 22, 2019 8:01 am
Location: Vienna

Re: Easy way to calculate function values, solve and integrate

Post by deetee »

Hello Werner!

Thanks for your fast reply.
I didn't know VARMENU - and always wanted a solution with values from the stack. I added VARM to my customer menu.

Let me ask two additional questions:
* While solving I always get the positive solution (1.41). How can I initialize a negative value to start - so that the approximation ends with the negative solution (-1.44)?
* Is there a solution in the program to clear the variable X after (ie) a root was found? I want to automate pressing CLV and X per hand to "reduce" my bunch of variables.

Regards
deetee
whuyse
Posts: 198
Joined: Thu Dec 21, 2017 1:23 pm

Re: Easy way to calculate function values, solve and integrate

Post by whuyse »

- to get the negative root, provide two estimates spanning the solution, eg. -2 and -1.
So, -2 'X', -1 'X', 'X' -> -1.4142
- to clear the variable after solving, you'll need to program the solver instead of using it interactively.

Code: Select all

00 { 34-Byte Prgm }
01▸LBL "FX"
02 MVAR "X"
03 FC? 45
04 GTO 00
05 RCL "X"
06 X^2
07 2
08 -
09 RTN
10▸LBL 00
11 PGMSLV "FX"
12 1
13 LSTO "X" @ LSTO will make sure local variable "X" is deleted at the END
14 SOLVE "X"
15 END
Use XEQ "FX" instead of SOLVEing for FX this time though.

Cheers, Werner
41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE, DM15L
User avatar
deetee
Posts: 54
Joined: Mon Jul 22, 2019 8:01 am
Location: Vienna

Re: Easy way to calculate function values, solve and integrate

Post by deetee »

Hi Werner!

A program to engage the solver - that's quite tricky.

I found out that leaving the solver, STO an init value to X (ie -5) and solving again finds the negative root.

Thanks for this private lesson.

Regards
deetee
Thomas Okken
Posts: 1101
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Easy way to calculate function values, solve and integrate

Post by Thomas Okken »

The solver uses two starting values, so when using the SOLVE menu, you can tell it to look for a root between 1 and 2 while solving for X by typing 1 [X] 2 [X] [X].

If you only provide one starting guess, the most recent value is retained and used as the other, which is why you'll get the same root as before. To make it forget the old root, you have to provide two starting guesses; but it is OK to provide the same number twice, by typing, for example, 3 [X] [ENTER] [X] [X].
Thomas Okken
Posts: 1101
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Easy way to calculate function values, solve and integrate

Post by Thomas Okken »

Perhaps I should also mention that it works a bit differently when calling the solver from a program: in that case, the first starting value comes from the variable you're solving for, i.e. the value you get when RCLing that variable, and the second starting value is whatever is in the X register when SOLVE is called.

When using the SOLVER menu, SOLVE does not take the second starting guess from the X register; instead, it gets both starting guesses from the variable being solved for. This is made possible by a bit of trickery performed when you store a value using the SOLVER menu: it stores the new value, just like the regular STO would, but it also retains the previous value, in a hidden location, and that is what is used as the second starting guess.
User avatar
deetee
Posts: 54
Joined: Mon Jul 22, 2019 8:01 am
Location: Vienna

Re: Easy way to calculate function values, solve and integrate

Post by deetee »

Hello Thomas!

Thank's for enlightening me.

To enter two x-values (perhaps with ENTER) to define an interval is good to know.

Actually I decided to calculate all three exercises (function value, root, integral) comfortable by entering the formula as f(X)-Y=0:
* Calculate a function value with SOLVER, enter X and "solve" Y (makes more sense with formulae with many variables like Present Value calculations)
* Find a root with SOLVER, enter Y=0 and solve X (doing your interval-trick)
* Calculate an integral with 0 STO Y and then calling the INTEGRATOR, enter the lower and upper limit and INTEGRATE

Regards
deetee
Thomas Okken
Posts: 1101
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Easy way to calculate function values, solve and integrate

Post by Thomas Okken »

The chapters on the solver and integrator in the HP-42S Owner's Manual (chapters 12 and 13) and in the HP-42S Programming Examples and Techniques book (chapters 3 and 4) also worth a read. You'll find worked-out examples and useful tips there. And everything you read there applies to Free42 and the DM42 as well.

https://literature.hpcalc.org/community/hp42s-om-en.pdf
https://literature.hpcalc.org/community ... rog-en.pdf
Post Reply