.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_build/auto_examples/convergence/TangentIntegrationvs_FD.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr__build_auto_examples_convergence_TangentIntegrationvs_FD.py: Valiadation of tangent integration alorithms using Finite Differences ===================================================================== .. GENERATED FROM PYTHON SOURCE LINES 7-8 Evaluation of relative quadrature error with the following parameters: .. GENERATED FROM PYTHON SOURCE LINES 8-36 .. code-block:: Python ndim = 2 A = np.random.random((ndim,ndim)) def fun(t,x): return np.dot(A,x) def gun(t,x): return np.dot(A,x)*np.dot(x,x) def gradfun(t,x,dx): return np.dot(A,dx) def gradgun(t,x,dx): dxr = np.asarray(dx).reshape(-1) resr = 2*np.dot(x,dxr)*np.dot(A,x) + np.dot(x,x)*np.dot(A,dxr) return resr.reshape(-1,1) plt.show() .. image-sg:: /_build/auto_examples/convergence/images/sphx_glr_TangentIntegrationvs_FD_001.png :alt: TangentIntegrationvs FD :srcset: /_build/auto_examples/convergence/images/sphx_glr_TangentIntegrationvs_FD_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 120-176 .. code-block:: Python plt.close() nint = 100 t_span = (0.,0.01) def int_fun(x): xx = x[:ndim].copy() vx = x[ndim:].copy() resx, resv = choreo.scipy_plus.ODE.ImplicitSymplecticIVP( fun , gun , t_span , xx , vx , rk , rk , nint = nint , ) return np.ascontiguousarray(np.concatenate((resx,resv)).reshape(-1)) def int_grad_fun_impl(x,dx): xx = x[:ndim].copy() vx = x[ndim:].copy() dxx = dx[:ndim].reshape(ndim,-1).copy() dxv = dx[ndim:].reshape(ndim,-1).copy() resx, resv, grad_resx, grad_resv = choreo.scipy_plus.ODE.ImplicitSymplecticIVP( fun , gun , t_span , xx , vx , rk , rk , grad_fun = gradfun , grad_gun = gradgun , grad_x0 = dxx , grad_v0 = dxv , nint = nint , ) return np.ascontiguousarray(np.concatenate((grad_resx,grad_resv)).reshape(-1)) plt.show() .. image-sg:: /_build/auto_examples/convergence/images/sphx_glr_TangentIntegrationvs_FD_002.png :alt: TangentIntegrationvs FD :srcset: /_build/auto_examples/convergence/images/sphx_glr_TangentIntegrationvs_FD_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 200-244 .. code-block:: Python plt.close() def int_grad_fun_expl(x,dx): xx = x[:ndim].copy() vx = x[ndim:].copy() dxx = dx[:ndim].reshape(ndim,-1).copy() dxv = dx[ndim:].reshape(ndim,-1).copy() resx, resv, grad_resx, grad_resv = choreo.scipy_plus.ODE.ExplicitSymplecticIVP( fun , gun , t_span , xx , vx , rk_expl , grad_fun = gradfun , grad_gun = gradgun , grad_x0 = dxx , grad_v0 = dxv , nint = nint , ) return np.ascontiguousarray(np.concatenate((grad_resx,grad_resv)).reshape(-1)) fig, ax = plt.subplots( figsize = figsize, dpi = dpi , ) for i, order in enumerate(orderlist): err = choreo.scipy_plus.test.compare_FD_and_exact_grad(int_fun,int_grad_fun_expl,xo,dx=dxo,epslist=epslist,order=order) plt.plot(epslist,err) ax.set_xscale('log') ax.set_yscale('log') plt.tight_layout() plt.show() .. image-sg:: /_build/auto_examples/convergence/images/sphx_glr_TangentIntegrationvs_FD_003.png :alt: TangentIntegrationvs FD :srcset: /_build/auto_examples/convergence/images/sphx_glr_TangentIntegrationvs_FD_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.115 seconds) .. _sphx_glr_download__build_auto_examples_convergence_TangentIntegrationvs_FD.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: TangentIntegrationvs_FD.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: TangentIntegrationvs_FD.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_