C++ (Qt)#include <iostream>#include <fstream>#include <vector>#include <cmath>#include <random> #include <specmath/minsearch.h>#include <specmath/grid.h> constexpr double pi = M_PI;constexpr size_t Sample_Size = 1000; int main(){ specmath::grid1d<double> grid(0.0, 2*pi, Sample_Size); std::random_device rd; std::mt19937 gen(rd()); const double dispersion = 0.2; std::normal_distribution<double> dist(0.0, dispersion); const double A = 1.0; // Amplitude const double omega = 1.0; // frequency grid.apply([&](double x) { // generate random data return A * sin(omega * x) + dist(gen); }); specmath::minsearch<double> minsearch(1.0, 10); std::vector<double> x = {0.0, 0.0}; // init point double sqr_sigma = minsearch.find_minimum([&](const std::vector<double> & v) { auto it = grid.first_point(); // x = 0.0, .. 2*pi double res = 0.0; double m_A = v[0]; double m_omega = v[1]; for (const auto & val : grid) { double xi = *(it++); double yi = val; double d = m_A * sin(m_omega * xi) - yi; res += d*d; } return res; }, x, specmath::breaker<double>(1e-6)); std::cout << "Amplitude = " << x[0] << " omega = " << x[1] << std::endl; std::cout << "sqr_sigma = " << sqr_sigma << std::endl; { std::ofstream out("data.txt"); grid.serialize(out); } return 0;}