slight rework of numerical jacobian functions

master
mcarfagno 2020-12-14 14:09:49 +00:00
parent 741f2c158d
commit 1ed479e965
1 changed files with 25 additions and 27 deletions

View File

@ -15,7 +15,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 31, "execution_count": 19,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -69,7 +69,7 @@
" theta + v*np.arctan(delta)/L*dt\n", " theta + v*np.arctan(delta)/L*dt\n",
" ])\n", " ])\n",
"\n", "\n",
"def J(f,x,u,epsilon=1e-4):\n", "def Jacobians(f,x,u,epsilon=1e-4):\n",
" \"\"\"\n", " \"\"\"\n",
" :param f:\n", " :param f:\n",
" :param x:\n", " :param x:\n",
@ -77,23 +77,28 @@
" \"\"\"\n", " \"\"\"\n",
" \n", " \n",
" jac_x = np.zeros((4,4))\n", " jac_x = np.zeros((4,4))\n",
" \n",
" for i in range(x.shape[0]):\n",
" perturb = np.zeros(x.shape[0])\n",
" perturb[i] = epsilon\n",
" \n",
" #each loumn of the jac is given by perturbing a variable\n",
" jac_x[:,i]= (f(x+perturb, u)-f(x-perturb, u))/2*epsilon\n",
" \n",
" \n",
" jac_u = np.zeros((4,2))\n", " jac_u = np.zeros((4,2))\n",
" \n", " \n",
" for i in range(u.shape[0]):\n", " perturb_x = np.eye(4)*epsilon\n",
" perturb = np.zeros(u.shape[0])\n", " perturb_u = np.eye(2)*epsilon\n",
" perturb[i] = epsilon\n",
" \n", " \n",
" #each loumn of the jac is given by perturbing a variable\n", " #each row is state vector where one variable has been perturbed\n",
" jac_u[:,i]= (f(x, u+perturb)-f(x, u-perturb))/2*epsilon\n", " x_perturbed_plus = np.tile(x,(4,1))+perturb_x\n",
" x_perturbed_minus = np.tile(x,(4,1))-perturb_x\n",
" \n",
" #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",
" \n",
" 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",
" \n",
" for i in range(u.shape[0]):\n",
" \n",
" #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", "\n",
" return jac_x, jac_u\n", " return jac_x, jac_u\n",
" " " "
@ -101,7 +106,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 32, "execution_count": 20,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -117,7 +122,7 @@
" [0.0000000e+00, 3.2051282e-09]]))" " [0.0000000e+00, 3.2051282e-09]]))"
] ]
}, },
"execution_count": 32, "execution_count": 20,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -127,15 +132,8 @@
"x=np.array([0,0,1,0])\n", "x=np.array([0,0,1,0])\n",
"u=np.array([1,0.2])\n", "u=np.array([1,0.2])\n",
"\n", "\n",
"J(f,x,u)" "Jacobians(f,x,u)"
] ]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
} }
], ],
"metadata": { "metadata": {
@ -154,7 +152,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.7.6" "version": "3.8.5"
} }
}, },
"nbformat": 4, "nbformat": 4,