function [inertia_matrix, torques] = open_link_2DOF_dynamics (theta, omega, param, gravity)
% calculating inertia matrix and generated torques
    theta1 = theta(1); theta2 = theta(2);
    omega1 = omega(1); omega2 = omega(2);
    l1 = param(1).length; lc1 = param(1).length_grav;
    m1 = param(1).mass; J1 = param(1).inertia;
    l2 = param(2).length; lc2 = param(2).length_grav;
    m2 = param(2).mass; J2 = param(2).inertia;
    
    inertia_matrix(1,1) = J1 + m1*lc1^2 + J2 + m2*(l1^2 + lc2^2 + 2*l1*lc2*cos(theta2));
    inertia_matrix(2,2) = J2 + m2*lc2*2;
    inertia_matrix(1,2) = J2 + m2*(lc2^2 + l1*lc2*cos(theta2));
    inertia_matrix(2,1) = inertia_matrix(1,2);
    
    h12 = m2*l1*lc2*sin(theta2);
    G1 = (m1*lc1 + m2*l1)*gravity*cos(theta1);
    G12 = m2*lc2*gravity*cos(theta1+theta2);
    torques = [  h12*omega2^2 + 2*h12*omega1*omega2 - G1 - G12;
                -h12*omega1^2 - G12 ];
end
