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

While loops in Mathematica

  1. Nov 26, 2011 #1
    I am trying to do some Newton's method-type stuff in Mathematica.

    I want to build a While loop that does the following:

    - Take input r.
    - Compute some quantity f(r).
    - Update the value of r to f(r).

    - Repeat until |g(r)| < C for some fixed constant C.

    The third (boldfaced) step is the one I'm having trouble with. I don't know how to do this. Can someone help?
     
  2. jcsd
  3. Nov 26, 2011 #2
    Something like this?

    f[r_]:=r^2;
    g[r_]:=r-1/100;
    c=1/20;
    r=Input["What's r?"];(*Enter something between -1 and 1 *)
    While[Abs[g[r]]>c,
    r=f[r];
    Print[r]
    ]

    If that doesn't give you enough then we need to see what you have already done
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: While loops in Mathematica
  1. Mathematica looping (Replies: 3)

Loading...