%% Solve ODE with Multiple Initial Conditions
% For simple ODE systems with one equation, you can specify y0 as a vector containing multiple initial conditions. This technique creates a system of independent equations through scalar expansion, one for each initial value, and ode45 solves the system to produce results for each initial value.
%
% Create an anonymous function to represent the equation f\left(t,y\right)=-2y+2\;\cos \left(t\right)\;\sin \left(2t\right). The function must accept two inputs for t and y.

yprime = @(t,y) -2*y + 2*cos(t).*sin(2*t);

%
% Create a vector of different initial conditions in the range \left\lbrack -5,\;5\right\rbrack.

y0 = -5:5; 

%
% Solve the equation for each initial condition over the time interval \left\lbrack 0,3\right\rbrack using ode45.

tspan = [0 3];
[t,y] = ode45(yprime,tspan,y0);

%
% Plot the results.

plot(t,y)
grid on
xlabel('t')
ylabel('y')
title('Solutions of y'' = -2y + 2 cos(t) sin(2t), y(0) = -5,-4,...,4,5','interpreter','latex')

%
% This technique is useful for solving simple ODEs with several initial conditions. However, the technique also has some tradeoffs:
% You cannot solve systems of equations with multiple initial conditions. The technique only works when solving one equation with multiple initial conditions.
% The time step chosen by the solver at each step is based on the equation in the system that needs to take the smallest step. This means the solver can take small steps to satisfy the equation for one initial condition, but the other equations, if solved on their own, would use different step sizes. Despite this, solving for multiple initial conditions at the same time is generally faster than solving the equations separately using a for-loop.
%
% Copyright 2020 The MathWorks, Inc.