MCPcopy Create free account
hub / github.com/bgrimstad/splinter / centralDifference

Function centralDifference

test/testingutilities.cpp:921–958  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

919}
920
921DenseMatrix centralDifference(const Function &approx, const DenseVector &x)
922{
923 DenseMatrix dx(1, x.size());
924
925 double h = 1e-6; // perturbation step size
926 double hForward = 0.5*h;
927 double hBackward = 0.5*h;
928
929 for (unsigned int i = 0; i < approx.getNumVariables(); ++i)
930 {
931 DenseVector xForward(x);
932// if (xForward(i) + hForward > variables.at(i)->getUpperBound())
933// {
934// hForward = 0;
935// }
936// else
937// {
938 xForward(i) = xForward(i) + hForward;
939// }
940
941 DenseVector xBackward(x);
942// if (xBackward(i) - hBackward < variables.at(i)->getLowerBound())
943// {
944// hBackward = 0;
945// }
946// else
947// {
948 xBackward(i) = xBackward(i) - hBackward;
949// }
950
951 double yForward = approx.eval(xForward);
952 double yBackward = approx.eval(xBackward);
953
954 dx(i) = (yForward - yBackward)/(hBackward + hForward);
955 }
956
957 return dx;
958}
959
960/*
961 * Checks that the hessian is symmetric across the diagonal

Callers 2

centralDifferenceMethod · 0.85
evalJacobianMethod · 0.85

Calls 1

evalMethod · 0.45

Tested by

no test coverage detected