Write an algorithm for bisection method?

1 answer

Answer

1164693

2026-05-21 09:05

+ Follow

#include[stdio.h]

#include[math.h]

#define epsilon 1e-6

void main()

{

double g1,g2,g,v,v1,v2,dx;

int found,converged,i;

found=0;

printf(" enter the first guess\n");

scanf("%lf",&g1);

v1=g1*g1*g1-15;

printf("value 1 is %lf\n",v1);

while (found==0)

{

printf("enter the second guess\n");

scanf("%lf",&g2);

v2=g2*g2*g2-15;

printf(" value 2 is %lf\n",v2);

if (v1*v2>0)

{found=0;}

else

found=1;

}

printf("right guess\n");

i=1;

while (converged==0)

{

printf("\n iteration=%d\n",i);

g=(g1+g2)/2;

printf("new guess is %lf\n",g);

v=g*g*g-15;

printf("new value is%lf\n",v);

if (v*v1>0)

{

g1=g;

printf("the next guess is %lf\n",g);

dx=(g1-g2)/g1;

}

else

{

g2=g;

printf("the next guess is %lf\n",g);

dx=(g1-g2)/g1;

}

if (fabs(dx)'less than' epsilon

{converged=1;}

i=i+1;

}

printf("\nth calculated value is %lf\n",v);

}

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.