## DM42/Free42 - which algorithm used in SOLVE ?

Discussion around the Swiss Micros DM42 calculator.
whuyse
Posts: 158
Joined: Thu Dec 21, 2017 1:23 pm

### Re: DM42/Free42 - which algorithm used in SOLVE ?

As Thomas already pointed out, SOLVEing an integral means (in this case) that the integral will be progressively closer to 0, and you'll need more and more evaluations to reach the desired relative accuracy.
I wonder how the 42S bypasses this, but one reason is that its precision is only 12 digits, not 34, and that precision is reached with 255 evaluations of JNX, a reasonable amount. To get close to 34 digits precision (which is effectively what you're asking), hundreds of thousands of evaluations need to be made.
To heed the ACC factor, we may add a constant to the integrand and subtract it again after integration:
(I haven't seen the programs in the .raw file, these are my implementations:)

Code: Select all

00 { 23-Byte Prgm }
01▸LBL "JNX" @ integrand COS(N*A - X*SIN(A))
02 RCL "A"
03 RCL× "N"
04 LASTX
05 SIN
06 RCL× "X"
07 -
08 COS
09 1
10 +
11 END

Code: Select all

00 { 48-Byte Prgm }
01▸LBL "BESSEL" @ Bessel function Jn(x) = int(JNX,0,PI)/PI
02 MVAR "N"
03 MVAR "X"
04 MVAR "ACC"
05 CLX
06 STO "LLIM"
07 PI
08 STO "ULIM"
09 PGMINT "JNX"
10 INTEG "A"
11 PI
12 ÷
13 1
14 -
15 END
Without the constant, and ACC=1E-4, JNX is sampled 4218730 times.
WIth the constant, it is 1134 times. Granted, the result is only accurate to about 7-8 digits now.
So, I don't think there's anything wrong with either SOLVE or INTEG.

Cheers, Werner
Last edited by whuyse on Wed Sep 08, 2021 8:23 am, edited 2 times in total.
42S #3249S01123
DM42 #00345
DM41X #01215
whuyse
Posts: 158
Joined: Thu Dec 21, 2017 1:23 pm

### Re: DM42/Free42 - which algorithm used in SOLVE ?

Strangely, when I rewrite the routines to have but a single global label, as follows:

Code: Select all

00 { 61-Byte Prgm }
01▸LBL "JNX" @ Bessel function Jn(x) = int(COS(N*A - X*SIN(A)),0,PI)/PI
02 MVAR "N"
03 MVAR "X"
04 MVAR "ACC"
05 FC? 46 @ not integrating
06 GTO 00
07 RCL "A"
08 RCL× "N"
09 LASTX
10 SIN
11 RCL× "X"
12 -
13 COS
14 RTN
15▸LBL 00
16 CLX
17 STO "LLIM"
18 PI
19 STO "ULIM"
20 PGMINT "JNX"
21 INTEG "A"
22 PI
23 ÷
24 END
It now takes 4 million evaluations of the integrand as well for ACC=1E-4. Strange? It should be exactly the same.
UPDATE: not so strange, I made an error before. The total amount of integrand evaluations is 4218730
BTW since ACC is a relative factor, and you're looking for a zero value, 1E-2 will work as well. Only a million evaluations this time
Werner
Last edited by whuyse on Wed Sep 08, 2021 8:12 am, edited 1 time in total.
42S #3249S01123
DM42 #00345
DM41X #01215
OlidaBel
Posts: 44
Joined: Thu Mar 11, 2021 8:52 am
Location: Belgium

### Re: DM42/Free42 - which algorithm used in SOLVE ?

whuyse wrote:
Tue Sep 07, 2021 10:32 am
Without the constant, and ACC=1E-4, JNX is sampled 524287 times (strange that it took >4e6 in your case, Thomas)
WIth the constant, it is 63 times. Granted, the result is only accurate to about 7-8 digits now.
So, I don't think there's anything wrong with either SOLVE or INTEG.

Cheers, Werner
Hi Werner,
Glad you discovered this as well.
I did not explain this here, but when testing before I also discovered the same behaviour.
I had followed the example from the "programming examples and techniques" manual (p.131) :
Integral=constant
express this as (equation-constant=0)
solve for this expression.

Trying to match a certain value for the integral (f.e. 1) gave good results.
Trying to match zero... panic
The added and substracted constants "absorb" the tiny values of the integral.
I simplified it after some trials, not good
Completely agree with you.
so, thank you.
(the very long precision on DM42 does not help with zero values detection)
---
Olivier
My old 48GX. Prime G2, 50G. DM15L & DM42. I was fan of 11C, 15C, 28S and some hours on 42S
Thomas Okken
Posts: 949
Joined: Tue May 02, 2017 5:48 pm
Location: United States → Netherlands
Contact:

### Re: DM42/Free42 - which algorithm used in SOLVE ?

whuyse wrote:
Tue Sep 07, 2021 11:47 am
Strangely, when I rewrite the routines to have but a single global label, as follows:

[...]

It now takes 4 million evaluations of the integrand as well for ACC=1E-4. Strange? It should be exactly the same.
Are you sure the label is the only difference? I don't see how that could affect anything about the result.
Thomas Okken
Posts: 949
Joined: Tue May 02, 2017 5:48 pm
Location: United States → Netherlands
Contact:

### Re: DM42/Free42 - which algorithm used in SOLVE ?

N.B. I don't know how to check Werner's test cases. The code, as it is, is not valid (PGMINT "Jn" but LBL "JNX") and it is not clear to me what the parameter N is supposed to be set to.
whuyse
Posts: 158
Joined: Thu Dec 21, 2017 1:23 pm

### Re: DM42/Free42 - which algorithm used in SOLVE ?

The first is of course it should be PGMINT "JNX" (and actually, JNX doesn't need MVAR statements)
The second is that I reset the count before each integration, so the 524287 was only the count of the last integral.
And N=0 here of course Thomas; JNX, and we're looking for a zero of J0 between 2.4 and 2.5.
Corrected it all in the posts above.

Cheers, Werner
42S #3249S01123
DM42 #00345
DM41X #01215
OlidaBel
Posts: 44
Joined: Thu Mar 11, 2021 8:52 am
Location: Belgium

### Re: DM42/Free42 - which algorithm used in SOLVE ?

whuyse wrote:
Wed Sep 08, 2021 8:17 am