Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (anonymous):

write a program to find the cube root of a given number

OpenStudy (opcode):

Check your PM.

OpenStudy (e.mccormick):

In what language?

OpenStudy (anonymous):

What language?

OpenStudy (anonymous):

You can use the equation: X^(1/2) X being your variable

OpenStudy (e.mccormick):

Depends on if there are restrictions on the question. Some assignments want people to find powers or roots without using the actual power or root parts of the programming language.

OpenStudy (anonymous):

Well most languages have a built in root variable (sqrt)

OpenStudy (e.mccormick):

Yep. So it is hard to answer Eashwar because the full requirements, language, etc., are unknown.

OpenStudy (anonymous):

I use Newton's method (iterative) to find the solution to the problem x^3-S=0, which in this case reduces to the so-called Babylonian method (for the square root at least). This is an iterative process which will converge to the exact solution for an infinite number of iterations, but give a good approximation after only a few. First, we must define the error in the solution x. If you make an initial guess x0 for the solution, it contains an error such that: \[\sqrt[3]{S}=x+e\] For example, the cube root of 3 is 1.442250 (rounded). If we make an initial guess x0=1.5, then \[\sqrt[3]{3}=1.5+e\] such that e=0.05775. To obtain an expression for the error based on S and a certain approximation x: \[S=(x+e)^3\] \[S=x^3+3x^2e+3xe^2+e^3\] \[S-x^3=(3x^2+3xe+e^2)e\] \[e=\frac{S-x^3}{(3x^2+3xe+e^2)}\] \[e=\frac{S-x^3}{3x^2} \quad e<<x\] This last step gives an estimation of the error, assuming that e<<x. Then we enter an iterative process: -Make a guess for x0 -Based on the error in the solution of iteration k, update the solution for iteration k+1 by this error (this would be a good update, as the estimation of the error sort of tells you how much off your solution is): \[x_{k+1} = x_k+e = \frac{ 2x_k + \frac{S}{x_k^2} }{ 3 }\] -repeat the previous step until you have reached a sufficiently accurate solution (for example close to float precision). For the example of the cube root of 3 above: \[x_0 = 1.5\] \[x_1 =\frac{2x_0 + \frac{S}{x_0^2}}{3} = \frac{3+\frac{3}{2.25}}{3}=1.44444444\] \[x_2 =\frac{2x_1 + \frac{S}{x_1^2}}{3} = \frac{2.888888+\frac{3}{1.444444^2}}{3}=1.479289\] \[x_3 =\frac{2x_2 + \frac{S}{x_2^2}}{3} = \frac{2.958578+\frac{3}{1.479289^2}}{3}=1.443169\] \[x_4 =\frac{2x_3 + \frac{S}{x_3^2}}{3} = \frac{2.886338+\frac{3}{1.443169^2}}{3}=1.442250\]

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!