Program for Approximating nth root of a Number

In summary, the conversation discusses a programming exercise to approximate the nth root of a number using Newton's method to six decimal places. The problem also states to terminate after 100 trials if it fails to converge. "Converge" means that the difference between two approximations can be made as small as desired. The program should terminate when either the loop has been executed 100 times or the difference between the "true" answer and the approximation is less than 0.000001. To express the second condition, it is suggested to set the condition as |approximation - previous approximation| < 0.0000001. Another way to express the condition is through estimating the error in an iterate, which can be done by raising the
  • #1
annie122
51
0
I have a question for a programming exercise I'm working on for C.

The problem is to "Write a program that uses Newton's method to approximate the nth root of a number to six decimal places." The problem also said to terminate after 100 trials if it failed to converge.

Q1. What does "converge" mean?
Does it mean the difference between two approximation can be made as small as I like?

Q2. On what condition should the program terminate?
There are two such conditions: 1) if the loop has been executed 100 times, 2) the difference between the "true" answer and the approximation is less than 0.000001.
I know how to set 1), but how should I express 2)?
Right now, I am setting the condition as
|approximation - root| < 0.00001,
but I feel it's kind of cheating, because I'm not supposed to know the real answer if I'm making approximations.
Are there any other any ways to express the condition, especially one involving the function f(x) = x^n - c (x is the nth root of c)?
 
Mathematics news on Phys.org
  • #2
Re: question on program for approximating nth root of a number

Hi Yuuki! :)

Yuuki said:
I have a question for a programming exercise I'm working on for C.

The problem is to "Write a program that uses Newton's method to approximate the nth root of a number to six decimal places." The problem also said to terminate after 100 trials if it failed to converge.

Q1. What does "converge" mean?
Does it mean the difference between two approximation can be made as small as I like

Yes. Basically.
More specifically, that you can get as close to the nth root as you want by just taking enough trials.

Q2. On what condition should the program terminate?
There are two such conditions: 1) if the loop has been executed 100 times, 2) the difference between the "true" answer and the approximation is less than 0.000001.
I know how to set 1), but how should I express 2)?
Right now, I am setting the condition as
|approximation - root| < 0.00001,
but I feel it's kind of cheating, because I'm not supposed to know the real answer if I'm making approximations.
Are there any other any ways to express the condition, especially one involving the function f(x) = x^n - c (x is the nth root of c)?

Exactly. You're not supposed to use the real root.
But what you can do is setting the condition as for instance
$$|\text{approximation} - \text{previous approximation}| < 0.0000001$$
If you achieve that, it is unlikely that the first 6 decimal digits will change in more iterations.
 
  • #3
Re: question on program for approximating nth root of a number

Thanks. :)

I set a new variable root0 to store the previous approximation, and set the condition as you said.
It worked beautifully.
 
  • #4
It is possible to estimate the error in an iterate directly (assuming it small anyway).

Let \(\displaystyle x\) be the 6-th root of \(\displaystyle k\) and \(\displaystyle x_n\) an estimate of \(\displaystyle x\) with error \(\displaystyle \varepsilon_n\) such that:

\(\displaystyle x=x_n+\varepsilon_n\)

Then raising this to the 6-th power gives:

\(\displaystyle x^6=x_n^6 + 6 \varepsilon_n x_n^5 + O(\varepsilon_n^2)\)

Now ignoring second and higher order terms in \(\displaystyle \varepsilon\) and rearranging we get:

\(\displaystyle \varepsilon_n=\frac{x_n^6-x^6}{6x_n^5}=\frac{x_n^6-k}{6x_n^5}\)

OK let's look at an example: Take \(\displaystyle k=66\), and \(\displaystyle x_n=2\), so \(\displaystyle x_n^6=64\), then

\(\displaystyle \varepsilon_n=\frac{66-64}{6\times32}\approx 0.01042\)

which compares nicely with \(\displaystyle 66^{1/6}=2.01028...\)

The above is very similar (for similar read identical) to computing the next iterate and taking the difference of the iterates as an estimate of the error in the first.
 
Last edited:
  • #5
zzephod said:
It is possible to estimate the error in an iterate directly (assuming it small anyway).

It is also possible to specify an upper boundary for the remaining error in a specific iteration (in this specific case).

First off, after the first (positive) iteration, all iterations are guaranteed to be above the root.
The remaining error in those iterations is guaranteed to be less than the change in the approximation.
 

FAQ: Program for Approximating nth root of a Number

What is a "Program for Approximating nth root of a Number"?

A "Program for Approximating nth root of a Number" is a computer program that calculates the approximate value of the nth root of a given number. It uses a mathematical algorithm to estimate the value of the root, rather than providing an exact answer.

How does the program work?

The program uses the Newton's method or the bisection method to estimate the nth root of a number. It involves repeatedly refining an initial guess until it is accurate enough. The algorithm used may vary, but the basic concept is the same.

What is the purpose of this program?

The program is designed to provide an approximate value of the nth root of a number. This can be useful in situations where an exact answer is not necessary, or when calculating the exact root is too time-consuming.

How accurate is the approximation?

The accuracy of the approximation depends on the algorithm used and the number of iterations performed. Generally, the more iterations, the more accurate the result. However, the approximation will never be as accurate as the exact value of the root.

What are some limitations of this program?

One limitation of this program is that it can only approximate the nth root of a number, rather than providing an exact answer. Additionally, the accuracy of the approximation may vary depending on the number and the algorithm used. It is also important to note that this program may not work for complex numbers or negative numbers.

Similar threads

Replies
1
Views
801
Replies
2
Views
1K
Replies
18
Views
4K
Replies
2
Views
3K
Replies
16
Views
2K
Replies
11
Views
3K
Replies
1
Views
2K
Back
Top