function  [DXDT,MARKERPOS,MARKERV,JRF,MINV,XNEW] = osim(K,X,U)
% OSIM creates an interface for forward simulation of OpenSim models in MATLAB.
% 
% OSIM(DT,ACC,FILENAME1,FILENAME2,...FILENAMEN) initializes N models,
% each described by an .osim file.  DT sets the length of the
% integrator's timestep (see below), and ACC sets the accuracy of the
% integration step.
% 
% DXDT = OSIM(K,X,U) calculates DXDT, the time derivative of the state X
% in model K, given the control input U.  Different models may have
% different number of states, so if size(X,1) doesn't match the number
% of state dimensions of model K, an error results.  The same holds for
% size(U,1).  If X has more than one column, size(X,2) should match
% size(U,2), and then every column of X is evaluated according to the
% corresponding column of U, and size(DXDT) equals size(X).
% 
% MARKERPOS = OSIM(K,X) outputs the positions of the markers specified
% in model K.  MARKERPOS is a 3xM matrix, where M is the number of
% markers.  If X has C columns, MARKERPOS is a multidimensional matrix
% of dimensions [3 M C].
% 
% [MARKERPOS,MARKERV] = OSIM(K,X) outputs the positions and velocities
% of the markers specified in model K.  MARKERV has the same dimensions
% as MARKERPOS.
% 
% [DXDT,MARKERPOS] = OSIM(K,X,U) outputs both the time derivative of the
% state and the marker positions.
% 
% [DXDT,MARKERPOS,MARKERV] = OSIM(K,X,U) outputs both time derivative of
% the state, marker positions and marker velocities.
% 
% [DXDT,MARKERPOS,MARKERV,JRF] = OSIM(K,X,U) also outputs the reaction
% forces at every joint as a 6xJxC matrix (where J is the number of
% joints, and C is the number of columns in X).  The order of the 6
% reactions are: three torques followed by three forces.  For more
% information on the calculation, please refer to
% SimbodyEngine.computeReactions.
% 
% [DXDT,MARKERPOS,MARKERV,JRF,XNEW] = OSIM(K,X,U) also outputs the
% state at time dt as integrated by the SimTK Runge-Kutta-Feldberg
% integrator. XNEW has as many columns as X.
% 
% [DXDT,MARKERPOS,MARKERV,JRF,XNEW] = OSIM(K,X,U,DT) integrates a
% timestep of length DT, overriding the default one specified at
% initiation.
%
% Example
%  dt=0.01;
%  osim(dt,1e-4,'arm26.osim');
%  q    = [0;0];            % angles
%  v    = [q;0;0];          % angular velocities (referred to in
% OpenSim code as U)
%  z1   = 0.1*ones(6,1);    % muscle activations
%  z2   = 0.5*ones(6,1);    % muscle fiber lengths
%  z    = [z1';z2'];        % "interlace" muscle parameters
%  x    = [q;v;z(:)];       % full state vector (referred to in OpenSim
% code as Y)
%  u    = 0.5*ones(6,1);    % control input
%  dxdt = osim(1,x,u);
%  xnew = x+dt*dxdt;        % Euler integration
%  % compare with SimTK integration:
%  [dummy1,dummy2,dummy3,dummy4,dummy5,xint] = osim(1,x,u);
%  dx   = xnew-xint
disp('The mex''ed osim library was not found.  Please refer to readme.txt for instructions')