%% DDE with State-Dependent Delays
% This example shows how to use ddesd to solve a system of DDEs (delay differential equations) with state-dependent delays. This system of DDEs was used as a test problem by Enright and Hayashi [1].
% The system of equations is
% {y_1 }^{\prime } \left(t\right)=y_2 \left(t\right),
% {y_2 }^{\prime } \left(t\right)=-y_2 \left(e^{1-y_2 \left(t\right)} \right)\cdot y_2 {\left(t\right)}^2 \cdot e^{1-y_2 \left(t\right)} \ldotp
% The history functions for t\le 0\ldotp 1 are the analytical solutions
% y_1 \left(t\right)=\log \left(t\right),
% y_2 \left(t\right)=\frac{1}{t}\ldotp
% The time delays in the equations are only present in y terms.  The delays depend only on the state of the second component y_2 \left(t\right), so the equations form a system of state-dependent delay equations.
% To solve this system of equations in MATLAB, you need to code the equations, delays, and history before calling the delay differential equation solver ddesd, which is meant for systems with state-dependent delays. You either can include the required functions as local functions at the end of a file (as done here), or save them as separate, named files in a directory on the MATLAB path.
%
%% Code Delays
% First, write a function to define the time delays in the system. The only delay present in this system of equations is in the term-y_2 \left(e^{1-y_2 \left(t\right)} \right).
% function d = dely(t,y)
% d = exp(1 - y(2));
% end
% Note: All functions are included as local functions at the end of the example.
%% Code Equation
% Now, create a function to code the equations. This function should have the signature dydt = ddefun(t,y,Z), where:
% t is time (independent variable).
% y is the solution (dependent variable).
% Z(n,j) approximates the delays y_n \left(d\left(j\right)\right), where the delay d\left(j\right) is given by component j of dely(t,y).
% These inputs are automatically passed to the function by the solver, but the variable names determine how you code the equations. In this case:
% Z(2,1)\;\to y_2 \left(e^{1-y_2 \left(t\right)} \right)
% function dydt = ddefun(t,y,Z)
% dydt = [y(2);
%        -Z(2,1)*y(2)^2*exp(1 - y(2))];
% end
%
%% Code Solution History
% Next, create a function to define the solution history. The solution history is the solution for times t\le t_0.
% function v = history(t) % history function for t < t0
% v = [log(t);
%      1./t];
% end
%
%% Solve Equation
% Finally, define the interval of integration \left\lbrack t_0 \;\;t_f \right\rbrack and solve the DDE using the ddesd solver.

tspan = [0.1 5];
sol = ddesd(@ddefun, @dely, @history, tspan);

%
%% Plot Solution
% The solution structure sol has the fields sol.x and sol.y that contain the internal time steps taken by the solver and corresponding solutions at those times. (If you need the solution at specific points, you can use deval to evaluate the solution at the specific points.)
% Plot the two solution components against time using the history function to calculate the analytical solution within the integration interval for comparison.

ta = linspace(0.1,5);
ya = history(ta);

plot(ta,ya,sol.x,sol.y,'o')
legend('y_1 exact','y_2 exact','y_1 ddesd','y_2 ddesd')
xlabel('Time t')
ylabel('Solution y')
title('D1 Problem of Enright and Hayashi')

%
%% Local Functions
% Listed here are the local helper functions that the DDE solver ddesd calls to calculate the solution. Alternatively, you can save these functions as their own files in a directory on the MATLAB path.

function dydt = ddefun(t,y,Z) % equation being solved
dydt = [y(2); 
       -Z(2,1).*y(2)^2.*exp(1 - y(2))];
end
%-------------------------------------------
function d = dely(t,y) % delay for y
d = exp(1 - y(2));
end
%-------------------------------------------
function v = history(t) % history function for t < t0
v = [log(t); 
     1./t];
end
%-------------------------------------------

%% References
% [1] Enright, W.H. and H. Hayashi. “The Evaluation of Numerical Software for Delay Differential Equations.” In Proceedings of the IFIP TC2/WG2.5 working conference on Quality of numerical software: assessment and enhancement. (R.F. Boisvert, ed.). London, UK: Chapman & Hall, Ltd., pp. 179-193.
% Copyright 2018 The MathWorks, Inc.
%
%