Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import exception.NotAllowedScopeException;
- import exception.NotImplementedMethodException;
- import exception.NotImplementedSolutionException;
- import exception.UnknownReimannSumRuleException;
- public class ReimannSum {
- public double getReimannSum(
- Function function,
- Bounds bounds,
- double accuracy,
- ReimannSumRule rule,
- TypeOfSolution solutionType
- ) throws
- NotImplementedMethodException,
- UnknownReimannSumRuleException,
- NotAllowedScopeException {
- checkAllowedScope(function, bounds);
- switch (solutionType) {
- case SOLUTION_BY_FORMULAS:
- return getSumByFormulasSolution(function, bounds, accuracy, rule);
- case SOLUTION_BY_RUNGE:
- return getSumByRungeSolution(function, bounds, accuracy, rule);
- default:
- throw new NotImplementedSolutionException();
- }
- }
- public double getReimannSum(
- Function function,
- Bounds bounds,
- double accuracy
- ) throws
- NotImplementedMethodException,
- UnknownReimannSumRuleException,
- NotAllowedScopeException {
- return getReimannSum(
- function,
- bounds,
- accuracy,
- ReimannSumRule.TRAPEZOIDAL_RULE,
- TypeOfSolution.SOLUTION_BY_FORMULAS
- );
- }
- private void checkAllowedScope(Function function, Bounds bounds)
- throws NotAllowedScopeException {
- for (Interval notAllowedScope : function.getAllowedScope()) {
- if (notAllowedScope.isIntersect(bounds)) {
- throw new NotAllowedScopeException();
- }
- }
- }
- private double getSumByFormulasSolution(
- Function function,
- Bounds bounds,
- double accuracy,
- ReimannSumRule rule
- ) throws NotImplementedMethodException, UnknownReimannSumRuleException {
- int n = getCountOfSections(function, bounds, accuracy, rule);
- return getSumByRuleByN(function, bounds, rule, n);
- }
- private double getSumByRungeSolution(
- Function function,
- Bounds bounds,
- double accuracy,
- ReimannSumRule rule
- ) throws NotImplementedSolutionException {
- int n = 5;
- double curValue = getSumByRuleByN(function, bounds, rule, n);
- double prevValue;
- do {
- n <<= 1;
- prevValue = curValue;
- curValue = getSumByRuleByN(function, bounds, rule, n);
- } while (!(Math.abs(curValue - prevValue) < accuracy));
- return curValue;
- }
- private double getSumByRuleByN(Function function, Bounds bounds, ReimannSumRule rule, int n)
- throws NotImplementedSolutionException {
- double sum = 0d;
- double step = bounds.getLength() / n;
- for (Bounds cur: bounds.separate(n)) {
- switch (rule) {
- case LEFT_RULE:
- sum += function.getValue(cur.getLeftBound()) * step;
- break;
- case RIGHT_RULE:
- sum += function.getValue(cur.getRightBound()) * step;
- break;
- case MIDPOINT_RULE:
- sum += function.getValue(
- (cur.getLeftBound() + cur.getRightBound()) / 2
- ) * step;
- break;
- case TRAPEZOIDAL_RULE:
- sum += (
- function.getValue(cur.getLeftBound()) +
- function.getValue(cur.getRightBound())
- ) * step / 2;
- break;
- case SIMPSONS_RULE:
- //TODO: solve
- default:
- throw new NotImplementedSolutionException();
- }
- }
- return sum;
- }
- private int getCountOfSections(
- Function function,
- Bounds bounds,
- double accuracy,
- ReimannSumRule rule
- ) throws UnknownReimannSumRuleException, NotImplementedMethodException {
- double res;
- switch (rule) {
- case LEFT_RULE:
- case RIGHT_RULE:
- case MIDPOINT_RULE:
- res = Math.sqrt(
- Math.pow(bounds.getLength(), 3) *
- function.get2Derivative().getMaxValue(bounds) / 24 / accuracy
- );
- return (int) (res + 1.0d);
- case SIMPSONS_RULE:
- res = Math.sqrt(
- Math.sqrt(
- Math.pow(bounds.getLength(), 5) *
- function.get4Derivative().getMaxValue(bounds) / 180 / accuracy
- )
- );
- return (int) (res + 1.0d);
- case TRAPEZOIDAL_RULE:
- res = Math.sqrt(
- Math.pow(bounds.getLength(), 3) *
- function.get2Derivative().getMaxValue(bounds) / 12 / accuracy
- );
- return (int) (res + 1.0d);
- default:
- throw new UnknownReimannSumRuleException();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement