Skip to content

Commit 2205a98

Browse files
Merge pull request #20 from dhakalnirajan/updates
Uploaded False Position Method code for NM Lab.
2 parents aaf1f2b + 910b326 commit 2205a98

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <stdio.h>
2+
#include <math.h>
3+
4+
#define f(x) (x*x + 5*x - 8)
5+
#define e 0.0001
6+
7+
int main() {
8+
float x1, x2, x0, f1, f2, f0;
9+
10+
do {
11+
printf("Enter any two variables: ");
12+
scanf("%f%f", &x1, &x2);
13+
f1 = f(x1);
14+
f2 = f(x2);
15+
} while (f1 * f2 > 0);
16+
17+
do {
18+
x0 = x1-((x2 - x1) / (f2 - f1)) * f1; // False-position formula
19+
f0 = f(x0);
20+
21+
if (f0 == 0) {
22+
break;
23+
}
24+
25+
if (f1 * f0 < 0) {
26+
x2 = x0;
27+
f2 = f0;
28+
} else {
29+
x1 = x0;
30+
f1 = f0;
31+
}
32+
33+
} while (fabs(x2 - x1) >= e);
34+
35+
printf("Required Root is: %f", x0);
36+
37+
return 0;
38+
}

0 commit comments

Comments
 (0)