Skip to content
Snippets Groups Projects
Commit 0516dd8d authored by Sergi Hernandez's avatar Sergi Hernandez
Browse files

Added a flag to indicate if the convergence went beyond the specified limits.

parent d2f67dbe
No related branches found
No related tags found
No related merge requests found
......@@ -28,7 +28,7 @@ class CGradient
void get_coord_constraints(double &max,double &min);
void set_function(boost::function<double (double)> p_function);
void set_function_der(boost::function<double (double)> p_function);
bool compute(double max_func_error,unsigned int max_iter,double start_point,bool max=true);
bool compute(double max_func_error,unsigned int max_iter,double start_point,bool &beyond_limits,bool max=true);
double get_value(void);
double get_coordinate(void);
~CGradient();
......
......@@ -47,13 +47,13 @@ void CGradient::set_function_der(boost::function<double (double)> p_function)
this->p_function_der=p_function;
}
bool CGradient::compute(double max_func_error,unsigned int max_iter,double start_point,bool max)
bool CGradient::compute(double max_func_error,unsigned int max_iter,double start_point,bool &beyond_limits,bool max)
{
unsigned int iteration=0;
double gamma=1.0,func_value,func_der_value,prev_func_value,prev_func_der_value;
double coord,prev_coord,coord_inc;
bool beyond_limits=false;
beyond_limits=false;
coord=start_point;
func_value=this->p_function(coord);
func_der_value=this->p_function_der(coord);
......@@ -68,7 +68,7 @@ bool CGradient::compute(double max_func_error,unsigned int max_iter,double start
coord+=coord_inc;
else
coord-=coord_inc;
if(coord>this->max_coord)
if((coord-this->max_coord)>max_func_error)
{
coord=this->max_coord;
this->coordinate=coord;
......@@ -76,7 +76,7 @@ bool CGradient::compute(double max_func_error,unsigned int max_iter,double start
beyond_limits=true;
break;
}
else if(coord<this->min_coord)
else if((coord-this->min_coord)<-max_func_error)
{
coord=this->min_coord;
this->coordinate=coord;
......@@ -93,7 +93,7 @@ bool CGradient::compute(double max_func_error,unsigned int max_iter,double start
}while((fabs(func_value-prev_func_value)>max_func_error || fabs(coord-prev_coord)>0.001) && iteration<max_iter);
this->coordinate=coord;
this->value=func_value;
if(iteration==max_iter || beyond_limits)
if(iteration==max_iter)
return false;
else
return true;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment