Mastering While Loops in Mathematica for Newton's Method Calculations

In summary, the conversation discusses the use of Newton's method in Mathematica to build a While loop that takes input r, computes a quantity f(r), updates the value of r to f(r), and repeats until the absolute value of g(r) is less than a fixed constant c. The speaker is having trouble with the third step and is looking for assistance and feedback on their current code.
  • #1
AxiomOfChoice
533
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
  • #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
 

1. What is Newton's Method and why is it important in Mathematica?

Newton's Method is an iterative numerical method used to find the roots of a function. It is important in Mathematica because it allows for efficient and accurate calculations of roots, which are essential in many scientific and mathematical applications.

2. How do while loops work in Mathematica for Newton's Method calculations?

In Mathematica, while loops are implemented using the While function. The loop will continue to iterate as long as the specified condition is true, and the loop variable is updated with each iteration. This allows for repeated calculations until a desired level of accuracy is achieved.

3. Can while loops be used for other types of calculations in Mathematica?

Yes, while loops can be used for a variety of calculations in Mathematica. They are commonly used for iterative methods, such as Newton's Method, but can also be used for other tasks such as data processing and optimization algorithms.

4. How can I ensure that while loops in Mathematica do not run indefinitely?

To prevent while loops from running indefinitely, it is important to include a condition that will eventually evaluate to false. This condition can be based on a specific number of iterations, a desired level of accuracy, or any other criteria that is relevant to the specific calculation.

5. Are there any alternatives to while loops for Newton's Method calculations in Mathematica?

Yes, there are alternative methods for implementing iterations in Mathematica, such as the NestWhile function or the Do loop. However, while loops are often the most straightforward and efficient choice for Newton's Method calculations.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
6K
  • Advanced Physics Homework Help
Replies
1
Views
692
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • Quantum Physics
Replies
1
Views
580
Back
Top