Newton raphson method

Post on 10-Jan-2017

596 views 0 download

Transcript of Newton raphson method

Newton-Raphson method, also known as the Newton’s Method, is the simplest and fastest approach to find the root of a function.

It is an open bracket method and requires only one initial guess.

Newton’s method is often used to improve the result or value of the root obtained from other methods.

This method is more useful when the first derivative of f(x) is a large value.

This is Newton’s Method of finding roots. It is an example of an Algorithm (a specific set of computational steps.)

It is sometimes called the Newton-Raphson method

Guess: 2.44948979592

2.44948979592 .00000013016f

Amazingly close to zero!

Newton’s Method: 1n

n nn

f xx x

f x

This is a Recursive algorithm because a set of steps are repeated with the previous answer put in the next repetition. Each repetition is called an Iteration.

Commonly, we use the Newton-Raphson method to find a root of a complicated function . This iterative process follows a set guideline to approximate one root, considering the function, its derivative, and an initial x-value.

We know that a root of a function is a zero of the function. This means that at the "root" the function equals zero.

We can find these roots of a simple function such as: f(x) = x2 - 4 simply by setting the function to zero, and solving:

f(x) = x2-4 = 0(x+2)(x-2) = 0x = 2 or x = -2

The Newton-Raphson method uses an iterative process to approach one root of a function. The specific root that the process locates depends on the initial, arbitrarily chosen x-value.

Here, xn is the current known x-value, f(xn) represents the value of the function at xn, f'(xn) is the derivative (slope) at xn.

xn+1 represents the next x-value that you are trying to find.

Essentially, f'(x), the derivative represents f(x)/dx (dx = delta-x). Therefore, the term f(x)/f'(x) represents a value of dx.

The more iterations that are run, the closer dx will be to zero (0).

To see how this works, we will perform the Newton-Raphson method on the function that we investigated earlier, f(x) = x2-4.

Below are listed the values that we need to know in order to complete the process.

The table below shows the execution of the process.

Thus, using an initial x-value of six (6) we find one root of the equation f(x) = x2-4 is x=2.

If we were to pick a different inital x-value, we may find the same root, or we may find the other one, x=-2.

A graphical representation can also be very helpful. Below, you see the same function f(x) = x2-4 (shown in blue). The process here is the same as above.

In the first iteration, the red line is tangent to the curve at x0. The slope of the tangent is the derivative at the point of tangency, and for the first iteration is equal to 12. Dividing the value of the function at the initial x (f(6)=32) by the slope of the tangent (12), we find that the delta-x is equal to 2.67. Subtracting this from six (6) we find that the new x-value is equal to 3.33.

Another way of considering this is to find the root of this tangent line. The new x-value (xn+1) will be equal to the root of the tangent to the function at the current x-value (xn).

Features of Newton Raphson Method:

•Type – open bracket•No. of initial guesses – 1•Convergence – quadratic•Rate of convergence – faster•Accuracy – good•Programming effort – easy•Approach – Taylor’s series

The C program for Newton Raphson method presented here is a programming approach which can be used to find the real roots of not only a nonlinear function, but also those of algebraic and transcendental equations.

Newton Raphson Method Newton Raphson Method Algorithm:Algorithm:1.Start

2.Read x, e, n, d*x is the initial guesse is the absolute error i.e the desired degree of accuracyn is for operating loopd is for checking slope*

3.Do for i =1 to n in step of 24.f = f(x)5.f1 = f'(x)6.If ( [f1] < d), then display too small slope and goto 11.

*[ ] is used as modulus sign*7.x1 = x – f/f18.If ( [(x1 – x)/x1] < e ), the display the root as x1 and goto 11.

*[ ] is used as modulus sign*9.x = x1 and end loop10.Display method does not converge due to oscillation.11.Stop

Newton Raphson Method Newton Raphson Method Flowchart:Flowchart:

Source CodeSource Code# include <stdio.h> # include <conio.h> # include <math.h> # include <process.h> # include <string.h> # define f(x) 3*x -cos(x)-1 # define df(x) 3+sin(x) void NEW_RAP(); void main() { clrscr(); printf ("\n Solution by NEWTON RAPHSON method \n"); printf ("\n Equation is: ");

printf ("\n\t\t\t 3*X - COS X - 1=0 \n\n "); NEW_RAP(); getch(); } void NEW_RAP() { long float x1,x0; long float f0,f1; long float df0; int i=1; int itr; float EPS; float error;

for(x1=0;;x1 +=0.01) { f1=f(x1); if (f1 > 0) { break; } } x0=x1-0.01; f0=f(x0); printf(" Enter the number of iterations: "); scanf(" %d",&itr); printf(" Enter the maximum possible error: "); scanf("%f",&EPS);

if (fabs(f0) > f1) { printf("\n\t\t The root is near to %.4f\n",x1); } if(f1 > fabs(f(x0))) { printf("\n\t\t The root is near to %.4f\n",x0); } x0=(x0+x1)/2; for(;i<=itr;i++) { f0=f(x0); df0=df(x0); x1=x0 - (f0/df0); printf("\n\t\t The %d approximation to the root is:%f",i,x1); error=fabs(x1-x0);

if(error<EPS) { break; } x0 = x1; } if(error>EPS) { printf("\n\n\t NOTE:- "); printf("The number of iterations are not sufficient."); } printf("\n\n\n\t\t\t ------------------------------"); printf("\n\t\t\t The root is %.4f ",x1); printf("\n\t\t\t ------------------------------"); }

Write a program of GENERAL NEWTON RAPHSON METHOD.#include<conio.h>#include<stdio.h>#include<stdlib.h>#include<math.h> int user_power,i=0,cnt=0,flag=0;int coef[10]={0};float x1=0,x2=0,t=0;float fx1=0,fdx1=0; void main(){  clrscr();

printf("\n\n\t\t\t PROGRAM FOR NEWTON RAPHSON GENERAL");

printf("\n\n\n\tENTER THE TOTAL NO. OF POWER:::: "); scanf("%d",&user_power);

for(i=0;i<=user_power;i++) { printf("\n\t x^%d::",i); scanf("%d",&coef[i]); }

printf("\n");

printf("\n\t THE POLYNOMIAL IS ::: "); for(i=user_power;i>=0;i--)//printing coeff. { printf(" %dx^%d",coef[i],i); }

printf("\n\tINTIAL X1---->"); scanf("%f",&x1);

printf("\n ******************************************************"); printf("\n ITERATION X1 FX1 F'X1 "); printf("\n ******************************************************"); do { cnt++; fx1=fdx1=0; for(i=user_power;i>=1;i--) { fx1+=coef[i] * (pow(x1,i)) ; } fx1+=coef[0]; for(i=user_power;i>=0;i--) { fdx1+=coef[i]* (i*pow(x1,(i-1))); }

t=x2; x2=(x1-(fx1/fdx1));

x1=x2;

printf("\n %d %.3f %.3f %.3f ",cnt,x2,fx1,fdx1);

}

while((fabs(t - x1))>=0.0001); printf("\n\t THE ROOT OF EQUATION IS %f",x2); getch();}

/*******************************OUTPUT***********************************/

PROGRAM FOR NEWTON RAPHSON GENERAL

ENTER THE TOTAL NO. OF POWER:::: 3

x^0::-3

x^1::-1

x^2::0

x^3::1

THE POLYNOMIAL IS ::: 1x^3 0x^2 -1x^1 -3x^0

INTIAL X1---->3

************************************** ITERATION X1 FX1 F'X1 ************************************** 1 2.192 21.000 26.000 2 1.794 5.344 13.419 3 1.681 0.980 8.656 4 1.672 0.068 7.475 5 1.672 0.000 7.384 **************************************

THE ROOT OF EQUATION IS 1.671700

C PROGRAM OF NEWTON RAPHSON METHOD :#include<conio.h>#include<stdio.h>#include<stdlib.h>#include<math.h> int max_power,i=0,cnt=0,flag=0;int coef[10]={0};float x1=0,x2=0,t=0;float fx1=0,fdx1=0; int main(){ printf("-----------------------------------------------------------\n"); printf("-----------------------------------------------------------\n\n"); printf("\n\n\t C PROGRAM FOR NEWTON RAPHSON METHOD"); printf("\n\n\n\tENTER THE MAXIMUM POWER OF X = ");

scanf("%d",&max_power); for(i=0;i<=max_power;i++) { printf("\n\t x^%d = ",i); scanf("%d",&coef[i]); } printf("\n"); printf("\n\tTHE POLYNOMIAL IS = "); for(i=max_power;i>=0;i--)/*printing coefficients*/ { printf(" %dx^%d",coef[i],i); } printf("\n\n\tFirst approximation x1 ----> "); scanf("%f",&x1);

printf("\n\n-----------------------------------------------------------\n"); printf("\n ITERATION \t x1 \t F(x1) \t \tF'(x1) "); printf("\n-----------------------------------------------------------\n"); do { cnt++; fx1=fdx1=0; for(i=max_power;i>=1;i--) { fx1+=coef[i] * (pow(x1,i)) ; } fx1+=coef[0]; for(i=max_power;i>=0;i--) { fdx1+=coef[i]* (i*pow(x1,(i-1))); }

t=x2; x2=(x1-(fx1/fdx1)); x1=x2; printf("\n\t %d \t%.3f \t %.3f\t\t%.3f ",cnt,x2,fx1,fdx1); }while((fabs(t - x1))>=0.0001); printf("\n\n\n\t THE ROOT OF EQUATION IS = %f",x2); getch();}