Skip to content

Commit 8cee3f7

Browse files
committed
init
0 parents commit 8cee3f7

File tree

9 files changed

+504
-0
lines changed

9 files changed

+504
-0
lines changed

FalsePosition.c

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# include <stdio.h>
2+
# include <conio.h>
3+
# include <math.h>
4+
# define EPS 0.000001
5+
# define F(x) x*x + 2.5*x -1.5
6+
7+
void fal(float *a, float *b, int *s, float *root, int *count);
8+
9+
void main()
10+
{
11+
int s, count;
12+
float a, b, root;
13+
14+
printf("\n");
15+
printf("SOLUTION BY FALSE POSITION METHOD \n");
16+
printf("\n");
17+
printf("Input starting values \n");
18+
scanf("%f, %f", &a, &b);
19+
20+
float fa = F(a);
21+
float fb = F(b);
22+
23+
printf("\n f(a) = %f",fa);
24+
printf("\n f(b) = %f",fb);
25+
26+
printf("\n");
27+
28+
if(&fa == NULL)
29+
{
30+
printf("\n The function does not exist at the user entered start value. Please try again. \n");
31+
}
32+
if(&fb == NULL)
33+
{
34+
printf("\n The function does not exist at the user entered end value. Please try again. \n");
35+
}
36+
37+
else
38+
{
39+
fal(&a, &b, &s, &root, &count);
40+
41+
if(s == 0)
42+
{
43+
printf("\n");
44+
printf("Starting points do not bracket any roots or contain even number of roots \n");
45+
printf("\n");
46+
}
47+
48+
else
49+
{
50+
printf("\n");
51+
printf("Root = %f \n", root);
52+
printf("F(root) = %f \n", F(root));
53+
printf("No. of Iterations = %d \n", count);
54+
printf("\n");
55+
}
56+
}
57+
58+
}
59+
60+
void fal(float *a, float *b, int *s, float *root, int *count)
61+
{
62+
float x1, x2, x0, f0, f1, f2;
63+
64+
x1 = *a;
65+
x2 = *b;
66+
f1 = F(x1);
67+
f2 = F(x2);
68+
69+
if(f1*f2 >0)
70+
{
71+
*s = 0;
72+
return;
73+
}
74+
else
75+
{
76+
77+
*count = 1;
78+
79+
printf("count \t");
80+
printf(" x1 \t\t");
81+
printf("f1 \t\t");
82+
printf("x2 \t\t");
83+
printf("f2 \t\t");
84+
printf("x0 \t");
85+
printf("\n");
86+
87+
begin:
88+
89+
x0 = x1 - f1 * (x2 - x1) /( f2 - f1);
90+
// x0 = ((x1*f2)-(x2*f1))/(f2-f1);
91+
f0 = F(x0);
92+
93+
if(f0==0)
94+
{
95+
*s = 1;
96+
*root = x0;
97+
return;
98+
}
99+
100+
if (f1*f0 < 0)
101+
{
102+
x2 = x0;
103+
f2 = f0;
104+
}
105+
else
106+
{
107+
x1 = x0;
108+
f1 = f0;
109+
}
110+
111+
// printf("%5d %15.6f \n", *count, x1, x2);
112+
printf("%d \t", *count);
113+
printf("%f \t", x1);
114+
printf("%f \t", f1);
115+
printf("%f \t", x2);
116+
printf("%f \t", f2);
117+
printf("%f \t", x0);
118+
printf("\n");
119+
120+
// if(fabs((x2-x1)/x2) < EPS)
121+
if(fabs(F(x0)) < EPS)
122+
{
123+
*s = 1;
124+
// *root = (x2+x1)/2.0;
125+
*root = x0;
126+
return;
127+
}
128+
else
129+
{
130+
*count = *count + 1;
131+
goto begin;
132+
}
133+
}
134+
}
135+

bis.c

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# include <stdio.h>
2+
# include <conio.h>
3+
# include <math.h>
4+
# define EPS 0.000001
5+
# define F(x) x*x + 2.5*x -1.5
6+
7+
void bis(float *a, float *b, int *s, float *root, int *count);
8+
9+
void main()
10+
{
11+
int s, count;
12+
float a, b, root;
13+
14+
printf("\n");
15+
printf("SOLUTION BY BISECTION METHOD \n");
16+
printf("\n");
17+
printf("Input starting values \n");
18+
scanf("%f, %f", &a, &b);
19+
20+
float fa = F(a);
21+
float fb = F(b);
22+
23+
printf("\n f(a) = %f",fa);
24+
printf("\n f(b) = %f",fb);
25+
26+
printf("\n");
27+
28+
if(&fa == NULL)
29+
{
30+
printf("\n The function does not exist at the user entered start value. Please try again. \n");
31+
}
32+
if(&fb == NULL)
33+
{
34+
printf("\n The function does not exist at the user entered end value. Please try again. \n");
35+
}
36+
37+
else
38+
{
39+
40+
bis(&a, &b, &s, &root, &count);
41+
42+
if(s == 0)
43+
{
44+
printf("\n");
45+
printf("Starting points do not bracket any roots or contain even number of roots \n");
46+
printf("\n");
47+
}
48+
49+
else
50+
{
51+
printf("\n");
52+
printf("Root = %f \n", root);
53+
printf("F(root) = %f \n", F(root));
54+
printf("\n No. of Iterations = %d \n", count);
55+
}
56+
}
57+
58+
}
59+
60+
61+
void bis(float *a, float *b, int *s, float *root, int *count)
62+
{
63+
float x1, x2, x0, f0, f1, f2;
64+
65+
x1 = *a;
66+
x2 = *b;
67+
f1 = F(x1);
68+
f2 = F(x2);
69+
70+
if(f1*f2 >0)
71+
{
72+
*s = 0;
73+
return;
74+
}
75+
else
76+
{
77+
*count = 1;
78+
79+
printf("count \t");
80+
printf(" x1 \t\t");
81+
printf("f1 \t\t");
82+
printf("x2 \t\t");
83+
printf("f2 \t\t");
84+
printf("x0 \t");
85+
printf("\n");
86+
87+
begin:
88+
89+
x0 = (x1+x2)/2;
90+
f0 = F(x0);
91+
92+
if(f0==0)
93+
{
94+
*s = 1;
95+
*root = x0;
96+
return;
97+
}
98+
if (f1*f0 <0)
99+
{
100+
x2=x0;
101+
f2=f0;
102+
}
103+
else
104+
{
105+
x1 = x0;
106+
f1 = f0;
107+
}
108+
109+
// printf("%5d %15.6f \n", *count, x1, x2);
110+
printf("%d \t", *count);
111+
printf("%f \t", x1);
112+
printf("%f \t", f1);
113+
printf("%f \t", x2);
114+
printf("%f \t", f2);
115+
printf("%f \t", x0);
116+
printf("\n");
117+
118+
// if(fabs((x2-x1)/x2) < EPS)
119+
if(fabs(F((x1+x2)/2.0)) < EPS)
120+
{
121+
*s = 1;
122+
*root = (x1+x2)/2.0;
123+
return;
124+
}
125+
else
126+
{
127+
*count = *count + 1;
128+
goto begin;
129+
}
130+
}
131+
132+
}

0 commit comments

Comments
 (0)