Grr, deleted line in Newton's Method program

1. Mar 6, 2005

Coldie

Hi, I've done something really stupid. I found a Newton's Method program on the internet and copied it to my TI-83 Plus. This program let you put the function in Y1 and its derivative in Y2. Then, you ran the program, and once you input your first guess, you would hit the ENTER key as many times as you wanted and it would continue to guess the next number. However, the program had no end, and so you'd have to hit the "ON" button to stop it, which resulted in the ERR:BREAK message. So, today, I was using the program, and when I hit the ON button to end it, I accidentally hit "Goto" instead of "Quit" in the message that came up, and, before I could stop myself, hit "CLEAR" to clear the window, but instead deleted a line of code.

Here's what remains of the program:
Prompt X
Lbl 0
X-Y1(X)/Y2(X)->X
(deleted line)
Goto 0

Can someone tell me what was in that line, please?

2. Mar 6, 2005

Jameson

That's probably where you want to add a segment of code to allow you to exit the program. I assume this because without such a segment, the program would go into an infinite loop.

---------
ClearHome
Output(1,1,X
Output(1,3, "Calculate another iteration? Y/N"
Prompt A
If A = "Y"
Then
Stop
Else
Goto 0

Last edited: Mar 6, 2005
3. Mar 6, 2005

Jameson

P.S. You can add another segment so you don't have to calculate the derivative into Y2. Add this in the beginning of your program.

-----
nDeriv(Y1,X,X) -> Y2

4. Mar 6, 2005

Coldie

It WAS an infinite loop, and I'd prefer that it be kept that way so that I don't have to go through one extra step before it calculates the next number for me. Thank you for going to such lengths in telling me how I could improve the program, but could you simply guess what the line of code that I deleted was? As I mentioned, it would calculate one, then wait for the ENTER key to be pressed before calculating another. You pressed the ON key to exit the program, and all I want is for it to work like that again.

Thanks for the second part, I think I will put that in.

Last edited: Mar 6, 2005
5. Mar 6, 2005

Jameson

It still is an infinite loop, I just allowed you to actually say if you wanted to stop without manually stopping the program with a "BREAK". I don't understand why you want it the other way.... that way honestly seems worse... but as you wish.

The missing code was
-------
Disp X
Pause

6. Mar 6, 2005

Coldie

Thank you very much for your help! I don't understand how I managed to delete two lines of code, but it works just like before!

If you could just tell me one more thing I'd be very grateful... how do you input things like Y1 and Y2? I seem to have forgotten where to get them as it was quite a long time that I wrote the program in.

7. Mar 6, 2005

Jameson

Goto VARS... it may be a 2nd funtion (i don't have my calculator right in front of me).
There you should find something called "Y-VARS". Click that and pick the one you want.

Jameson

8. Mar 6, 2005

Coldie

Y-VARS, Function. Thanks again for all the help!

9. Mar 6, 2005

Coldie

Um, sorry, but I'm getting ERR:DATA TYPE with that line that you told me to add...

10. Mar 6, 2005

Jameson

Which line?

11. Mar 6, 2005

Jameson

Make sure type in "nDeriv(Y2,X,X"

For Y2, goto the VARS button like we talked about....

12. Mar 6, 2005

Coldie

That's the line.

nDeriv(Y1,X,X)->Y2, with both Y's entered via the VARS button.

13. Mar 6, 2005

Jameson

are you hitting the "STORE" button... the one that looks like STO?

EDIT: It looks like "->" this, but on the calculator it looks like "STO ->"

14. Mar 6, 2005

Coldie

Sorry for the delay. Yes, I am, I know the syntax exactly and I've compared it with another program and it looks fine. Could you test it out on yours and tell me if it works?

15. Mar 7, 2005

Jameson

Did you have an equation into Y1 before you ran the program?

16. Mar 7, 2005

Justin Lazear

Using the nDeriv function will slow down the program a great deal. May as well just calculate the derivative yourself and program it in.

--J

17. Mar 8, 2005

Jameson

Yes it will, but if the derivative is not so easy to compute, you may just want to let the calculator do it. It really doesn't take that long, only when you have to graph it.

18. Mar 8, 2005

Justin Lazear

I looked at the 83's documentation, and the nDeriv() function uses the secant method to calculate the derivative at a point, i.e.

$$f'(a) = \frac{f(a + \epsilon) - f(a - \epsilon)}{2\epsilon}$$

where $\epsilon$ is defaulted to 10-3, but can be specified.

Because of this, using nDeriv to calculate the derivative makes the entire algorithm the secant method instead of the Newton-Raphson, so it will converge a bit more slowly (on the order of the golden ratio instead of quadratically). Additionally, the secant method is a little bit slower, since it needs to calculate two extra values before it can get the derivative (f(a ± epsilon)). It's a tradeoff between having to calculate the derivative and how quickly you want it to converge and a bit of processing time. If you're controlling the iterations manually, you'll never notice the processing time, so it's down to convergence.

If you're tested on the Newton-Raphson method and you use the nDeriv function, you'll most likely get the wrong answer. Then again, if it's just for your own purposes and you don't throw any particularly misbehaved functions at it, the secant (nDeriv) method will be more than sufficient.

Or you could just program in both.

--J

19. Mar 8, 2005

Jameson

Where did you find that the 83 uses a secant method? I've used it to calculate many derivatives that were exactly correct. Did you find this in the manual, and if so, what page?

20. Mar 8, 2005

Justin Lazear

Page 70 explains the nDeriv function.

There are times that the secant method will give the exact slope. For instance, if f(x) = x2, then the formula used to calculate slope reduces to 2x, regardless of the epsilon.

--J

Last edited by a moderator: May 1, 2017
21. Mar 9, 2005

Jameson

Wow, I didn't know that. Thanks for the link. So I guess if you want to use nDeriv for the program you should write the code:

nDeriv(Y1,X,X,1^-7) -> Y2

--------
Or something to that extent, where the approximation will be close. You could even enter a little code to ask whether you want to enter the derivative manually or not.

-------
Lbl 1
ClearHome
Disp "Enter derivative manually?"
Prompt A
If A = "YES"
Then
Goto Lbl 3
Else
Goto Lbl 2
Stop

Lbl 2
nDeriv(Y1,X,X,1^-7) ->Y2
Goto 4

Lbl 3
Disp "Enter derivative"
Prompt D
D -> Y2
Goto 4

Lbl 4

.............. Rest of code

Jameson