Mastering While Loops in Mathematica for Newton's Method Calculations

  • Context: Mathematica 
  • Thread starter Thread starter AxiomOfChoice
  • Start date Start date
  • Tags Tags
    Loops Mathematica
Click For Summary
SUMMARY

This discussion focuses on implementing a While loop in Mathematica to perform Newton's method calculations. The user aims to repeatedly compute a function f(r) and update the variable r until the absolute value of g(r) is less than a specified constant C. The provided code snippet demonstrates the structure of the While loop, including function definitions for f and g, and the loop's termination condition based on the value of g(r).

PREREQUISITES
  • Understanding of Newton's method in numerical analysis
  • Familiarity with Mathematica syntax and functions
  • Knowledge of function definitions in Mathematica
  • Basic concepts of loop structures in programming
NEXT STEPS
  • Explore advanced function manipulation in Mathematica
  • Learn about error handling in While loops in Mathematica
  • Investigate convergence criteria for iterative methods
  • Study optimization techniques for performance in Mathematica
USEFUL FOR

Mathematica users, mathematicians, and programmers interested in numerical methods and iterative algorithms, particularly those implementing Newton's method for root-finding problems.

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
3K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
9K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 52 ·
2
Replies
52
Views
13K