While loops in Mathematica

  • Mathematica
  • Thread starter AxiomOfChoice
  • Start date
  • #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?
 

Answers and Replies

  • #2
1,069
29
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
 

Related Threads on While loops in Mathematica

Replies
12
Views
2K
Replies
12
Views
5K
Replies
1
Views
3K
  • Last Post
Replies
3
Views
2K
Replies
5
Views
13K
Replies
3
Views
24K
Replies
3
Views
2K
Replies
8
Views
7K
Replies
2
Views
4K
Replies
25
Views
8K
Top