Array and Strings – Algebra – Problem #6
IIT Kharagpur CS10001 / CS11001 Programming and Data Structures Lab Question Archive
Problem Statement
In this question, we will approximate the square root of a real-valued function f(x) = 0 by a numerical approximation technique. The idea is that, a continuous and differentiable function can be approximated by a straight line tangent to it. Consider that we want to find the root of a continuous, differentiable function f(x), and we know that the root is near the point x = x0. Then the approximation of the root is computed as, x1 = x0 − f(x0) f 0(x0) , where f 0 is the derivative of f, and x0 is the first approximate value. This process is repeated as many times as necessary to get the desired accuracy. In the general case, if xn is the current estimated value, the next approximation xn+1 is given by, xn+1 = xn − f(xn) f 0(xn) . Your task is to consider a function f(x) = x 3 − 25, repeat the approximation process until the criteria |xn+1 − xn| < e is met (where e is the tolerance limit), or a maximum number of iterations are reached. Print the iteration number, x0, x1, error, and the final computed square root value. Example: Input: x0 = 5.0, e = 0.00005, maxIteration = 20 Output: Iteration x0 x1 Error 1 5.000000 3.666667 1.333333 2 3.666667 3.064279 0.602387 3 3.064279 2.930340 0.133939 4 2.930340 2.924031 0.006309 5 2.924031 2.924018 0.000014 The square root is 2.924018