Mathematica Mastering While Loops in Mathematica for Newton's Method Calculations

  • Thread starter Thread starter AxiomOfChoice
  • Start date Start date
  • Tags Tags
    Loops Mathematica
Click For Summary
The discussion centers on implementing a While loop in Mathematica to perform iterations based on Newton's method. The goal is to take an initial input value r, compute a function f(r), and update r to f(r) repeatedly until the absolute value of another function g(r) is less than a specified constant C. The user is encountering difficulties specifically with updating the value of r in the loop. An example code snippet is provided, where f[r] is defined as r^2 and g[r] as r - 1/100, with a constant c set to 1/20. The user seeks assistance to ensure the loop correctly updates r and continues until the stopping condition is met. Further clarification on the user's existing code may be required for additional support.
AxiomOfChoice
Messages
531
Reaction score
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?
 
Physics news on Phys.org
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
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
5K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
9K
  • · Replies 52 ·
2
Replies
52
Views
12K
  • · Replies 1 ·
Replies
1
Views
2K