-
Notifications
You must be signed in to change notification settings - Fork 0
/
fal.c
138 lines (112 loc) · 2.41 KB
/
fal.c
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# include <stdio.h>
# include <conio.h>
# include <math.h>
# define EPS 0.000001
# define F(x) x*x + 2.5*x -1.5
void fal(float *a, float *b, int *s, float *root, int *count);
void main()
{
int s, count;
float a, b, root;
printf("\n");
printf("SOLUTION BY FALSE POSITION METHOD \n");
printf("\n");
printf("Input starting values \n");
scanf("%f, %f", &a, &b);
float fa = F(a);
float fb = F(b);
printf("\n f(a) = %f",fa);
printf("\n f(b) = %f",fb);
printf("\n");
if(&fa == NULL)
{
printf("\n The function does not exist at the user entered start value. Please try again. \n");
}
if(&fb == NULL)
{
printf("\n The function does not exist at the user entered end value. Please try again. \n");
}
else
{
fal(&a, &b, &s, &root, &count);
if(s == 0)
{
printf("\n");
printf("Starting points do not bracket any roots or contain even number of roots \n");
printf("\n");
}
else
{
printf("\n");
printf("Root = %f \n", root);
printf("F(root) = %f \n", F(root));
printf("No. of Iterations = %d \n", count);
printf("\n");
}
}
}
void fal(float *a, float *b, int *s, float *root, int *count)
{
float x1, x2, x0, f0, f1, f2;
x1 = *a;
x2 = *b;
f1 = F(x1);
f2 = F(x2);
if(f1*f2 >0)
{
*s = 0;
return;
}
else
{
*count = 1;
printf("count \t");
printf(" x1 \t\t");
printf("f1 \t\t");
printf("x2 \t\t");
printf("f2 \t\t");
printf("x0 \t");
printf("\n");
begin:
x0 = x1 - f1 * (x2 - x1) /( f2 - f1);
// x0 = ((x1*f2)-(x2*f1))/(f2-f1);
f0 = F(x0);
if(f0==0)
{
*s = 1;
*root = x0;
return;
}
if (f1*f0 < 0)
{
x2 = x0;
f2 = f0;
}
else
{
x1 = x0;
f1 = f0;
}
// printf("%5d %15.6f \n", *count, x1, x2);
printf("%d \t", *count);
printf("%f \t", x1);
printf("%f \t", f1);
printf("%f \t", x2);
printf("%f \t", f2);
printf("%f \t", x0);
printf("\n");
// if(fabs((x2-x1)/x2) < EPS)
if(fabs(F(x0)) < EPS)
{
*s = 1;
// *root = (x2+x1)/2.0;
*root = x0;
return;
}
else
{
*count = *count + 1;
goto begin;
}
}
}