Todays Code ::Numerical Method (Bisection Algorithm)
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) x*x+4*x-10
#define E 0.0001
void main(){
clrscr();
float x1,x2,x0,f1,f2,f0,root;
int count=0;
printf("\t\t\t\tBisection Method\n");
initial:printf("Enter initial guesses x1 and x2\nx1 : ");
scanf("%f",&x1);
printf("\nx2 : ");
scanf("%f",&x2);
f1 = f(x1);
f2 = f(x2);
if((f1*f2)>0){
printf("\nYour guesses are wrong. It can't be proceeded.\n");
goto initial;
}
printf("Iteration\tX1\tX2\tX0\tf0\tf1\tf2\n");
L:
count++;
printf("%d\t\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\n",count,x1,x2,x0,f0,f1,f2);
x0 = (x1+x2)/2;
f0 = f(x0);
// printf("%d\n",count);
if((f0*f1)<0)
{
x2 = x0;
f2 = f0;
}
else
{
x1 = x0;
f1 = f0;
}
if(fabs((x2-x1)/x2)<E)
{
root = (x1+x2)/2;
printf("\nThe required root is %.4f",root);
}
else {
goto L;
}
getch();
}
0 comments:
Post a Comment