Discontinuous Galerkin Library
#include "dg/algorithm.h"
nullstelle.h
Go to the documentation of this file.
1
6#ifndef _NULLSTELLE_
7#define _NULLSTELLE_
8
9#include <exception>
10#include <math.h>
11#include "backend/exceptions.h"
12
13namespace dg{
14
19class NoRoot1d: public std::exception
20{
21 private:
22 double x1, x2, wert_min, wert_max;
23 public:
31 NoRoot1d(double x_min, double x_max, double wert_min, double wert_max): x1(x_min), x2(x_max), wert_min(wert_min), wert_max(wert_max){}
34 void display() const
35 {
36 std::cerr << "Between " <<x1<<"/"<<wert_min << " and "<<x2<<"/"<<wert_max<<" is no root!\n";
37 }
41 char const* what() const throw(){ return "There is no root!";}
42};
43
63template <typename UnaryOp>
64int bisection1d (UnaryOp& op, double& x_min, double& x_max, const double eps)
65{
66 double mitte;
67 double wert_max, wert_mitte, wert_min;
68 wert_max=op(x_max);
69 wert_min=op(x_min);
70
71 if(wert_max*wert_min>=0)
72 throw NoRoot1d(x_min, x_max, wert_min, wert_max);
73
74 int j_max = 60;
75 for(int j=0; j<j_max; j++)
76 {
77 wert_mitte = op( mitte =(x_min+x_max)/2.0 );
78 if(wert_mitte==0) {x_min=x_max=mitte; return j+3;}
79 else if(wert_mitte*wert_max>0) x_max = mitte;
80 else x_min = mitte;
81 if( fabs(x_max-x_min)<eps*fabs(x_max) + eps) return j+3;
82 }
83 throw std::runtime_error("Too many steps in root finding!");
84}
85
86}//namespace dg
87#endif //_NULLSTELLE_
88
89
90
Exception class, that stores boundaries for 1D root finding.
Definition: nullstelle.h:20
char const * what() const
what string
Definition: nullstelle.h:41
NoRoot1d(double x_min, double x_max, double wert_min, double wert_max)
construct
Definition: nullstelle.h:31
void display() const
display left and right boundary on std::cerr
Definition: nullstelle.h:34
Error classes or the dg library.
int bisection1d(UnaryOp &op, double &x_min, double &x_max, const double eps)
Find a root of a 1d function .
Definition: nullstelle.h:64
This is the namespace for all functions and classes defined and used by the discontinuous Galerkin li...