Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Grr, deleted line in Newton's Method program

  1. Mar 6, 2005 #1
    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
    (deleted line)
    Goto 0

    Can someone tell me what was in that line, please?
  2. jcsd
  3. Mar 6, 2005 #2
    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.

    I'd add something like this:

    Output(1,3, "Calculate another iteration? Y/N"
    Prompt A
    If A = "Y"
    Goto 0
    Last edited: Mar 6, 2005
  4. Mar 6, 2005 #3
    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
  5. Mar 6, 2005 #4
    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
  6. Mar 6, 2005 #5
    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
  7. Mar 6, 2005 #6
    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.
  8. Mar 6, 2005 #7
    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.

  9. Mar 6, 2005 #8
    Y-VARS, Function. Thanks again for all the help!
  10. Mar 6, 2005 #9
    Um, sorry, but I'm getting ERR:DATA TYPE with that line that you told me to add...
  11. Mar 6, 2005 #10
    Which line?
  12. Mar 6, 2005 #11
    Make sure type in "nDeriv(Y2,X,X"

    For Y2, goto the VARS button like we talked about....
  13. Mar 6, 2005 #12
    That's the line.

    nDeriv(Y1,X,X)->Y2, with both Y's entered via the VARS button.
  14. Mar 6, 2005 #13
    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 ->"
  15. Mar 6, 2005 #14
    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?
  16. Mar 7, 2005 #15
    Did you have an equation into Y1 before you ran the program?
  17. Mar 7, 2005 #16
    Using the nDeriv function will slow down the program a great deal. May as well just calculate the derivative yourself and program it in.

  18. Mar 8, 2005 #17
    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.
  19. Mar 8, 2005 #18
    I looked at the 83's documentation, and the nDeriv() function uses the secant method to calculate the derivative at a point, i.e.

    [tex]f'(a) = \frac{f(a + \epsilon) - f(a - \epsilon)}{2\epsilon}[/tex]

    where [itex]\epsilon[/itex] 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.

  20. Mar 8, 2005 #19
    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?
  21. Mar 8, 2005 #20

    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.

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Grr, deleted line in Newton's Method program
  1. Newtons method (Replies: 5)

  2. Newton's method (Replies: 4)

  3. Newtons method. (Replies: 2)