#include "stdio.h"
#include "conio.h"
#include "graphics.h"
#include "math.h"
void main()
{
int power,i=0,m,n,n1,l,gd,gm,xaxis,yaxis,p=0;
double x1,x2,x0,fx1,fx2,fx0,c[100],n2,fx,gx1[100],gx2[100],f1[100],f2[100];
char a[50],b[100],e;
clrscr();
printf("Welcome to Bisection & False Position Methods\n\n");
printf("I design this program in different manner with GRAPH fro function, Let's TRY\n\n");
printf("\nEnter your lucky number (0 to 14) : ");
scanf("%d",&i);
textcolor(i);
textbackground(WHITE);
clrscr();
printf("\nEnter your name:\t");
scanf("%s",a);
clrscr();
teja:
printf("\nHi, %s Welcome to the world of computational mathematics\n\n",a);
printf("Which method would you like to perform\n\n 1) Bisection Method\t2)False Position Method\n\n");
scanf("%d",&m);
if(m!=1&&m!=2)
{
clrscr();
printf("\nInvalid option, try again\n");
goto teja;
}
else
{
printf("\n\nyour option is %d (y | n)\n\n",m);
e=getche();
if(e=='y'||e=='Y')
{
}
else
{
clrscr();
printf("\nSo, choose your option again\n\n");
goto teja;
}
}
clrscr();
printf("\n\nEnter the maximum power\t");
scanf("%d",&power);
for(i=power;i>=0;i--)
{
if(i==0)
{
printf("Enter a constant number\t");
}
else
{
printf("Enter the coefficient of the power %d \t",i);
}
scanf("%lf",&c[i]);
}
printf("\n\n");
for(i=power;i>=0;i--)
{
if(i==0)
{
printf("(%lf) = 0\n",c[i]);
}
else
{
printf("(%lf) x^%d + ",c[i],i);
}
}
printf("\nEnter the value of x1 & x2\n\n");
scanf("%lf%lf",&x1,&x2);
clrscr();
tej:
printf("\nhey...! %s do you want to perform function till \n\n1)No. of ittrations\t2)Accuracy\n\n",a);
scanf("%d",&n);
if(n==1)
{
printf("\nSo, enter the no. of ittrations\t");
scanf("%d",&n1);
}
else if(n==2)
{
printf("\nSo, enter the Accuracy till\t");
scanf("%lf",&n2);
}
else
{
printf("\n\tInvalid option try again\n");
goto tej;
}
clrscr();
for( l=1,fx0=1; (n==1?l<=n1:fabs(fx0)>=n2); l++ )
{
for(fx1=0,i=power;i>=0;i--)
{
fx=c[i] * pow(x1,i);
fx1=fx+fx1;
}
for(fx2=0,i=power;i>=0;i--)
{
fx=c[i] * pow(x2,i);
fx2=fx+fx2;
}
if(m==1)
{
x0=(x1+x2)/2;
}
else
{
x0=(x1-((x2-x1)/(fx2-fx1))*fx1);
}
for(fx0=0,i=power;i>=0;i--)
{
fx=c[i] * pow(x0,i);
fx0=fx+fx0;
}
if(l==1)
{
printf("\nIttration x1 x2 x0 fx1 fx2 fx0\n");
}
printf("\n%d %lf %lf %lf %lf %lf %lf\n",l,x1,x2,x0,fx1,fx2,fx0);
if((fx1>0&&fx2>0)||(fx1<0&&fx2<0))
{
printf("\n\nThe function won't be perform further, because f(x1) * f(x2) > 0 \n");
getch();
goto exit;
}
gx1[l]=x1;gx2[l]=x2;f1[l]=fx1;f2[l]=fx2;
p=p+1;
x1=((fx0>0&&fx1>0)||(fx0<0&&fx1<0)?x0:x1);
x2=((fx0>0&&fx2>0)||(fx0<0&&fx2<0)?x0:x2);
}
getch();
clrscr();
printf("\nDo you want to see the graph of the function: (y | n)\n\n");
e=getche();
if(e=='y'||e=='Y')
{
goto graph;
}
else
{
goto repeat;
}
graph:
clrscr();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
xaxis=getmaxx();
yaxis=getmaxy();
line(0,yaxis/2,xaxis,yaxis/2);
line(xaxis/2,0,xaxis/2,yaxis);
getch();
for(l=1;l<=2;l++)
{
gx1[l]=gx1[l]*10;
gx2[l]=gx2[l]*10;
f1[l]=f1[l]*10 ;
f2[l]=f2[l]*10;
line(gx1[l]+xaxis/2,f1[l]+yaxis/2,gx2[l]+xaxis/2,f2[l]+yaxis/2);
}
getch();
repeat:
printf("\n\n hi, %s thanks for spending your valuable time\n do you want to continue (y | n)\n");
e=getche();
if(e=='y'||e=='Y')
{
clrscr();
goto teja;
}
else if(e=='n'||e=='N')
{
goto exit;
}
else
{
printf("Please press or enter a valid option");
}
exit:
}
0 comments:
Post a Comment