Bisection Method
Algorithm : Bisection Method Bisection(a,b) { Let us consider a function f(x)=0.This algorithm finds a root of f(x)=0 between a and b. error = 10-6 if(f(a)*f(b)>0) //f(a) is a function { which returns the value of f(x) at x=a print “Root not lie between [a,b]” } else { c=(a+b)/2 do { fc=f(c) fa=f(a) fb=f(b) if(fc=0) { print “The root is c” return } else { if(fc<0) { if(fa<0) a=c else b=c } else if(fc>0) { if(fa>0) a=c else b=c } cprev=c c=(a+b)/2 } } while(|cprev-c|>error) print “The root is c” } } C Program For Bisection Method Source Code: #include&t;stdio.h> #include<conio.h> #include<math.h> void bisection(float,float); float f(float); void main() { int i; clrscr(); printf("\nThe equation is : X^3-9X+1=0\n\n"); for(i=-32768;i<32767;i++) { if(f(i)*f(i+1)<0) { bisection(i,i+1); getch(); } } } void bisection(float a,float b) { float error,c,Cprev,FA,FB,FC; error=0.000001; printf("a f(a) b f(b) c f(c) "); printf("\n--------------------------------------------------------------"); c=(a+b)/2; do { FA=f(a); FB=f(b); FC=f(c); printf("\n%f %f %f %f %f %f ",a,FA,b,FB,c,FC); if(FC==0) { printf("\nThe root is %f\n",c); return; } else { if(FC<0) { if(FA<0) a=c; else b=c; } else { if(FA>0) a=c; else b=c; } } Cprev=c; c=(a+b)/2; }while(fabs(Cprev-c)>error); printf("\nThe root is %f\n",c); } float f(float x) { return (x*x*x-9*x+1); } OUTPUT : The equation is : x^3-9x+1=0 a f(a) b f(b) c f(c) -------------------------------------------------------------- -4.000000 -27.000000 -3.000000 1.000000 -3.500000 -10.375000 -3.500000 -10.375000 -3.000000 1.000000 -3.250000 -4.078125 -3.250000 -4.078125 -3.000000 1.000000 -3.125000 -1.392578 -3.125000 -1.392578 -3.000000 1.000000 -3.062500 -0.160400 -3.062500 -0.160400 -3.000000 1.000000 -3.031250 0.428680 -3.062500 -0.160400 -3.031250 0.428680 -3.046875 0.136372 -3.062500 -0.160400 -3.046875 0.136372 -3.054688 -0.011455 -3.054688 -0.011455 -3.046875 0.136372 -3.050781 0.062598 -3.054688 -0.011455 -3.050781 0.062598 -3.052734 0.025606 -3.054688 -0.011455 -3.052734 0.025606 -3.053711 0.007084 -3.054688 -0.011455 -3.053711 0.007084 -3.054199 -0.002183 -3.054199 -0.002183 -3.053711 0.007084 -3.053955 0.002451 -3.054199 -0.002183 -3.053955 0.002451 -3.054077 0.000134 -3.054199 -0.002183 -3.054077 0.000134 -3.054138 -0.001024 -3.054138 -0.001024 -3.054077 0.000134 -3.054108 -0.000445 -3.054108 -0.000445 -3.054077 0.000134 -3.054092 -0.000156 -3.054092 -0.000156 -3.054077 0.000134 -3.054085 -0.000011 -3.054085 -0.000011 -3.054077 0.000134 -3.054081 0.000062 -3.054085 -0.000011 -3.054081 0.000062 -3.054083 0.000026 The root is -3.054084 a f(a) b f(b) c f(c) -------------------------------------------------------------- 0.000000 1.000000 1.000000 -7.000000 0.500000 -3.375000 0.000000 1.000000 0.500000 -3.375000 0.250000 -1.234375 0.000000 1.000000 0.250000 -1.234375 0.125000 -0.123047 0.000000 1.000000 0.125000 -0.123047 0.062500 0.437744 0.062500 0.437744 0.125000 -0.123047 0.093750 0.157074 0.093750 0.157074 0.125000 -0.123047 0.109375 0.016933 0.109375 0.016933 0.125000 -0.123047 0.117188 -0.053078 0.109375 0.016933 0.117188 -0.053078 0.113281 -0.018078 0.109375 0.016933 0.113281 -0.018078 0.111328 -0.000573 0.109375 0.016933 0.111328 -0.000573 0.110352 0.008180 0.110352 0.008180 0.111328 -0.000573 0.110840 0.003803 0.110840 0.003803 0.111328 -0.000573 0.111084 0.001615 0.111084 0.001615 0.111328 -0.000573 0.111206 0.000521 0.111206 0.000521 0.111328 -0.000573 0.111267 -0.000026 0.111206 0.000521 0.111267 -0.000026 0.111237 0.000247 0.111237 0.000247 0.111267 -0.000026 0.111252 0.000110 0.111252 0.000110 0.111267 -0.000026 0.111259 0.000042 0.111259 0.000042 0.111267 -0.000026 0.111263 0.000008 0.111263 0.000008 0.111267 -0.000026 0.111265 -0.000009 The root is 0.111264 a f(a) b f(b) c f(c) -------------------------------------------------------------- 2.000000 -9.000000 3.000000 1.000000 2.500000 -5.875000 2.500000 -5.875000 3.000000 1.000000 2.750000 -2.953125 2.750000 -2.953125 3.000000 1.000000 2.875000 -1.111328 2.875000 -1.111328 3.000000 1.000000 2.937500 -0.090088 2.937500 -0.090088 3.000000 1.000000 2.968750 0.446259 2.937500 -0.090088 2.968750 0.446259 2.953125 0.175922 2.937500 -0.090088 2.953125 0.175922 2.945312 0.042378 2.937500 -0.090088 2.945312 0.042378 2.941406 -0.023990 2.941406 -0.023990 2.945312 0.042378 2.943359 0.009160 2.941406 -0.023990 2.943359 0.009160 2.942383 -0.007423 2.942383 -0.007423 2.943359 0.009160 2.942871 0.000867 2.942383 -0.007423 2.942871 0.000867 2.942627 -0.003279 2.942627 -0.003279 2.942871 0.000867 2.942749 -0.001206 2.942749 -0.001206 2.942871 0.000867 2.942810 -0.000170 2.942810 -0.000170 2.942871 0.000867 2.942841 0.000348 2.942810 -0.000170 2.942841 0.000348 2.942825 0.000089 2.942810 -0.000170 2.942825 0.000089 2.942818 -0.000040 2.942818 -0.000040 2.942825 0.000089 2.942822 0.000025 2.942818 -0.000040 2.942822 0.000025 2.942820 -0.000008 The root is 2.942821
Tags:
Non Linear
0 comments