forked from cstrahan/aduni
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRFuncLib.java
More file actions
37 lines (32 loc) · 912 Bytes
/
RFuncLib.java
File metadata and controls
37 lines (32 loc) · 912 Bytes
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
public class RFuncLib{
public static double bracketRoot(double a, double b, double maxErr,Function f){
double fl = f.evaluate(a);
double fu = f.evaluate(b);
double upper = b;
double lower = a;
double x = 0.0;
double fx;
//System.out.println("delta = " + (upper - lower));
while((upper-lower) > maxErr){
x = (upper+lower)/2;
//System.out.println("x = " + (x));
fx = f.evaluate(x);
if(fx == 0) return(x);
else if((fx>0) && (fu>0)){ upper = x; fu = fx;}
else if((fx<0) && (fu<0)){ upper = x; fu = fx;}
else { lower = x; fl = fx;}
}
return(x);
}
public static double defIntegral(double a, double b, int nsteps,Function f){
int i;
double h = (b-a)/nsteps;
double x = a;
double sum = (f.evaluate(a)+f.evaluate(b))/2;
for(i=1;i<nsteps;i++){
x += h;
sum += f.evaluate(x);
}
return(sum * h);
}
}