function dotq = pendulum_variable_Cartesian (t, q)
% 長さが変化する単振り子の運動方程式（デカルト座標）
global len;
global amp;
global omega;
global mass;
global grav;
global nu;

x = q(1); y = q(2);
vx = q(3); vy = q(4);

l = len + amp*sin(omega*t);
dotl = amp*omega*cos(omega*t);
ddotl = -amp*omega^2*sin(omega*t);

R = sqrt(x*x + (y-len)*(y-len)) - l;

P = 1/sqrt(x*x + (y-len)*(y-len));
Rx = x*P; Ry = (y-len)*P;
Rxx = P - x^2*P^3; Ryy = P - (y-len)^2*P^3; Rxy = -x*(y-len)*P^3;

J = [Rxx,Rxy; Rxy,Ryy];
C = [vx, vy]*J*[vx; vy] - ddotl + ...
    2*nu*(Rx*vx + Ry*vy - dotl) + nu*nu*R;

A = [ mass, 0, -Rx;...
      0, mass, -Ry;...
      -Rx, -Ry,  0 ];
b = [ 0; -mass*grav; C ];
d = A\b;
dotvx = d(1);
dotvy = d(2);
dotq = [vx; vy; dotvx; dotvy];
end
