smooth_feedback
Control and estimation on Lie groups
Loading...
Searching...
No Matches
dyn_error.hpp
Go to the documentation of this file.
1// Copyright (C) 2022 Petter Nilsson. MIT License.
2
3#pragma once
4
10#include <Eigen/Core>
11
12#include "mesh.hpp"
13
14namespace smooth::feedback {
15
28Eigen::VectorXd
29mesh_dyn_error(auto && f, const MeshType auto & m, const double t0, const double tf, auto && xfun, auto && ufun)
30{
31 using smooth::utils::zip;
32 static constexpr auto Nx = std::invoke_result_t<decltype(xfun), double>::SizeAtCompileTime;
33
34 static_assert(Nx > 0, "Static size required");
35
36 const auto N = m.N_ivals();
37
38 Eigen::VectorXd ival_errs(N);
39
40 // for each interval
41 for (auto ival = 0u, M = 0u; ival < N; M += m.N_colloc_ival(ival), ++ival) {
42 const auto Kext = m.N_colloc_ival(ival);
43
44 // evaluate xs and F at those points
45 Eigen::Matrix<double, Nx, -1> Fval(Nx, Kext + 1);
46 Eigen::Matrix<double, Nx, -1> Xval(Nx, Kext + 1);
47 for (const auto & [j, tau] : zip(std::views::iota(0u, Kext + 1), m.interval_nodes(ival))) {
48 const double tj = t0 + (tf - t0) * tau;
49
50 // evaluate x and u values at tj using current degree polynomials
51 const auto Xj = xfun(tj);
52 const auto Uj = ufun(tj);
53
54 // evaluate right-hand side of dynamics at tj
55 Fval.col(j) = f(tj, Xj, Uj);
56
57 // store x values for later comparison
58 Xval.col(j) = Xj;
59 }
60
61 // "integrate" system inside interval
62 const Eigen::MatrixXd Xval_est =
63 Xval.col(0).replicate(1, Kext) + (tf - t0) * Fval.leftCols(Kext) * m.interval_intmat(ival);
64
65 // absolute error in interval
66 Eigen::VectorXd e_abs = (Xval_est - Xval.rightCols(Kext)).colwise().norm();
67 Eigen::VectorXd e_rel = e_abs / (1. + Xval.rightCols(Kext).colwise().norm().maxCoeff());
68
69 // mex relative error on interval
70 ival_errs(ival) = e_rel.maxCoeff();
71 }
72
73 return ival_errs;
74}
75
76} // namespace smooth::feedback
MeshType is a specialization of Mesh.
Definition: mesh.hpp:488
Eigen::VectorXd mesh_dyn_error(auto &&f, const MeshType auto &m, const double t0, const double tf, auto &&xfun, auto &&ufun)
Calculate relative dynamics errors for each interval in mesh.
Definition: dyn_error.hpp:29
Refinable Legendre-Gauss-Radau mesh of time interval [0, 1].