cout<<"\n\tAt x = "<<a-1<<", f(x)= "<<evaluate((float)a-1);
cout<<"\n\tAt x = "<<a<<", f(x)= "<<evaluate((float)a);
cout<<"\n\tAt x = "<<b<<", f(x)= "<<evaluate((float)b);
return 1;
}
return 0;
}
void main()
{
Front();
textcolor(LIGHTCYAN);
textbackground(BLUE);
clrscr();
A:
clrscr();
cout<<"\n\n\n\n\t\t\tREGULA FALSI METHOD";
cout<<"\n\n\n\n\t[T] Theory of False Position";
cout<<"\n\n\n\n\t[S] Solutions for Polynomial Equations";
cout<<"\n\n\n\n\t[Q] Quit";
cout<<"\n\n\n\n\tYour choice: ";
cin>>choice;
switch (choice)
{
case 't':
case 'T':
{
clrscr();
cout<<"\n\n\n\n\n\n LINEAR INTERPOLATION (FALSE POSITION)";
cout<<"\n_____________________________________________________________________";
cout<<"\n\n\tA way to avoid such pathology is to ensure that the root is";
cout<<"\n bracketed between the two starting values and remains between";
cout<<"\n the successive pairs. When this is done, the method is known as";
cout<<"\n linear interpolation, or, as the method of false position (in";
cout<<"\n Latin, Regula Falsi). This technique is similar to bisection ";
cout<<"\n except the next iterate is taken at the intersection of a line";
cout<<"\n between the pair of x-values and the x-axis rather than at the ";
cout<<"\n midpoint. Doing so gives faster convergence than does bisection,";
cout<<"\n but at the expense of a more complicated algorithm.";
getch();
clrscr();
algorithm();
goto A;
//break;
}
case 's':
case 'S':
{
clrscr();
cout<<"\n\t______________________________________________________";
cout<<"\n\t\t\tREGULA FALSI METHOD";
cout<<"\n\t______________________________________________________";
cout<<"\n\n\tUp to what power does the equation have? Answer : ";
cin>>nf;
fx=(int*)malloc((nf+1)*sizeof(int));
cout<<"\n\n\tThe equation has "<<nf<<" as the highest power.";
cout<<"\n\n\tNext, input the corresponding coefficients of\n";
for(i=nf;i>=0;i--)
{
cout<<"\n\t\t x^";
cout<<i;
cout<<": ";
cin>>fx[i];
}
for(i=0;;i++)
{
if(sign_change(i,i+1))
{
s1=(float)i;
s2=(float)i+1;
break;
}
}
cout<<"\n\tThe root of the equation is in between "<<s1<<" and "<<s2<<".";
cout<<"\n\tHere is the iteration:";
process();
graph();
goto A;
//break;
}
case 'q':
case 'Q':
{
quitProg();
break;
}
default:
{
wrongChoice();
goto A;
}
//break;
}
//getch();
}
void graph()
{
clrscr();
int gd=DETECT, gm;
initgraph(&gd,&gm,"");
setbkcolor(WHITE);
setcolor(BLUE);
//float m=getmaxx()/abs(x[7]);
//float n=getmaxy()/10;
settextstyle(BOLD_FONT,HORIZ_DIR,3);
outtextxy(100,100,"Graph of the Polynomial");
settextstyle(SMALL_FONT,HORIZ_DIR,5);
setcolor(RED);
outtextxy(50,300,"*NOTE: Because of limitations, some equations might be plot out of range.");
getch();
clrscr();
setcolor(MAGENTA);
outtextxy(400,400,"Graph of the Polynomial");
setcolor(BLUE);
moveto(100,350);
linerel(20+x[1],-1.25*y[1]);
linerel(20+x[2],-1.25*y[2]);
linerel(20+x[3],-1.25*y[3]);
linerel(20+x[4],-1.25*y[4]);
linerel(20+x[5],-1.25*y[5]);
linerel(20+x[6],-1.25*y[6]);
linerel(20+x[7],-1.25*y[7]);
linerel(20+x[8],-1.25*y[8]);
linerel(20+x[9],-1.25*y[9]);
linerel(20+x[10],-1.25*y[10]);
linerel(20+x[12],-1.25*y[12]);
linerel(20+x[13],-1.25*y[13]);
linerel(20+x[14],-1.25*y[14]);
linerel(20+x[16],-1.25*y[16]);
linerel(20+x[17],-1.25*y[17]);
linerel(20+x[18],-1.25*y[18]);
linerel(20+x[19],-1.25*y[19]);
//for the regula falsi line
moveto(100,350);
linerel(20+x[1],-1.25*y[1]);
linerel(20+x[2],-1.25*y[2]);
linerel(20+x[3],-1.25*y[3]);
linerel(20+x[4],-1.25*y[4]);
linerel(20+x[5],-1.25*y[5]);
linerel(20+x[6],-1.25*y[6]);
setcolor(RED);
setlinestyle(DOTTED_LINE,1,2);
linerel(20+x[15],-1.25*y[15]);
outtext("The root lies here.");
}
//Graph per iteration
void graph2()
{
clrscr();
int gd=DETECT, gm;
initgraph(&gd,&gm,"");
setbkcolor(WHITE);
setcolor(BLUE);
setcolor(BLUE);
moveto(100,250);
linerel(100+x[6],-20*y[6]);
linerel(100+x[7],-20*y[7]);
setcolor(MAGENTA);
setlinestyle(DOTTED_LINE,1,2);
lineto(100,250);
setcolor(GREEN);
linerel(100+x[11],-20*y[11]);
linerel(0,20*y[7]);
settextstyle(SMALL_FONT,HORIZ_DIR,4);
outtext("x");
setcolor(BROWN);
outtextxy(350,275,"LEGENDS:");
outtextxy(350,300," Blue = Polynomial curve");
outtextxy(350,325," Magenta= Line between (a,f(a)) and (b,f(b))");
outtextxy(350,350," Green = Line from (a,f(a)) and (x, f(x))");
settextstyle(SMALL_FONT,HORIZ_DIR,4);
outtextxy(50,425,"NOTE: If the green line intersects with the blue, that is the root,");
outtextxy(75,450,"otherwise, 'x' points the root (projected vertically towards the blue line).");
getch();
closegraph();
}
void Front(void)
{
int g_driver=DETECT, g_mode;
initgraph (&g_driver, &g_mode, "");
setbkcolor(GREEN);
setcolor(WHITE);
int x=getmaxx()/15;
int y=getmaxy()/5;
rectangle(10, 10, getmaxx()-10, getmaxy()-10);
rectangle(20, 20, getmaxx()-20, getmaxy()-20);
settextstyle(BOLD_FONT,HORIZ_DIR,5);
outtextxy(x*.5,y*.1," *****************");
outtextxy(x*.5,y*3.9," *****************");
delay(200);
outtextxy(x*4.5,y*1.5,"R");
delay(200);
outtextxy(x*5.5,y*1.5,"E");
delay(200);
outtextxy(x*6.5,y*1.5,"G");
delay(200);
outtextxy(x*7.5,y*1.5,"U");
delay(200);
outtextxy(x*8.5,y*1.5,"L");
delay(200);
outtextxy(x*9.5,y*1.5,"A");
delay(200);
outtextxy(x*5,y*2.5,"F");
delay(200);
outtextxy(x*6,y*2.5,"A");
delay(200);
outtextxy(x*7,y*2.5,"L");
delay(200);
outtextxy(x*8,y*2.5,"S");
delay(200);
outtextxy(x*9,y*2.5,"I");
delay(200);
settextstyle(BOLD_FONT,HORIZ_DIR,2);
outtextxy(x*4.5,y*3.5,"Press any key...");
getch();
closegraph();
restorecrtmode();
}
void wrongChoice(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
setbkcolor(RED);
setcolor(YELLOW);
int color;
int x=getmaxx()/16;
int y=getmaxy()/5;
int mradius;
rectangle(10, 10, getmaxx()-10, getmaxy()-10);
rectangle(20, 20, getmaxx()-20, getmaxy()-20);
settextstyle(BOLD_FONT,HORIZ_DIR,4);
delay(500);
outtextxy(x*1,y*2,"W");
delay(200);
outtextxy(x*2.5,y*2,"R");
delay(200);
outtextxy(x*3.5,y*2,"O");
delay(200);
outtextxy(x*4.5,y*2,"N");
delay(200);
outtextxy(x*5.5,y*2,"G");
delay(200);
outtextxy(x*6.5,y*2," ");
delay(200);
outtextxy(x*7.5,y*2,"C");
delay(200);
outtextxy(x*8.5,y*2,"H");
delay(200);
outtextxy(x*9.5,y*2,"O");
delay(200);
outtextxy(x*10.75,y*2,"I");
delay(200);
outtextxy(x*11.5,y*2,"C");
delay(200);
outtextxy(x*12.5,y*2,"E");
delay(200);
outtextxy(x*13.5,y*2,"!");
delay(200);
outtextxy(x*14.5,y*2,"!");
delay(200);
outtextxy(x*15.5,y*2,"!");
delay(200);
settextstyle(BOLD_FONT,HORIZ_DIR,2);
outtextxy(x*4.5,y*3.5,"Press any key...");
getch();
closegraph();
restorecrtmode();
}
void quitProg(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
setbkcolor(BLACK);
setcolor(GREEN);
int color;
int x=getmaxx()/16;
int y=getmaxy()/5;
int mradius;
int r=50;
settextstyle(BOLD_FONT,HORIZ_DIR,4);
delay(500);
outtextxy(x*1,y*1.5,"*");
delay(200);
outtextxy(x*2,y*1.5,"*");
delay(200);
outtextxy(x*3,y*1.5,"*");
delay(200);
outtextxy(x*4,y*1.5,"T");
delay(200);
outtextxy(x*5,y*1.5,"H");
delay(200);
outtextxy(x*6,y*1.5,"A");
delay(200);
outtextxy(x*7,y*1.5,"N");
delay(200);
outtextxy(x*8,y*1.5,"K");
outtextxy(x*9,y*1.5," ");
delay(200);
outtextxy(x*10,y*1.5,"Y");
delay(200);
outtextxy(x*11,y*1.5,"O");
delay(200);
outtextxy(x*12,y*1.5,"U");
delay(200);
outtextxy(x*13,y*1.5,"*");
delay(200);
outtextxy(x*14,y*1.5,"*");
delay(200);
outtextxy(x*15,y*1.5,"*");
delay(200);
setcolor(WHITE);
settextstyle(BOLD_FONT,HORIZ_DIR,1);
outtextxy(x*1.75,y*3,"Designed by LCG David, MB Manalo,");
outtextxy(x*.1,y*3.5,"JA Field, ATQ Adraneda, and SR Fernandez");
delay(3500);
while (!kbhit()){
color = random(getmaxcolor()-1)+1;
setcolor(color);
setfillstyle(random(14)+1,color);
circle(random(getmaxx()), random(getmaxy()),random(r));
}
getch();
closegraph();
}
void algorithm(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
setbkcolor(BLACK);
setcolor(WHITE);
int color;
int x=getmaxx()/16;
int y=getmaxy()/5;
settextstyle(SIMPLEX_FONT,HORIZ_DIR,1);
rectangle(10, 10, getmaxx()-10, getmaxy()-10);
rectangle(20, 20, getmaxx()-20, getmaxy()-20);
outtextxy(x*1,y*.25,"An Algorithm for the method of False Position (regula falsi)");
settextstyle(SIMPLEX_FONT,HORIZ_DIR,1);
outtextxy(x*1.5,y*1,"To determine a root of f(x) = 0, given values of x0 and x1");
outtextxy(x*1.5,y*1.25,"that bracket a root, that is, f(x0) and f(x1) are of");
outtextxy(x*1.5,y*1.5,"opposite sign,");
outtextxy(x*1.5,y*2,"REPEAT");
outtextxy(x*1.5,y*2.25,"Set x2 = x0 - f(x0)*(x0 - x1)/(f(x0) - f(x1)).");
outtextxy(x*1.5,y*2.5,"IF f(x2) of opposite sign to f(x0):");
outtextxy(x*1.5,y*2.75," Set x1 = x2.");
outtextxy(x*1.5,y*3,"ELSE Set x0 = x2.");
outtextxy(x*1.5,y*3.25,"ENDIF.");
outtextxy(x*1.5,y*3.5,"UNTIL |f(x2)| < tolerance value.");
outtextxy(x*1.5,y*4,"Note: The method may fail if f(x) is discontinous on the");
outtextxy(x*1.5,y*4.25,"interval." );
getch();
closegraph();
restorecrtmode();
}