ASSIGNMENT – 3
Problem Statement: Write a C program to find the root of 3x - cosx - 1 = 0 using Regula Falsi
method, correct upto 4 significant figures.
Solution:
Algorithm:
1) Start
2) Define ( f(x) = 3x - cos(x) - 1 ).
3) Take guesses ( a ), ( b ) where ( f(a) times f(b) < 0 ).
4) Loop until ( |f(c)| < 10^{-4} ):
𝑎.𝑓(𝑏)−𝑏.𝑓(𝑎)
• Compute c =
𝑓(𝑏)−𝑓(𝑎)
• If \( f(a) \times f(c) < 0 \), set \( b = c \); else, set \( a = c \).
5) Print root ( c ) (accurate to **4 significant figures**).
6) Stop
Program
Code:
#include <stdio.h>
#include <math.h>
double f(double x) {
return 3 * x - cos(x) - 1;
}
int main() {
int i;
double a, b, c, fa, fb, fc;
int max_iterations = 1000;
double tolerance = 1e-4;
printf("Enter initial guesses a and b (such that f(a)*f(b) < 0): ");
scanf("%lf %lf", &a, &b);
fa = f(a);
fb = f(b);
if (fa * fb >= 0) {
printf("Invalid initial guesses. f(a) and f(b) must have opposite signs.\n");
return 1;
}
printf("\nIteration\t a\t\t b\t\t c\t\t f(c)\n");
printf("------------------------------------------------------------------\n");
for (i = 1; i <= max_iterations; i++) {
c = (a * fb - b * fa) / (fb - fa);
fc = f(c);
printf("%d\t\t %.6lf\t %.6lf\t %.6lf\t %.6lf\n", i, a, b, c, fc);
if (fabs(fc) < tolerance) {
printf("\nRoot found: %.6lf (correct up to 4 significant figures)\n", c);
return 0;
}
if (fa * fc < 0) {
b = c;
fb = fc;
} else {
a = c;
fa = fc;
}
}
printf("\nMaximum iterations reached. Root approximation: %.6lf\n", c);
return 0;
}
Output:
Discussion: The Regula Falsi method to solve 3x - cos(x) - 1 = 0. It takes two initial guesses
that bracket the root, then iteratively refines the estimate using linear interpolation. The loop
continues until the solution reaches 4-digit accuracy (tolerance 1e-4) or 1000 iterations. It prints
each step's values and finally outputs the root approximation or a non-convergence warning.
The method guarantees convergence while being faster than bisection.