-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsincurve.c
68 lines (55 loc) · 1.23 KB
/
sincurve.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
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MAXN 200
double bisection(double *root,double a,double b,double (*func)(double));
double sine(double x)
{
double rad=3.14/180;
return sin(rad*x);
}
int main()
{
double a, b;
printf("Enter the interval a and b: ");
scanf("%lf %lf", &a, &b);
double root1;
bisection(&root1,a, b, sine);
printf("The root of func1 is: %lf\n", root1);
//double root2 = bisection(a, b, func2);
// printf("The root of func2 is: %lf\n", root2);
return 0;
}
double bisection(double *root,double a, double b, double (*func)(double))
{
double c;
double fa,fb,fc,tol=10.e-6;
fa=(*func)(a);
fb=(*func)(b);
if(fa*fb>=0)
{
puts("no roots");
return EXIT_FAILURE;
}
int n=200,i=0;
while (i<=n)
{
*root = a+(b-a)/2;
fc=(*func)(*root);
printf("%9.6lf %9.6lf %9.6lf %9.6lf %9.6lf %9.6lf %d\n",a,b,*root,fa,fb,fc,i);
if (fabs(fc) <=tol)
return c;
else if (fc*fa < 0)
{
b=*root;
fb=fc;
}
else
{
a = *root;
fa=fc;
}
i++;
}
//return c;
}