-
Notifications
You must be signed in to change notification settings - Fork 0
/
Regular_Falsi_Method
41 lines (40 loc) · 1014 Bytes
/
Regular_Falsi_Method
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<stdio.h>
#include<math.h>
float f(float x)
{
return x - sin(x) + 1;
}
void regula (float *x, float x0, float x1, float fx0, float fx1, int *itr)
{
*x = x0 - ((x1 - x0) / (fx1 - fx0))*fx0;
++(*itr);
printf("Iteration no. %3d X = %7.5f \n", *itr, *x);
}
int main ()
{
int itr = 0, maxmitr;
float x0,x1,x2,x3,allerr;
printf("\nEnter the values of x0, x1:\n");
scanf("%f %f", &x0, &x1);
printf("Enter allowed error and maximum iterations:\n");
scanf("%f" , &allerr);
scanf("%d" , &maxmitr);
regula (&x2, x0, x1, f(x0), f(x1), &itr);
do
{
if (f(x0)*f(x2) < 0)
x1=x2;
else
x0=x2;
regula (&x3, x0, x1, f(x0), f(x1), &itr);
if (fabs(x3-x2) < allerr)
{
printf("After %d iterations, root = %6.4f\n", itr, x3);
return 0;
}
x2=x3;
}
while (itr<maxmitr);
printf("Solution does not converge or iterations not sufficient:\n");
return 1;
}