Friday 8 May 2015

Numerical Analysis Bisection Method



 Bisection Method

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main(void)
{
//clrscr();
float xl,xu,xu_n,xl_n,xm,f_xm,xm_old,xm_new;
float f_xl,f_xu;
float condition,error;
printf("Enter value of xl:\n");
scanf("%f",&xl);
printf("Enter value of xu:\n");
scanf("%f",&xu);
f_xl=(xl*xl*xl)-20;
f_xu=(xu*xu*xu)-20;
printf("f(xl)=%.3f\nf(xu)=%.3f\n",f_xl,f_xu);
condition=f_xu*f_xl;
printf("product of f(xl) and f(xu): %f\n",condition);
for(int i=0;i<=4;i++)
{
//cout<<"";
xm_old=(xl+xu)/2;
printf("xm_old=%f\n",xm_old);
xm=xm_old;
//xu=xm_old;
f_xl=(xl*xl*xl)-20;
f_xm=(xm*xm*xm)-20;
printf("f(xl)=%.3f\nf(xm)=%.3f\n",f_xl,f_xm);
condition=f_xm*f_xl;
printf("product of f(xl) and f(xm): %f\n",condition);

if(condition<0){xl=xl;xu=xm_old;}
if(condition>0){xl=xm_old;xu=xu;}
//xm_new=(xl+xu)/2;
//printf("xm_new=%f\n",xm_new);
//error=((xm_new-xm_old)/xm_new)*100;
//printf("Error=%f\n",error);
}


getch();
}

No comments:

Post a Comment