Search This Blog

Rugular falsi , Bisection, Newton Raphson, common methods program in c, c program

#include<stdio.h>
#include<math.h>
float fu(float x)//defining a function
{
    float f;
    f = ((x*x*x)+(4*x*x)-10);
    return f;
}
double dfu(double x)
{
    double f;
    f=(3*x*x)+(8*x);
    return f;
   
}
int main()
{
    float xl,xu,xr,xi;
    int i;
    printf("\nEnter 1 for Rugular falsi 2 for Bisetion for and 3 for Newton Raphson method\n");
    scanf("%d",&i);

switch(i)
    {
       
        case 1://Regular falsi method
        {
         printf("\nCalculation using Regular falsi method\n");
         printf("\nEnter xl and xu\n");
         scanf("%f%f",&xl,&xu);
          xr=xu-(fu(xu)*(xl-xu)/(fu(xl)-fu(xu)));
      if(fu(xu)*fu(xl)<0)
      {
      while(fabs(fu(xr))>0.0001)   
     {
        xr=xu-(fu(xu)*(xl-xu)/(fu(xl)-fu(xu)));
        if(fu(xr)*fu(xl)<0)
    {
        xu = xr;
    }
    else
    {
    xl=xr;   
    }
}
printf("\nRoot of the equation is xr =%f\n\n",xr);
}
else
{
    printf("\nEntered values are not correct\n");
}
            
        }
        break;
        case 2://Bisection method
        {
        xr =(xu+xl)/2;
        printf("\nCalculation using Bisection method\n");
        printf("\nEnter xl and xu\n");
    scanf("%f%f",&xl,&xu);
 if(fu(xu)*fu(xl)<0)
 {
while(fabs(fu(xr))>0.0001)   
{
    xr=((xl+xu)/2);
    if(fu(xr)*fu(xl)<0)
    {
        xu = xr;
    }
    else
    {
    xl=xr;   
    }
}
printf("\nRoot of the equation is xr =%f\n\n",xr);
}
else
{
    printf("\nEntered values are not correct as f(xu)*f(xl)>0\n");
}
   
        }
        break;
        case 3://Newton raphson method
        {
        printf("\n Calculation using Newton raphson Method\n");
        printf("\nEnter value of xi=\n");
        scanf("%f",&xi);
        while(fabs(fu(xi))>0.0001)
        {
            xr=xi-(fu(xi)/dfu(xi));
            xi=xr;
        }
        printf("\nRoot of the equation is %f\n",xr);
        }
    break;
}

}