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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
#include <iostream>
#include <math.h>
using namespace std;
namespace csc350Lib_calc_base {
class Function1D {
public:
Function1D(void){};
virtual ~Function1D(void){};
virtual float func(float x) = 0;
virtual float dfunc(float x);
virtual bool isExactDerivativeDefined(void) = 0;
};
}
//-------------------------------------------------------
using namespace csc350Lib_calc_base;
namespace csc350Lib_calculus_snle {
typedef enum SolutionStatus {
SEARCH_SUCCESSFUL = 0,
SEARCH_FAILED_TOO_MANY_ITERATIONS = 1,
SEARCH_FAILED_OUT_OF_RANGE = 2,
SEARCH_FAILED_NUMERICAL_ERROR = 3,
SEARCH_FAILED_OTHER_REASON = 4
} SolutionStatus;
class SolutionNLE{
public:
SolutionNLE(){}; //Default constructor
SolutionStatus getStatus(void);
float getSolution(void);
float getValueAtSolution(void);
int getNumberOfIterations(void);
void setSolution(float);
void setFuncValue(float);
void setIterations(int);
~SolutionNLE();
private:
float solution;
float funcValue;
int numOfIterations;
SolutionStatus status;
};
void SolutionNLE::setSolution(float answer)
{
solution = answer;
}
float SolutionNLE::getSolution(void)
{
return solution;
}
float SolutionNLE::getValueAtSolution(void)
{
return funcValue;
}
void SolutionNLE::setFuncValue(float answer)
{
funcValue = answer;
}
int SolutionNLE::getNumberOfIterations(void)
{
return numOfIterations;
}
void SolutionNLE::setIterations(int iterations)
{
numOfIterations = iterations;
}
SolutionNLE::~SolutionNLE(){}
//--------------------------------------------------
class NonlinearSolver {
public:
NonlinearSolver(void);
static const SolutionNLE * solve(Function1D* f, float a, float b, float tol);
~NonlinearSolver();
private:
SolutionNLE * nleSolution;
};
NonlinearSolver::NonlinearSolver()
: nleSolution(){}
NonlinearSolver::~NonlinearSolver(){}
}
//-----------------------------------------------------------------
using namespace csc350Lib_calculus_snle;
//---------------------------------------------------------------
class MyFunc1 : public Function1D {
public:
float func(float);
bool isExactDerivativeDefined(void);
};
float MyFunc1::func(float x)
{
return cos(x+5);
}
bool MyFunc1::isExactDerivativeDefined(void)
{
return false;
}
//-----------------------------------------------------------------
class NonlinearSolver_bisection : public NonlinearSolver {};
const SolutionNLE* NonlinearSolver::solve(Function1D *f, float a, float b, float tol)
{
SolutionNLE *nleSolution = new SolutionNLE;
float fa = f->func(a);
float fb = f->func(b);
float c = (a+b)/2;
int iterations = 1;
float tolerance = 2 * tol;
while ((b-a > tolerance ) && (c != a) && (c != b)){
float fc = f->func(c);
if ((fc * fa) > 0 ) {
a = c;
fa = fc;
}
else if (fc != 0){
b = c;
fb = fc;
}
else {
nleSolution->setIterations(iterations);
nleSolution->setFuncValue(f->func(c));
nleSolution->setSolution(c);
return nleSolution;
}
c = (a + b)/2;
iterations++;
}
nleSolution->setIterations(iterations);
nleSolution->setFuncValue(f->func(c));
nleSolution->setSolution(c);
cout << nleSolution->getSolution() << " f(c) soulution "<< endl;
cout << nleSolution->getValueAtSolution() << " value at solution" << endl;
cout << nleSolution->getNumberOfIterations() << " number of iterations " << endl;
return nleSolution;
}
int main (int argc, char * const argv[]) {
Function1D *f1;
f1 = new MyFunc1();
NonlinearSolver *nls;
nls = new NonlinearSolver_bisection();
nls->solve(f1, 1., 4., 0.01);
nls->nleSolution = nls->solve(f1, 1., 4., 0.01);
nls->nleSolution->getSolution();
float x = 3.f;
float y1 = f1->func(x),
dy1 = f1->dfunc(x);
cout << y1 << endl;
cout << dy1 << endl;
return 0;
}
|