forked from cstrahan/aduni
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRFunc.java
More file actions
38 lines (33 loc) · 917 Bytes
/
RFunc.java
File metadata and controls
38 lines (33 loc) · 917 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
38
public abstract class RFunc{
public abstract double evaluate(double x);
public double bracketRoot(double a, double b, double maxErr){
double fl = evaluate(a);
double fu = 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 = 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 double defIntegral(double a, double b, int nsteps){
int i;
double h = (b-a)/nsteps;
double x = a;
double sum = (evaluate(a)+evaluate(b))/2;
for(i=1;i<nsteps;i++){
x += h;
sum += evaluate(x);
}
return(sum * h);
}
}