Newton Raphson Formula

Discussion in 'C' started by Nadr, May 21, 2009.

  1. Nadr

    Nadr New Member

    Joined:
    Oct 16, 2007
    Messages:
    165
    Likes Received:
    1
    Trophy Points:
    0
    Newton Raphson Interpolation Formula implementation in C.
    Code:
    #include <stdio.h>
    #include <conio.h>
    #define N 100
    float 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:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #include <stdlib.h>
    
    #define ACC 0.01
    #define N 10
    
    int 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);
    }
    
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  3. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    Same here : code does not conform to ANSI C specifications !
     
  4. LenoxFinlay

    LenoxFinlay Banned

    Joined:
    Apr 15, 2009
    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    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!!
     
  5. faceliftguide

    faceliftguide New Member

    Joined:
    Jun 18, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Great work !
    Well done ! !Keep it up ! !
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice