Find roots of any linear algebraic nth order equation by Regula Falsi method

Discussion in 'C++' started by coderzone, Aug 14, 2010.

  1. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    39
    Trophy Points:
    28
    Find roots of any linear algebraic nth order equation by Regula Falsi method

    The Code



    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #define ACC 0.00001
    #define N 10
    int degree;
    float coeff[N];
    void input(void);
    float power(float,int);
    float F(float);
    /******************************************************/
    /*****  Finds the  root  of any linear algebraic  *****/
    /*****  any order equation by Regula Falsi method *****/
    /******************************************************/
    void main()
    {
    	char ch;
    	clrscr();
    	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;
    	float a,b;
    	float f;
    	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]);
    	}
    	do
    	{
    		printf("\nEnter the values of a and b\n");
    		scanf("%f%f",&a,&b);
    		if(F(a)*F(b)>0)
    			printf("\nThere is no root in the given interval please re-enter\n");
    	}while(F(a)*F(b)>0);
    	if(F(a)*F(b)==0)
    	{
    		if(F(a)==0)
    			printf("\n%f  is the root of the equation\n",a);
    		else if(F(b)==0)
    			printf("\n%f  is the root of the equation\n",b);
    		else
    			printf("\n%f  and %f  are the roots of the equation\n",a,b);
    	}
    	do
    	{
    		f=((a*F(b))-(b*F(a)))/(F(b)-F(a));
    		if(F(a)*F(f)<0)
    			b=f;
    		else if(F(b)*F(f)<0)
    			a=f;
    	}while(fabs(F(f))>=ACC);
    	printf("\n%f  is the root\n",f);
    }
    float F(float x)
    {
    	int i,deg;
    	float func=0.0;
    	deg=degree;
    	for(i=0;i<=degree;i++)
    	{
    		func=func+coeff[i]*power(x,deg);
    		deg--;
    	}
    	return(func);
    }
    float power(float x,int deg)
    {
    	int i;
    	float res=1;;
    	for(i=1;i<=deg;i++)
    		res=res*x;
    	return(res);
    }
    
     
    shabbir likes this.
  2. shabbir

    shabbir Administrator Staff Member

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

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