Sunday 18 August 2019

C programming for bisection method


/*bisection method*/
#include<stdio.h>
#include<math.h>
#define f(x) ((x*sin(x))+((x-2)*cos(x)))
void
main()
{
                int count;
                float a,b,x,y,y1,y2;
    printf("enter the valu of the intervals:");
                scanf("%f %f",&a,&b);
                y1=f(a);
                y2=f(b);
                count=1;
                if(y1*y2>0)
                                printf("\n invalid interval\n");
                                else
                                {label: x=(a+b)/2;
                                y=f(x);
                                if(y1*y<0)
                                {               b=x;
                                                y2=y;
                                }
                                else
                                {               a=x;
                                                y1=y;
                                }
                                printf("%d\t",count);
                                printf("%f\n",x);
                                count=count+1;
                                if(fabs(b-a)>0.001)
                                                goto label;
                                }
}
RESULT:
enter the valu of the intervals:2 5
1       3.500000
2       2.750000
3       3.125000
4       2.937500
5       2.843750
6       2.890625
7       2.867188
8       2.855469
9       2.849609
10      2.852539
11      2.851074
12      2.851807
Press any key to continue

No comments:

Post a Comment

Thanks for comment.