Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   Newton Raphson Formula (http://www.go4expert.com/articles/newton-raphson-formula-t17642/)

Newton Raphson Formula

Newton Raphson Interpolation Formula implementation in C.
Code: C

`#include <stdio.h>#include <conio.h>#define N 100float comb(float,int);/*****************************************************************//*****  Newton-raphson-interpolation formula implementation  *****//*****   This program uses the forward difference formula    *****//*****************************************************************/void main(){    /* y=y[0]+rC1*dely[0]+rC2*del2y[0]+rC3*del3y[0]+...+rCn*delny[0] */    /* where r is given as ==>  x=x[0]+r*h */    float x[N],y[N],n,temp[N],res,r,X,h;    int i,j;    char ch;    do    {        printf("\nEnter the total number of points for interpolation\n");        scanf("%f",&n);        printf("\nEnter the values of x\n");        for(i=0;i<n;i++)            scanf("%f",&x[i]);        h=x[1]-x[0];        printf("\nEnter the values of y\n");        for(i=0;i<n;i++)            scanf("%f",&y[i]);        res=y[0];        printf("\nEnter the values of x to find y\n");        scanf("%f",&X);        r=(X-x[0])/h;        for(i=1;i<n;i++)        {            for(j=0;j<n;j++)                temp[j]=y[j+1]-y[j];            for(j=0;j<n;j++)                y[j]=temp[j];            res=res+comb(r,i)*temp[0];        }        printf("\nThe answer is %g\n",res);        printf("\nDo you wish to continue[y/n]\n");        ch=getche();    }while(ch=='Y' || ch=='y');    printf("\nPress any key to exit\n");}float comb(float n,int r){    int i;    float res=1;    for(i=1;i<=r;i++)        res=res*(n-i+1)/i;    return(res); }`

The code below finds the root of linear algebraic any order equation by "Newton-Raphson" method
Code: C

`#include <stdio.h>#include <conio.h>#include <math.h>#include <stdlib.h>#define ACC 0.01#define N 10int degree;float coeff[N];void input(void);float F(float);float dF_dx(float);/********************************************************//*****  Finds the root of any linear algebraic any  *****//*****  order equation by "Newton-Raphson"  method  *****//********************************************************/void main(){    char ch;    do    {        input();        printf("Do you wish to continue[y/n]\n");        fflush(stdin);        scanf("%c",&ch);    }while(ch=='Y' || ch=='y');}void input(void){    int i,print,loop=0;    float ig;    float h;    printf("\nEnter the degree of diferential equation\n");    scanf("%d",&degree);    print=degree;    for(i=0;i<=degree;i++)    {        printf("Enter the co-efficient of x to the power of %d\t",print--);        scanf("%f",&coeff[i]);    }    printf("\nEnter the initial guess\n");    scanf("%f",&ig);    if(F(ig)==0)        printf("\n%f  is the root of the equation\n",ig);    do    {        h=-(F(ig))/(dF_dx(ig));        ig=ig-h;        loop++;        if(loop>=40)            exit(0);    }while(fabs(h)>=ACC);    printf("\n%f  is the root\n",ig);}float dF_dx(float x){    int i,deg;    float func=0.0;    deg=degree;    for(i=0;i<degree;i++)    {        func=func+coeff[i]*deg*pow(x,(deg-1));        deg--;    }    if(func==0)        return(1);    return(func);}float F(float x){    int i,deg;    float func=0.0;    deg=degree;    for(i=0;i<=degree;i++)    {        func=func+coeff[i]*pow(x,deg);        deg--;    }    return(func);}`

 shabbir 3Jun2009 09:27

Re: Newton Raphson Formula

Re: Newton Raphson Formula

Same here : code does not conform to ANSI C specifications !

 LenoxFinlay 10Jun2009 12:46

Re: Newton Raphson Formula

I know the formula but can't derive it from the Newton-Raphson's method of finding roots of f(x)=0.

the formula that I know is x1 = x0 - k * f(x0) / f`(x0)
where k = number of times a single root is repeating for f(x) = 0.

plz help!!

 faceliftguide 18Jun2009 16:34

Re: Newton Raphson Formula

Great work !
Well done ! !Keep it up ! !

 All times are GMT +5.5. The time now is 23:04.