1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Newton Raphson Formula

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

  1. Nadr

    Nadr New Member

    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

  3. SaswatPadhi

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

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

    LenoxFinlay Banned

    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

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

Share This Page