HP Forums
WP 34S solve an integral - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: Not HP Calculators (/forum-7.html)
+--- Forum: Not quite HP Calculators - but related (/forum-8.html)
+--- Thread: WP 34S solve an integral (/thread-3700.html)

Pages: 1 2


WP 34S solve an integral - lrdheat - 04-23-2015 07:27 PM

I was wondering how to use solve for a problem such as, (integral x^2 from 0 to x) =10. (Answer is ~3.107)


RE: WP 34S solve an integral - Marcus von Cube - 04-23-2015 08:05 PM

(04-23-2015 07:53 PM)BarryMead Wrote:  You don't use solve, you use the Integral operator the g shifted 2.

I'm afraid You misread the original post. Lrdheat is asking for the upper bound so that the integral is 10. This can be done:

Code:
LBL 00
0
x<> Y
∫ 00 // should be 01
      // 10 - is missing. See next post
RTN
LBL 01

RTN

Now you can SLV 00 and should get your answer. Beware: I didn't test the code. It's just of the top of my head.

Edit: See comments in the code.


RE: WP 34S solve an integral - BarryMead - 04-23-2015 08:32 PM

(04-23-2015 08:05 PM)Marcus von Cube Wrote:  I didn't test the code. It's just of the top of my head.
That program was close to working, but this one does work.

Code:
LBL 00
0
x <> Y
∫ 01
# 010    //Entered with h CONST #  and 010
x <> Y
-
RTN
LBL 01

RTN

Exit programming mode and press 3 Enter 4 SLV 00


RE: WP 34S solve an integral - lrdheat - 04-23-2015 08:43 PM

Thanks, Barry,

It works!

What is the #010?


RE: WP 34S solve an integral - BarryMead - 04-23-2015 08:50 PM

(04-23-2015 08:43 PM)lrdheat Wrote:  Thanks, Barry,

It works!

What is the #010?
It is a constant 10 (The value sought for the integral end result) The numbers entered before solve are only
guesses for the solve search range. If you entered 2 and 4 SLV 00 it would still work.

If you want to rework the program to use the X register as target integral result you could store X in a register then
subtract the integral result from that register to make the solve function converge on zero relative to that stored value instead of 10.

I just hard coded the 10 in the program with a constant to make it easier to write the program.


Does that help?


RE: WP 34S solve an integral - BarryMead - 04-23-2015 09:05 PM

This version of the program uses label A as the start of the program and you put the
Target integral value into the X , the Upper solve guess bound in Y and the Lower solve guess Bound in Z and press A

Code:
LBL A
STO 00                  //Save target integral result desired
DROP                    //Lower stack to get Upper/Lower solve guess bounds
SLV 00
RTN
ERR 20                  //If solve fails to find a root show this error
LBL 00
0                       //Lower bound of integral (from zero to solve guess)
x <> Y
∫ 01
RCL 00                  //Value to solve for (to get upper integral bound)
x <> Y
-                       //Integral function returns zero when integral equals R00 target value
RTN
LBL 01

RTN

Then Press 0 Enter 5 Enter 10 A (Solves for X where the integral of x² from zero to X = 10) = 3.10723250595
Try 3 Enter 7 Enter 20 A (Solves for X where integral of x² from zero to X = 20) = 3.91486764117

I have found that even if you pick really bad guesses for the solve bounds, it usually still finds the right answer, but takes a little longer.


RE: WP 34S solve an integral - CR Haeger - 04-23-2015 10:28 PM

Thanks for this post ldheat and Marcus/Barry for your advice - I have been looking at something like this as well.

May I ask a few related questions for a WP34S that runs slower or is chewing up battery:

1. To limit the SLV routine precision/iterations, a RDP or RDP --> command in the routine being solved can be added near the end of the LBL 00 routine being "solved"?

2. To limit the integration precision, a SCI or SCI --> command can be added to the routine containing the integral (LBL 01)?

3. If I want to have a global "f(x)" routine versus a local one, could I change LBL 01 to say LBL 'F'? That way this global 'F' can be used as is or by other programs? If so, I assume the ∫ 01 needs to change to ∫ 'F' as well?

Thanks


RE: WP 34S solve an integral - BarryMead - 04-23-2015 10:34 PM

(04-23-2015 10:28 PM)CR Haeger Wrote:  Thanks for this post and Marcus/Barry for your advice - I have been looking at something like this as well.

May I ask a few related questions for a WP34S that runs slower or is chewing up battery:

1. To limit the SLV routine precision/iterations, a RDP or RDP --> command in the routine being solved can be added near the end of the LBL 00 routine being "solved"?

2. To limit the integration precision, a SCI or SCI --> command can be added to the routine containing the integral (LBL 01)?

3. If I want to have a global "f(x)" routine versus a local one, could I change LBL 01 to say LBL 'F'? That way this global 'F' can be used as is or by other programs?

Thanks

Yes to all three questions. The WP-34s is one of the most powerful programmable calculators out there.
You can do almost anything you can imagine with it.

A couple of suggestions regarding battery life.
1. Use the emulator on your computer (It uses only wall power and runs much faster than the real calculator)
2. If you have a calculator with Harold Pott's micro USB board installed, attach the USB cable to a computer (or a 5V USB adapter), and you have NO BATTERY DRAINAGE. (Nice for running those long battery hungry programs).


RE: WP 34S solve an integral - Dieter - 04-23-2015 10:46 PM

(04-23-2015 10:34 PM)BarryMead Wrote:  Yes to all three questions. The WP-34s is the most powerful programmable calculator I have ever used.

All this could already be done with the very first HP with "Integrate" – the 34C from 1979. However, not all later HPs allow nested Solve/Integrate. For instance the 35s doesn't.

@"lrdheat": please note that the result returned by "Integrate" may vary with the display setting. Some days ago I answered an older post where you asked a similar question. If you haven't noticed yet: look here.

(04-23-2015 10:34 PM)BarryMead Wrote:  You can do almost anything you can imagine with it.

Well, there still is some work to do. Recently I have been looking at the Poisson and Binomial quantile functions.

Dieter


RE: WP 34S solve an integral - lrdheat - 04-24-2015 02:11 AM

Thanks Barry, Dieter, and CR.

Nice work!


RE: WP 34S solve an integral - Thomas_Sch - 04-24-2015 06:19 AM

(04-24-2015 02:11 AM)lrdheat Wrote:  Thanks Barry, Dieter, and CR.

Nice work!
Same from me, very helpful!


RE: WP 34S solve an integral - Marcus von Cube - 04-24-2015 08:22 AM

(04-23-2015 10:46 PM)Dieter Wrote:  
(04-23-2015 10:34 PM)BarryMead Wrote:  Yes to all three questions. The WP-34s is the most powerful programmable calculator I have ever used.

All this could already be done with the very first HP with "Integrate" – the 34C from 1979. However, not all later HPs allow nested Solve/Integrate. For instance the 35s doesn't.

The 34S has a feature the 34C lacks: It can nest solve and integrate (and sum and product) to any depth as far as memory allows. We're using local registers in the XROM code that implements these functions.


RE: WP 34S solve an integral - walter b - 04-24-2015 08:57 AM

FYI, "RDP or RDP-->" are the same command, just with direct or indirect addressing. Same applies to "SCI or SCI-->".

d:-)


RE: WP 34S solve an integral - Dieter - 04-24-2015 12:16 PM

(04-23-2015 10:28 PM)CR Haeger Wrote:  1. To limit the SLV routine precision/iterations, a RDP or RDP --> command in the routine being solved can be added near the end of the LBL 00 routine being "solved"?

I would recommend a solution like this:
Code:
...
RDP 05    // round to e.g. 5 decimal places
x≠0?      // if result does not round to zero
x<> L     // recall the original unrounded value
RTN

This way the solver always gets the exact function result, and only values below the (here) 5–digit threshold are rounded to zero so that the solver quits.

Dieter


RE: WP 34S solve an integral - Dieter - 04-24-2015 12:34 PM

(04-23-2015 09:05 PM)BarryMead Wrote:  
Code:
...
LBL 00
0                       //Lower bound of integral (from zero to solve guess)
x <> Y
∫ 01
RCL 00                  //Value to solve for (to get upper integral bound)
x <> Y
-                       //Integral function returns zero when integral equals R00 target value
RTN
...

Of course it doesn't matter whether you solve ∫ – 10 = 0 or 10 – ∫ = 0, so the second x<>y is not required, and a simple RCL–00 will do as well.
Or, even shorter:

Code:
LBL 00
 0
∫ 01      // ∫ from x to 0 = –∫ from 0 to x 
RCL+ 00   // 10 – ∫
RTN

Sorry for the nitpicking. ;-)

(04-23-2015 09:05 PM)BarryMead Wrote:  Then Press 0 Enter 5 Enter 10 A (Solves for X where the integral of x² from zero to X = 10) = 3.10723250595
Try 3 Enter 7 Enter 20 A (Solves for X where integral of x² from zero to X = 20) = 3.91486764117

FTR: the exact solutions are the cube roots of 3·10 resp. 3·20.

Dieter


RE: WP 34S solve an integral - CR Haeger - 04-24-2015 01:01 PM

(04-24-2015 12:16 PM)Dieter Wrote:  I would recommend a solution like this:
Code:
...
RDP 05    // round to e.g. 5 decimal places
x≠0?      // if result does not round to zero
x<> L     // recall the original unrounded value
RTN

This way the solver always gets the exact function result, and only values below the (here) 5–digit threshold are rounded to zero so that the solver quits.

Dieter

Thanks Dieter!

If I understand correctly, the x≠0? and x<> L steps ensure that the latest exact (versus rounded) SLV guess is passed back to the SLV routine?


RE: WP 34S solve an integral - Dieter - 04-24-2015 01:09 PM

(04-24-2015 01:01 PM)CR Haeger Wrote:  If I understand correctly, the x≠0? and x<> L steps ensure that the latest exact (versus rounded) SLV guess is passed back to the SLV routine?

These two steps make sure that usually the exact function result is returned to the SLV routine.
Only if that value rounds to zero, this zero is passed to SLV so that it will quit.

In other words:

IF Round(fx, 5) = 0
    THEN return 0
    ELSE return fx
END IF

Dieter


RE: WP 34S solve an integral - CR Haeger - 04-24-2015 01:20 PM

Right, f(x) not "guess"...

thanks again.


RE: WP 34S solve an integral - CR Haeger - 04-24-2015 01:24 PM

(04-24-2015 08:57 AM)walter b Wrote:  FYI, "RDP or RDP-->" are the same command, just with direct or indirect addressing. Same applies to "SCI or SCI-->".

d:-)

Thank you. Yes, I thought the indirect addressing might be handy for adjusting SCI or RDP values from outside the program - perhaps even during its execution. Cool feature.


RE: WP 34S solve an integral - BarryMead - 04-24-2015 02:46 PM

(04-24-2015 12:34 PM)Dieter Wrote:  
Code:
LBL A
STO 00      //Save target integral result desired
DROP        //Lower stack to get Upper/Lower solve guess bounds
SLV 00
RTN
ERR 20      //If solve fails to find a root show this error
LBL 00
 0
∫ 01      // ∫ from x to 0 = –∫ from 0 to x 
RCL+ 00   // 10 – ∫
RTN
LBL 01

RTN
Very cleaver optimization. I knew that my original program was clumsy, and non-optimal. I did that on purpose to make it as clear and obvious as possible how to turn the original idea into a program. Using reversed integral bounds (to make the integral negative), and adding a positive target value is equivalent to subtracting a target, and the ability of solve to converge regardless of the sign of the error also can make the program more compact. Congrats on how small you made the program.