2020-12-12 22:45:43 +08:00
|
|
|
{
|
|
|
|
"cells": [
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"# Compute the jacobian numerically\n",
|
|
|
|
"\n",
|
|
|
|
"link: --> http://www.maths.lth.se/na/courses/FMN081/FMN081-06/lecture7.pdf\n",
|
|
|
|
"\n",
|
|
|
|
"Often the Jacobian is not **analytically** available and it has to be computed numerically.\n",
|
|
|
|
"It can be computed column wise by finite differences:\n",
|
|
|
|
"\n"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2020-12-14 22:09:49 +08:00
|
|
|
"execution_count": 19,
|
2020-12-12 22:45:43 +08:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"import numpy as np\n",
|
|
|
|
"\n",
|
2020-12-13 02:15:08 +08:00
|
|
|
"# #CONTINUOUS\n",
|
|
|
|
"# def f(x,u):\n",
|
|
|
|
"# \"\"\"\n",
|
|
|
|
"# :param x:\n",
|
|
|
|
"# :param u:\n",
|
|
|
|
"# \"\"\"\n",
|
|
|
|
"# xx = x[0]\n",
|
|
|
|
"# xy = x[1]\n",
|
|
|
|
"# v = x[2]\n",
|
|
|
|
"# theta =x[3]\n",
|
|
|
|
" \n",
|
|
|
|
"# a = u[0]\n",
|
|
|
|
"# delta = u[1]\n",
|
|
|
|
" \n",
|
|
|
|
"# L=0.3\n",
|
|
|
|
" \n",
|
|
|
|
"# #vector of ackerman equations\n",
|
|
|
|
"# return np.array([\n",
|
|
|
|
"# np.cos(theta)*v,\n",
|
|
|
|
"# np.sin(theta)*v,\n",
|
|
|
|
"# a,\n",
|
|
|
|
"# v*np.arctan(delta)/L\n",
|
|
|
|
"# ])\n",
|
|
|
|
"\n",
|
|
|
|
"#DISCRETE\n",
|
|
|
|
"def f(x, u, dt=0.1):\n",
|
2020-12-12 22:45:43 +08:00
|
|
|
" \"\"\"\n",
|
|
|
|
" :param x:\n",
|
|
|
|
" :param u:\n",
|
|
|
|
" \"\"\"\n",
|
|
|
|
" xx = x[0]\n",
|
|
|
|
" xy = x[1]\n",
|
|
|
|
" v = x[2]\n",
|
|
|
|
" theta =x[3]\n",
|
|
|
|
" \n",
|
|
|
|
" a = u[0]\n",
|
|
|
|
" delta = u[1]\n",
|
|
|
|
" \n",
|
|
|
|
" L=0.3\n",
|
|
|
|
" \n",
|
|
|
|
" #vector of ackerman equations\n",
|
|
|
|
" return np.array([\n",
|
2020-12-13 02:15:08 +08:00
|
|
|
" xx + np.cos(theta)*v*dt,\n",
|
|
|
|
" xy + np.sin(theta)*v*dt,\n",
|
|
|
|
" v + a*dt,\n",
|
|
|
|
" theta + v*np.arctan(delta)/L*dt\n",
|
2020-12-12 22:45:43 +08:00
|
|
|
" ])\n",
|
|
|
|
"\n",
|
2020-12-14 22:09:49 +08:00
|
|
|
"def Jacobians(f,x,u,epsilon=1e-4):\n",
|
2020-12-12 22:45:43 +08:00
|
|
|
" \"\"\"\n",
|
|
|
|
" :param f:\n",
|
|
|
|
" :param x:\n",
|
|
|
|
" :param u:\n",
|
|
|
|
" \"\"\"\n",
|
|
|
|
" \n",
|
2020-12-13 02:15:08 +08:00
|
|
|
" jac_x = np.zeros((4,4))\n",
|
2020-12-14 22:09:49 +08:00
|
|
|
" jac_u = np.zeros((4,2))\n",
|
2020-12-12 22:45:43 +08:00
|
|
|
" \n",
|
2020-12-14 22:09:49 +08:00
|
|
|
" perturb_x = np.eye(4)*epsilon\n",
|
|
|
|
" perturb_u = np.eye(2)*epsilon\n",
|
2020-12-12 22:45:43 +08:00
|
|
|
" \n",
|
2020-12-14 22:09:49 +08:00
|
|
|
" #each row is state vector where one variable has been perturbed\n",
|
|
|
|
" x_perturbed_plus = np.tile(x,(4,1))+perturb_x\n",
|
|
|
|
" x_perturbed_minus = np.tile(x,(4,1))-perturb_x\n",
|
2020-12-12 22:45:43 +08:00
|
|
|
" \n",
|
2020-12-14 22:09:49 +08:00
|
|
|
" #each row is state vector where one variable has been perturbed\n",
|
|
|
|
" u_perturbed_plus = np.tile(u,(2,1))+perturb_u\n",
|
|
|
|
" u_perturbed_minus = np.tile(u,(2,1))-perturb_u\n",
|
2020-12-12 22:45:43 +08:00
|
|
|
" \n",
|
2020-12-14 22:09:49 +08:00
|
|
|
" for i in range(x.shape[0]):\n",
|
|
|
|
" \n",
|
|
|
|
" #each coloumn of the jac is given by perturbing a variable\n",
|
|
|
|
" jac_x[:,i]= (f(x+perturb_x[i,:], u)-f(x-perturb_x[i,:], u))/2*epsilon\n",
|
2020-12-13 02:15:08 +08:00
|
|
|
" \n",
|
2020-12-14 22:09:49 +08:00
|
|
|
" for i in range(u.shape[0]):\n",
|
2020-12-13 02:15:08 +08:00
|
|
|
" \n",
|
2020-12-14 22:09:49 +08:00
|
|
|
" #each coloumn of the jac is given by perturbing a variable\n",
|
|
|
|
" jac_u[:,i]= (f(x, u+perturb_u[i,:])-f(x, u-perturb_u[i,:]))/2*epsilon\n",
|
|
|
|
"\n",
|
2020-12-13 02:15:08 +08:00
|
|
|
" return jac_x, jac_u\n",
|
2020-12-12 22:45:43 +08:00
|
|
|
" "
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2020-12-14 22:09:49 +08:00
|
|
|
"execution_count": 20,
|
2020-12-12 22:45:43 +08:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"text/plain": [
|
2020-12-13 02:15:08 +08:00
|
|
|
"(array([[1.00000000e-08, 0.00000000e+00, 1.00000000e-09, 0.00000000e+00],\n",
|
|
|
|
" [0.00000000e+00, 1.00000000e-08, 0.00000000e+00, 9.99999998e-10],\n",
|
|
|
|
" [0.00000000e+00, 0.00000000e+00, 1.00000000e-08, 0.00000000e+00],\n",
|
|
|
|
" [0.00000000e+00, 0.00000000e+00, 6.57985199e-10, 1.00000000e-08]]),\n",
|
|
|
|
" array([[0.0000000e+00, 0.0000000e+00],\n",
|
|
|
|
" [0.0000000e+00, 0.0000000e+00],\n",
|
|
|
|
" [1.0000000e-09, 0.0000000e+00],\n",
|
|
|
|
" [0.0000000e+00, 3.2051282e-09]]))"
|
2020-12-12 22:45:43 +08:00
|
|
|
]
|
|
|
|
},
|
2020-12-14 22:09:49 +08:00
|
|
|
"execution_count": 20,
|
2020-12-12 22:45:43 +08:00
|
|
|
"metadata": {},
|
|
|
|
"output_type": "execute_result"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
|
|
|
"#starting condition\n",
|
2020-12-13 02:15:08 +08:00
|
|
|
"x=np.array([0,0,1,0])\n",
|
|
|
|
"u=np.array([1,0.2])\n",
|
2020-12-12 22:45:43 +08:00
|
|
|
"\n",
|
2020-12-14 22:09:49 +08:00
|
|
|
"Jacobians(f,x,u)"
|
2020-12-12 22:45:43 +08:00
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"metadata": {
|
|
|
|
"kernelspec": {
|
|
|
|
"display_name": "Python 3",
|
|
|
|
"language": "python",
|
|
|
|
"name": "python3"
|
|
|
|
},
|
|
|
|
"language_info": {
|
|
|
|
"codemirror_mode": {
|
|
|
|
"name": "ipython",
|
|
|
|
"version": 3
|
|
|
|
},
|
|
|
|
"file_extension": ".py",
|
|
|
|
"mimetype": "text/x-python",
|
|
|
|
"name": "python",
|
|
|
|
"nbconvert_exporter": "python",
|
|
|
|
"pygments_lexer": "ipython3",
|
2020-12-14 22:09:49 +08:00
|
|
|
"version": "3.8.5"
|
2020-12-12 22:45:43 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"nbformat": 4,
|
|
|
|
"nbformat_minor": 4
|
|
|
|
}
|