Beispiel #1
0
    def test_cubic(self):

        # Interpolate with values and rates provided at [-1, 1] in tau space
        tau_given = [-1.0, 0.0, 1.0]
        tau_eval = np.linspace(-1, 1, 101)

        # In time space use the boundaries [-2, 2]
        dt_dtau = 4.0 / 2.0

        # Provide values for y = t**2 and its time-derivative
        y_given = [-8.0, 0.0, 8.0]
        ydot_given = [12.0, 0.0, 12.0]

        # Get the hermite matrices.
        Ai, Bi, Ad, Bd = hermite_matrices(tau_given, tau_eval)

        # Interpolate y and ydot at tau_eval points in tau space.
        y_i = np.dot(Ai, y_given) + dt_dtau * np.dot(Bi, ydot_given)
        ydot_i = (1.0 / dt_dtau) * np.dot(Ad, y_given) + np.dot(Bd, ydot_given)

        # Compute our function as a point of comparison.
        y_computed = (tau_eval * dt_dtau)**3
        ydot_computed = 3.0 * (tau_eval * dt_dtau)**2

        # Check results
        assert_almost_equal(y_i, y_computed)
        assert_almost_equal(ydot_i, ydot_computed)
Beispiel #2
0
    def phase_hermite_matrices(self, given_set_name, eval_set_name):
        """
        Compute the matrices mapping values at some nodes to values and derivatives at new nodes.

        Parameters
        ----------
        given_set_name : str
            Name of the set of nodes with which to perform the interpolation.
        eval_set_name : str
            Name of the set of nodes at which to evaluate the values and derivatives.

        Returns
        -------
        ndarray[num_eval_set, num_given_set]
            Matrix that maps values at given nodes to values at eval nodes.
            This is A_i in the equations above.
        ndarray[num_eval_set, num_given_set]
            Matrix that maps derivatives at given nodes to values at eval nodes.
            This is B_i in the equations above.
        ndarray[num_eval_set, num_given_set]
            Matrix that maps values at given nodes to derivatives at eval nodes.
            This is A_d in the equations above.
        ndarray[num_eval_set, num_given_set]
            Matrix that maps derivatives at given nodes to derivatives at eval nodes.
            This is A_d in the equations above.

        Notes
        -----
        The equation for Hermite interpolation of the values is:

        .. math::

            x_{eval} = \\left[ A_i \\right] x_{given}
                             + \\frac{dt}{d\\tau} \\left[ B_i \\right] \\dot{x}_{given}

        Hermite interpolation of the derivatives is performed as:

        .. math::

            \\dot{x}_{eval} = \\frac{d\\tau}{dt} \\left[ A_d \\right] x_{given}
                                   + \\left[ B_d \\right] \\dot{x}_{given}
        """
        Ai_list = []
        Bi_list = []
        Ad_list = []
        Bd_list = []

        for iseg in range(self.num_segments):
            i1, i2 = self.subset_segment_indices[given_set_name][iseg, :]
            indices = self.subset_node_indices[given_set_name][i1:i2]
            nodes_given = self.node_stau[indices]

            i1, i2 = self.subset_segment_indices[eval_set_name][iseg, :]
            indices = self.subset_node_indices[eval_set_name][i1:i2]
            nodes_eval = self.node_stau[indices]

            Ai_seg, Bi_seg, Ad_seg, Bd_seg = hermite_matrices(
                nodes_given, nodes_eval)

            Ai_list.append(Ai_seg)
            Bi_list.append(Bi_seg)
            Ad_list.append(Ad_seg)
            Bd_list.append(Bd_seg)

        Ai = block_diag(*Ai_list)
        Bi = block_diag(*Bi_list)
        Ad = block_diag(*Ad_list)
        Bd = block_diag(*Bd_list)

        return Ai, Bi, Ad, Bd
Beispiel #3
0
                 axes[0],
                 color='b',
                 scale=.25)
    plot_tangent(time[state_disc_idxs],
                 vy[state_disc_idxs],
                 vydot[state_disc_idxs],
                 axes[1],
                 color='b',
                 scale=.25)

    if i == 0:
        plt.savefig('lgl_animation_1.png')

    # Plot the interpolating polynomials
    t_dense = np.linspace(time[0], time[-1], 100)
    A_i, B_i, A_d, B_d = hermite_matrices(time[state_disc_idxs], t_dense)
    y_dense = (A_i.dot(y[state_disc_idxs]) + B_i.dot(ydot[state_disc_idxs]))
    vy_dense = A_i.dot(vy[state_disc_idxs]) + B_i.dot(vydot[state_disc_idxs])

    axes[0].plot(t_dense, y_dense, ms=None, ls=':')
    axes[1].plot(t_dense, vy_dense, ms=None, ls=':')

    # Plot the values and rates at the collocation nodes
    tau_s, _ = lgl(3)
    tau_disc = tau_s[0::2]
    tau_col = tau_s[1::2]
    A_i, B_i, A_d, B_d = hermite_matrices(tau_disc, tau_col)

    y_col = B_i.dot(ydot[state_disc_idxs]) * dt_dstau[col_idxs] + A_i.dot(
        y[state_disc_idxs])
    vy_col = B_i.dot(vydot[state_disc_idxs]) * dt_dstau[col_idxs] + A_i.dot(