%% Convert DAE System to Mass Matrix Form
%% 1

syms x1(t) x2(t) f(t, x1, x2) r m
eqs = [m*x2(t)*diff(x1(t), t) + m*t*diff(x2(t), t) == f(t, x1(t), x2(t)),...
       x1(t)^2 + x2(t)^2 == r^2];
vars = [x1(t), x2(t)];

%
%% 2

[M, F] = massMatrixForm(eqs, vars)

%
%% 3

syms Y1 Y2
M = subs(M, [vars, m, r, f], [Y1, Y2, 100, 1, @(t,x1,x2) t + x1*x2]);
F = subs(F, [vars, m, r, f], [Y1, Y2, 100, 1, @(t,x1,x2) t + x1*x2]);

%
%% 4

MM = matlabFunction(M, 'vars', {t, [Y1; Y2]});
FF = matlabFunction(F, 'vars', {t, [Y1; Y2]});

%
%% 5

opt = odeset('Mass', MM, 'InitialSlope', [0.005;0]);
ode15s(FF, [0,1], [0.5; 0.5*sqrt(3)], opt)

% Copyright 2012 The MathWorks, Inc.