xxxxxxxxxx
/* Program: Finding real roots of nonlinear
equation using Regula Falsi or False Position Method
Author: CodeSansar
Date: November 18, 2018 */
/* Header Files */
#include<stdio.h>
#include<conio.h>
#include<math.h>
/* Defining equation to be solved.
Change this equation to solve another problem. */
#define f(x) x*log10(x) - 1.2
int main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1;
clrscr();
/* Inputs */
up:
printf("\nEnter two initial guesses:\n");
scanf("%f%f", &x0, &x1);
printf("Enter tolerable error:\n");
scanf("%f", &e);
/* Calculating Functional Values */
f0 = f(x0);
f1 = f(x1);
/* Checking whether given guesses brackets the root or not. */
if( f0*f1 > 0.0)
{
printf("Incorrect Initial Guesses.\n");
goto up;
}
/* Implementing Regula Falsi or False Position Method */
printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n");
do
{
x2 = x0 - (x0-x1) * f0/(f0-f1);
f2 = f(x2);
printf("%d\t\t%f\t%f\t%f\t%f\n",step, x0, x1, x2, f2);
if(f0*f2 < 0)
{
x1 = x2;
f1 = f2;
}
else
{
x0 = x2;
f0 = f2;
}
step = step + 1;
}while(fabs(f2)>e);
printf("\nRoot is: %f", x2);
getch();
return 0;
}
xxxxxxxxxx
/*========================================================================!
!Program to find solutions to equations of the form f(x)=0 using !
!Regula Falsi method (Method of False Position) !
!LANGUAGE :: C (compiler: GNU GCC) !
!========================================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double f(double y){
return y*exp(y)-1;
}
double newApprox(double yL,double yH){
return yL-f(yL)*(yH-yL)/(f(yH)-f(yL));
}
int main()
{
double xL,xH,diff,tolerance;
double xMid,prevXMid;
printf("Enter the two numbers xL and xH between which root is to be found\n");
scanf("%lf %lf",&xL,&xH);
printf("\nEnter the tolerance\n");
scanf("%lf",&tolerance);
if (f(xL)==0){
printf("\nThe root is : %lf",xL);
return 0;
}
else if (f(xH)==0){
printf("\nThe root is : %lf",xH);
return 0;
}
else if (f(xL)*f(xH)>0){
printf("\nf(xH) and f(xL) must be of opposite sign");
return 0;
}
xMid = newApprox(xL,xH);
printf("\nxL= %lf \t xH= %lf \t xMid= %lf \t f(xMid)= %lf",xL,xH,xMid,f(xMid));
do{
prevXMid = xMid;
if (f(xMid)*f(xH) < 0){
xL = xMid;
}
else if ( f(xMid)*f(xL) <0 ){
xH = xMid;
}
xMid = newApprox(xL,xH);
diff = fabs((xMid-prevXMid)/xMid);
printf("\nxL= %lf \t xH= %lf \t xMid= %lf \t f(xMid)= %lf",xL,xH,xMid,f(xMid));
}while(diff > tolerance);
printf("\n\nThe solution is %lf\n",xMid);
return 0;
}
/*--------------------- OUTPUT -------------------------------------------
Enter the two numbers xL and xH between which root is to be found
0.1
0.9
Enter the tolerance
0.0000001
xL= 0.100000 xH= 0.900000 xMid= 0.438347 f(xMid)= -0.320500
xL= 0.438347 xH= 0.900000 xMid= 0.534792 f(xMid)= -0.087062
xL= 0.534792 xH= 0.900000 xMid= 0.559236 f(xMid)= -0.021707
xL= 0.559236 xH= 0.900000 xMid= 0.565224 f(xMid)= -0.005294
xL= 0.565224 xH= 0.900000 xMid= 0.566678 f(xMid)= -0.001284
xL= 0.566678 xH= 0.900000 xMid= 0.567031 f(xMid)= -0.000311
xL= 0.567031 xH= 0.900000 xMid= 0.567116 f(xMid)= -0.000075
xL= 0.567116 xH= 0.900000 xMid= 0.567137 f(xMid)= -0.000018
xL= 0.567137 xH= 0.900000 xMid= 0.567142 f(xMid)= -0.000004
xL= 0.567142 xH= 0.900000 xMid= 0.567143 f(xMid)= -0.000001
xL= 0.567143 xH= 0.900000 xMid= 0.567143 f(xMid)= -0.000000
xL= 0.567143 xH= 0.900000 xMid= 0.567143 f(xMid)= -0.000000
xL= 0.567143 xH= 0.900000 xMid= 0.567143 f(xMid)= -0.000000
The solution is 0.567143
---------------------------------------------------------------------------*/