Pages - Menu

Tuesday, 22 July 2014

Implementation of Regula Falsi Method in C.

Here is the code..

//AIM: TO SOLVE A GIVEN EQUATION BY REGULA FALSI //METHOD
#include<stdio.h>
float fx(float );
void main()
{
float a,b,i,x,y;
int j,n;
printf("Given equation is fx = x^3-x^2-2 \n");
for(i=1;fx(i)>0;)
i-=0.2;
a=i;
for(i=1;fx(i)<0;)
i+=0.2;
b=i;
printf("enter the no. of iterations \n");
scanf("%d", &n);
for(j=0;j<n;j++)
{
y = (fx(b)/(fx(b)-fx(a)));
x = (b-((b-a)*y));
if(fx(x)<0)
{
printf("root after %d iteration is %f\n",(j+1),x);
a = x;
}
else
{
printf("root after %d iteration is %f\n",(j+1),x);
b = x;
}
}
printf("so the root is %f \n",x);

}
float fx(float m)
{
float f;
f = ((m*m*m)-(m*m)-2);
return(f);
}


For Turbo C users..

//AIM: TO SOLVE A GIVEN EQUATION BY REGULA FALSI METHOD
#include<stdio.h>
#include<conio.h>
float fx(float );
void main()
{
float a,b,i,x,y;
int j,n;
clrscr();
printf("Given equation is fx = x^3-x^2-2 \n");
for(i=1;fx(i)>0;)
i-=0.2;
a=i;
for(i=1;fx(i)<0;)
i+=0.2;
b=i;
printf("enter the no. of iterations \n");
scanf("%d", &n);
for(j=0;j<n;j++)
{
y = (fx(b)/(fx(b)-fx(a)));
x = (b-((b-a)*y));
if(fx(x)<0)
{
printf("root after %d iteration is %f\n",(j+1),x);
a = x;
}
else
{
printf("root after %d iteration is %f\n",(j+1),x);
b = x;
}
}
printf("so the root is %f \n",x);
getch();
}
float fx(float m)
{
float f;
f = ((m*m*m)-(m*m)-2);
return(f);
}

No comments:

Post a Comment