blob: aa72471ee20c04cb7f30e290a320bae7d9d5bd3a (
plain)
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
|
#include <cassert>
double ddivid(double (*foo)(double),double x1,double x2,double err)
{
assert(x2>x1);
assert(foo(x2)*foo(x1)<0);
while(x2-x1>err)
{
double x=(x2+x1)/2.;
double y=foo(x);
double y1=foo(x1);
double y2=foo(x2);
if(y1*y<0)
{
x2=x;
}
else if(y2*y<0)
{
x1=x;
}
else
{
assert(0);
}
}
return (x1+x2)/2.;
}
double ddivid(double (*foo)(double),double z,double x1,double x2,double err)
{
assert(x2>x1);
assert((foo(x2)-z)*(foo(x1)-z)<0);
while(x2-x1>err)
{
double x=(x2+x1)/2.;
double y=foo(x)-z;
double y1=foo(x1)-z;
double y2=foo(x2)-z;
if(y1*y<0)
{
x2=x;
}
else if(y2*y<0)
{
x1=x;
}
else
{
assert(0);
}
}
return (x1+x2)/2.;
}
|