Wednesday, October 5, 2016

প্রোগ্রামিং-এর গল্প (A tale of Programming) : Numerical Method- Coding for False Position Method

//program for false position
#include<stdio.h>
#include<math.h>
float f(float x)
{
    return x*x-4*x-10;
}
main()
{
    float xl,xu,x0,fxl,fxu,fx0,e,error,root;
    int i=0;
    printf("Enter lower limit xl:");
    scanf("%f",&xl);
    printf("Enter upper limit xu:");
    scanf("%f",&xu);
    printf("Enter limit of error:");
    scanf("%f",&e);
    fxl=f(xl);
    fxu=f(xu);
    if(fxl*fxu>0)
    {
        printf("\nSorry, the limits doesn't bracket any root:");
        main();
    }
    else//    if(fxl*fxu<0)
    {
        printf("\n\n\nIteration  xl\txu\tx0\tfxl\tfxu\tfx0\terror");
        while(1)
        {
           i++;
           x0=xl-((fxl*(xl-xu))/(fxl-fxu));
           fxl=f(xl);
           fxu=f(xu);
           fx0=f(x0);
           if(fx0*fxl<0)
           {
               xu=x0;
           }
           else//           if(fx0*fxu<0)
           {
               xl=x0;
           }
           error=fabs((xl-xu)/xl);
           printf("\n%d\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f",i,xl,xu,x0,fxl,fxu,fx0,error);
           if(error<=e)
           {
               x0=xl-((fxl*(xl-xu))/(fxl-fxu));
               root=x0;
               printf("\n\n\n\nThe desired root is= %f\n\n",root);
               break;
           }
        }
    }
}

Output for False Position Method

No comments:

Post a Comment