J = jacobian(f, x);J.sparsity(). spy_matlab('jsp.m');
import casadi.*
n=3;nx=n*3+(n-1)*3;nu=3;
p0x=0;p0y=0;p0z=0;
fv=SX.sym('fv',nx+nu,1);
k=0.1;lr=0.55;m=0.45;g=9.81;
xend=1;yend=0;zend=0;
px=fv(1:n,1);py=fv(n+1:2*n,1);pz=fv(2*n+1:3*n,1);vx=fv(3*n+1:4*n-1,1);vy=fv(4*n:5*n-2,1);vz=fv(5*n-1:6*n-3,1);ux=fv(nx+1);uy=fv(nx+2);uz=fv(nx+3);
norm=SX(n,1);Fx=SX(n,1);Fy=SX(n,1);Fz=SX(n,1);ax=SX(n-1,1);ay=SX(n-1,1);az=SX(n-1,1);
norm(1)= ((px(1)-p0x)^2+(py(1)-p0y)^2+(pz(1)-p0z)^2)^0.5 ;Fx(1)=(px(1)-p0x)*k*(n-lr/norm(1));Fy(1)=(py(1)-p0y)*k*(n-lr/norm(1));Fz(1)=(pz(1)-p0z)*k*(n-lr/norm(1));for i=2:n norm(i)= ((px(i)-px(i-1))^2+(py(i)-py(i-1))^2+(pz(i)-pz(i-1))^2)^0.5 ; Fx(i)= (px(i)-px(i-1))*k*(n-lr/norm(i)) ; Fy(i)= (py(i)-py(i-1))*k*(n-lr/norm(i)) ; Fz(i)= (pz(i)-pz(i-1))*k*(n-lr/norm(i)) ; ax(i-1,1)= (Fx(i)-Fx(i-1))*n/m ; ay(i-1,1)= (Fy(i)-Fy(i-1))*n/m ; az(i-1,1)= (Fz(i)-Fz(i-1))*n/m-g ;end
xdot=[vx;ux;vy;uy;vz;uz;ax;ay;az];
M = 4; DT = Ts/M;
X=fv(1:nx);U=fv(nx+1:nx+nu);f = Function('f', {X,U}, {xdot},{'X','U'},{'xdot'}); fv_MX=MX.sym('fv_MX',nx+nu,1);X=fv_MX(1:nx);U=fv_MX(nx+1:nx+nu);k1=f(X,U);k2 = f(X + DT/2 * k1, U);k3 = f(X + DT/2 * k2, U);k4 = f(X + DT * k3, U);step=X+DT/6*(k1 +2*k2 +2*k3 +k4);
onestep=Function('onestep',{fv_MX},{step},{'fv_MX'},{'step'});X=fv_MX(1:nx);U=fv_MX(nx+1:nx+nu);for j=1:M X=onestep([X;U]);end
F_RK4=Function('F_RK4', {fv_MX}, {X}, {'fv_MX'}, {'xf'});Gw_RK4= F_RK4.jacobian('fv_MX','xf');
When you evaluate it in from CasADi, did you try to set the option "jit" to true for the Function instance? That will involve CasADi's just-in-time compiler (normally Clang).
Joel