Sunday 20 March 2016

Solve the Newton-Raphson method equation using C

Recall that the equation of a straight line is given by the equation
y = mx +n  (1)

where m is called the slope of the line. (This means that all points (x,y) on the line satisfy the equation above.)
If we know the slope m and one point (x0,y0) on the line, equation (1) becomes 
y−y0 = m(x−x0)  (2)

Idea behind Newton’s method 
Assume we need to find a root of the equation f(x) = 0. Consider the graph of the function f(x) and an initial estimate of the root, x0. To improve this estimate, take the tangent to the graph of f(x) through the point (x0, f(x0) and let x1 be the point where this line crosses the horizontal axis.

According to eq. (2) above, this point is given by
x1 = x0−(f(x0)/f′(x0))

where f ′(x0) is the derivative of f at x0. Then take x1 as the next approximation and continue the procedure. The general iteration will be given by

xn+1 = xn− (f(xn)/f′(xn))  and so on.

Error analysis
Let α be the root of f(x) = 0 we are trying to approximate. Then, Taylor’s formula gives
 f(α) = f(xn)+(α−xn)f′(xn)+ 1/2 (α−xn)2 fn(cn)

where cn is an unknown point between α and xn. This eventually leads to
α−xn+1 = (α−xn)2 [−fn(cn)/2f′(xn) ]

which means that the error in xn+1 is proportional to the square of the error in xn.
Hence, if the initial estimate was good enough, the error is expected to decrease very fast and the algorithm converges to the root.

To give an actual error estimation, write Taylor’s formula again
f(α) = f(xn)+(α−xn)f′(cn)

where cn is an unknown point between α and xn, so
α−xn = −f(xn)/f′(cn) ≈ −f(xn) / f′(xn)

so

 α−xn ≈ xn+1−xn

which is usually quite accurate.

Advantages and disadvantages of Newton’s method:

  1. The error decreases rapidly with each iteration.
  2. Newton’s method is very fast. (Compare with bisection method!).
  3. Unfortunately, for bad choices of x0 (the initial guess) the method can fail to converge! Therefore the choice of x0 is VERY IMPORTANT!.
  4. Each iteration of Newton’s method requires two function evaluations, while the bisection method requires only one.
                                                                                                                       
Example Code:

equation x4-x-10
derimeter f’(x) ==4x3-1

File name : newton.c

#include<conio.h> #include<stdio.h> #include<stdlib.h> #include<math.h> float f(float x) { return (x*x*x*x)-x-10; } floatdf(float x) { return (4*x*x*x)-1; } void main() { float x1=0,x2=0,t=0,E; intcnt=0,max,i; clrscr(); printf("\nEnter Initial Value X1---->"); scanf("%f",&x1); printf("\nEnter Error Iteration---->"); scanf("%f",&E); printf("\nEnter Max Iteration---->"); scanf("%d",&max); printf("\n ITERATION X1 FX1 F'X1 X2"); for(i=1;i<=max;i++) { cnt++; t=f(x1)/df(x1); x2=x1-t; printf("\n %d %.3f %.3f %.3f %.3f",cnt,x1,f(x1),df(x1),x2); if(fabs(t)<E) { break; } x1=x2; } printf("\n\t The Root Of Equation is= %f",x2); getch(); }

No comments:

Post a Comment

Thanks to comment our blog. i will contact you as soon as possible

Create Thumbnail in Video in Spring and ffmpeg

import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import org.jcodec.api.Fr...