I meant to say that I'm solving f(x)-f(x-.001)=0 with my methodology...is there a better way?
HP 15C LE extremums
|
|
« Next Oldest | Next Newest »
|
▼
05-19-2013, 08:25 PM
▼
05-19-2013, 08:30 PM
I thought my initial post posted (it didn't). Is this the best way to find extremums? This finds "x". I then need to have the original function available to plug the "x" in order to find f(x) at the extremum. ▼
05-19-2013, 10:14 PM
There are better ways to estimate a functions derivative at a point. The WP 34S uses a four, six or ten point method e.g. At the very least use (f(x+h) - f(x-h)) / (2h). - Pauli
Edited: 21 May 2013, 7:51 a.m. after one or more responses were posted
05-20-2013, 10:12 AM
Examplef(x) = sin(x)/x Find the extremum between and 2.
The program for the functionLBL 0
The program to calculate the derivativeLBL 1
SolutionWe use a small value h in register 0: 0.00001
Kind regards AddendumUsing calculus we end up with: x = tan(x). We can solve that with a simple fixed-point iteration. Just fill the stack with , enter 4 as starting guess and repeat the following steps: TAN-1This list is the result: 4.00000 Edited: 20 May 2013, 10:40 a.m. ▼
05-20-2013, 01:43 PM
There is a great little article (very readable) on the complex-step method of calculating numerical derivatives, entitled "Complex-Step Derivatives: How Did I Miss This?" by Michael Sherman (Stanford University): http://biomedicalcomputationreview.org/2/3/8.pdf Edited: 20 May 2013, 1:45 p.m.
05-20-2013, 02:41 PM
That is neat! Your example worked great. Next post with article nice as well. I'm not getting the syntax for other examples, however. How would I enter as my LBL 0 f(x)= x^2 - 3*x -10 An extremum should be at x=1.5. f(x)=-12.25 Thanks! ▼
05-20-2013, 03:00 PM
f(x)= x^2 - 3*x -10 LBL 0
Cheers If you're missing the fact that the stack is filled with x you could use the following: LBL 1This makes using Horner's scheme to evaluate polynomials somewhat easier: LBL 0 Edited: 20 May 2013, 3:10 p.m. ▼
05-20-2013, 03:44 PM
Hi Thomas, When I enter your steps, I get 0 for solve 1 (and solve 0 incorrectly gives a root of 4 for intial guesses of 0,12 instead of 5). If I go: LBL 0 2 y^x x exchange y 3 * -
1 - RTN
I get the correct root of 5 for solve 0 (solve f(x)=0) LBL 1 RCL 0 I GSB 0 Re exchange Im RTN where .00001 is stored in "0" Can you reproduce my experience? How do I get the correct results that you came up with? ▼
05-20-2013, 04:01 PM
still get 0 for solve 1 when I add RCL 0 / after Re exchange Im
05-20-2013, 04:04 PM
First, please do not use 2 y^x when you want to get x^2. There is a dedicated x^2 key on virtually any scientific HP, which works much faster, preserves x in LastX and is even potentially more accurate. Your approach assumes that x fills the whole stack so that it can be recalled with x<>y in your LBL 0 routine. However, in LBL 1 it's this routine that provides the x-value, so you will have to enter a few ENTER commands that fill the stack before GSB 0 is called. Also the LBL 1 routine lacks the final division by h. Which usually (but not always) does not matter much if you want to solve for f'(x)=0, but I think you should add that division there. With these adjustments everything works fine. All this can be done shorter and faster if the function is coded the way Thomas and I proposed. Also see my detailled post below.
0 ENTER 12 f SOLVE 0 => 5,0000 f(5) = 0 Dieter ▼
05-20-2013, 04:22 PM
Blind as a bat re x^2 key. Last question...(and thanks for the last x discussion)...how do I implement that in this example...the HP 15C manual has a solve example: f(x)=5000*(1-e^(-x/20))-200*x using 0,12 a local max should occur at about x=4.4629,f(x)=107.4258 ▼
05-20-2013, 06:04 PM
I've modified the expression a little: LBL 0Be careful when using [CHS]: it changes only the real part of a complex number! Make sure to use it just with real constants as in -20. It took me some time to figure that out. 4 ENTER 5
Cheers
Edited: 20 May 2013, 6:29 p.m.
05-21-2013, 07:26 AM
Quote:You do it the same way for each and every arbitrary function you come across. Just define your function at LBL 0 and leave the rest as it is (be sure to add those three ENTERs in the LBL 1 routine). So simply rewrite your new function at LBL 0 - that's all. If you have problems implementing simple functions in 15C code, please take a closer look at the basic chapters on programming in the 15C manual.
A literal implementation of your last case may look like this: LBL 0Please note the already mentioned special behaviour of CHS with complex numbers, which is crucial here. Therefore CHS was only used to enter a negative real constant -20, but not for changing the sign of a computed result. 0 ENTER 12Dieter ▼
05-21-2013, 08:09 AM
Quote:
And now I suddenly understand this: Quote: Quote:This doesn't happen with the HP-42S though. And if somebody is still in doubt why it would have been a good idea to provide complex variants for all functions of the HP-35S: that's why. You can still use this neat trick with polynomials but it fails with trigonometric functions or a simple square root.
Cheers
Edited: 21 May 2013, 8:41 a.m. ▼
05-21-2013, 11:01 AM
A most interesting discussion. Excellent! Amazing what happens when one (me) isn't afraid to uncover some lack of knowledge! ▼
05-21-2013, 12:06 PM
I'm dangerous...I get an error 8 when I try solve 1 on x^(3/5) * (4-x) Any way around this? Thanks again! ▼
05-21-2013, 12:29 PM
LBL 0
1 ENTER 2
GSB 0 Works for me.
Cheers ▼
05-21-2013, 01:58 PM
For what it's worth you can shave a step by replacing X<>Y 4 X<>Y with 4 RollUp.
05-21-2013, 01:58 PM
Quote:Then you must be doing something wrong. LBL 0Where's the problem? Dieter ▼
05-21-2013, 04:05 PM
I was inefficient like a novice...but don't know why it works as subroutine 0, but generates error 8 on solve 1: LBL 1 enter enter enter CHS 4 + x exchange y 3 enter 5 / y^x * RTN
▼
05-21-2013, 04:42 PM
You forgot the special behaviour of the CHS function for complex values, as already discussed in this thread. So do not use CHS to change the sign of an already computed result in X, since it will change only the sign of the real part of X, but not that of the imaginary part!
That's why your CHS after the three ENTERs is causing the problem here. Your implementation CHSwill work fine for reals (as in GSB 0), but not for complex numbers (as used at LBL 1). You have to use 4here, and everything will work fine. Both for real and for complex values. Dieter
Edited: 21 May 2013, 4:44 p.m.
05-21-2013, 07:53 PM
Homework1. Single-step through GSB 1 both with a value a little smaller and a little bigger than 1.5, (e.g. 1.49 and 1.51). Use the (i)-key to monitor the imaginary part of the intermediate results. Is there a change in the sign of the derivatives? How is that related to the Error 8 you encountered?
2. Repeat the steps above with Kiyoshi's solution: LBL 0 3. Edit your post. Insert [pre] before and [/pre] after your listing. Remove the additional line-feed between the steps.
Kind regards PS: Bonus points if you succeed in writing yx instead of y^x.
05-21-2013, 02:22 PM
Quote: Just wondered what your next example will be. Maybe something like Valentin's extremely flat maximum that is difficult to calculate numerically? I faintly remember a cos was involved but I might be wrong.
Ok, found it in message #52 of
Quote:
LBL 0
1 ENTER 2
Quote:
Cheers
PS: Marcus von Cube posted in message #67: Quote: Edited: 21 May 2013, 8:37 p.m.
05-20-2013, 04:14 PM
If you expect that the stack is filled with x, use this program instead: LBL 1
HTH PS: You can use the [pre]-tag for listings: just click on the button [Preformatted] and REPLACE THIS TEXT WITH YOUR LISTING.
05-20-2013, 03:26 PM
It's done exactly the same way as in the previous example.
Edited: 20 May 2013, 3:33 p.m. |