public
Created

  • Download Gist
MyClass.cpp
C++
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
#include "MyClass.h"
 
// Define Class members
// =====================
 
double MyClass::GetSolution( ) {
// point the pt_MyClass element to this class
p->pt_MyClass = this;
 
 
// instantiate a gsl_function ...
gsl_function F;
F.params = p;
// ... and point the address of our
// wrapper to it
F.function = &gslClassWrapper;
 
std::cout << "at x=2, objective function value is " << gslClassWrapper(2.0,p) << std::endl;
std::cout << "at x=1, objective function value is " << gslClassWrapper(1.0,p) << std::endl;
 
// set up GSL root solver
const gsl_root_fsolver_type *T;
gsl_root_fsolver *sroot;
T = gsl_root_fsolver_brent;
sroot = gsl_root_fsolver_alloc (T);
 
double low = -5;
double hi = 5;
double root;
root = find_root(sroot,&F, low , hi );
 
return root;
}
 
double MyClass::getC( void ) { return( p->c); } ;
 
 
// Define Objective Function
// =========================
 
double MyClass::obj(double x, void * par) {
 
gsl_f_pars *p = (gsl_f_pars *)par;
//return x;
//return (p->a * x + p->b) * x + p->c;
return pow(x - p->a , 3);
}
 
// main routine
// ============
 
int main(){
MyClass C;
 
// allocate the param structure
gsl_f_pars myp;
myp.a=1.0;
myp.b= 0;
myp.c=-5;
 
// point to myp from inside the class
C.setPars( &myp );
std::cout << "checking value of C: " << C.getC() << std::endl;
 
std::cout << "computing result : " << C.GetSolution() << std::endl;
 
return 0;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.