Re: Hessian Estimation NOT only for Minimization Problems
Posted: February 19th, 2024, 5:19 pm
Hi,
I was able to use WinZip to unzip the .7z without any hassle (but Winzip is a 7-day trial).
Code compiled in one go without warnings.
A good 101 test case is based on the yugely popular thread (50 shades of [$]e^5[$]).
viewtopic.php?t=79451
The code is
I was able to use WinZip to unzip the .7z without any hassle (but Winzip is a 7-day trial).
Code compiled in one go without warnings.
A good 101 test case is based on the yugely popular thread (50 shades of [$]e^5[$]).
viewtopic.php?t=79451
The code is
Code: Select all
#include <cmath>
#include "nl2sol.h"
#include <iostream>
// DJD 2024, compiled under C++20 and Visual Studio 2019
/* Questions
1. how to tweak performance parameters.
2. vector case + Jacobian stuff.
*/
using value_type = double;
// 50 other ways to compute exp(5)
// https://forum.wilmott.com/viewtopic.php?t=79451
value_type objFunc(value_type x)
{ // x ~ 148.413 (accurate to 3 decimal places)
value_type a = 5.0 - std::log(x);
return a * a;
}
// V2: try std::function
class MyFunctor : public NL2SOL::Functor
{
public:
bool operator()(const value_type* x, std::size_t n_x, value_type* f, std::size_t n_f)
{ // Maybe a signature w/o pointers?
*f = objFunc(*x);
return true;
}
};
void Statistics(const NL2SOL& solver)
{
std::cout << "Return code: " << solver.ReturnCode << '\n'; // 6
std::cout << "Achieved accuracy: " << solver.AchievedAccuracy<< '\n';
std::cout << "RMS: " << solver.RMS << '\n';
std::cout << "#iterations: " << solver.Iterations << '\n';
std::cout << "# function evaluations: " << solver.FunctionEvaluations << '\n';
}
int main()
{
std::cout << "NL2SOL" << '\n';
// tweak tolerances
value_type argTol = 1.0e-3;
value_type funTol = 1.0e-3;
// tweak tolerances
NL2SOL ns;
NL2SOL ns2(argTol, funTol);
value_type x;
std::size_t n_x = 1;
std::size_t n_f = 1;
MyFunctor func;
value_type result = ns.tryToSolve(func, &x, n_x,n_f);
std::cout << "And the answer is: " << x << '\n'; // gives 148.408
std::cout << std::exp(5.0) << '\n'; // 148.413
Statistics(ns);
MyFunctor func2;
value_type result2 = ns2.tryToSolve(func2, &x, n_x, n_f);
std::cout << "And the answer is: " << x << '\n'; // gives 148.402
std::cout << std::exp(5.0) << '\n'; // 148.413
Statistics(ns2);
}