% MATLAB template file for solving a set of coupled initial value ordinary
% differential equations of the form
%   dz1/dt = f1(t, z1, z2, z3, ..., zn); z1(0) = c1
%   dz2/dt = f2(t, z1, z2, z3, ..., zn); z2(0) = c2
%              .
%              .
%              .
%   dzn/dt = fn(t, z1, z2, z3, ..., zn); zn(0) = cn
% where c1 through cn are known constants and where the final value of t is
% known.
%
% See Supplemental Unit S8 of "A First Course on Kinetics and Reaction
% Engineering" and/or the MATLAB documentation for additional information
% or if questions arise.
%
% Carl R. F. Lund, 4/22/13
%   last rev. 6/19/13
%
% Returned quantities
%   t_f (scalar) - the value of the independent variable at which the
%       values of the dependent variables were determined
%   z (column vector) - values of z1, z2, ..., zn found by integrating the
%       differential equations from the specified initial conditions to the
%       value of the independent variable returned as t_f
%
% This file requires four modifications before each use; the locations
% where editing is required are indicated by the comment "% EDIT HERE"
%
function [t_f,z] = SolvIVDifI
    % Known quantities and constants (in consistent units)
% EDIT HERE (Required modification 1 of 4):
    % define universal and problem-specific constants here

	% Function that evaluates the ODEs
    function dzdt = odeqns(t,z)
% EDIT HERE (Required modification 2 of 4):
        % t is the value of the independent variable
        % z is a vector containing the values of the dependent variables
        dzdt = [
            % Evaluate dz1/dt = f1(t, z1, z2, z3, ..., zn) here
			% Evaluate dz2/dt = f2(t, z1, z2, z3, ..., zn) here
            % and so on through fn, one per line
        ];
    end % of internal function odeqns
        
    % Initial and final values
% EDIT HERE (Required modification 3 of 4):
    t0 = ;% insert the initial value of independent variable here
    z0 = [
        % insert the initial values of dependent variables z1 here
        % insert initial values for z2, z3, ..., one per line
    ];
    tf = ; % insert the final value of the independent variable here
    
    % Solve the ODEs
    [t,zz] = ode45(@odeqns,[t0 tf],z0);
% NOTE: For stiff equations replace ode45 with ode15s

    % Extract the final values of the independent and dependent variables
	last_value = length(t);
	t_f = t(last_value); % final value of the independent variable
	z = zz(last_value,:); % final values of the dependent variables
	
% EDIT HERE (Required modification 4 of 4):
    % Enter code to calculate any other desired quantities using the 
    % results contained in z. (Alternatively, z will be returned when this
    % template file terminates; other quantities can then be calculated
    % at the MATLAB command prompt using the returned values
    
end % of SolvIVDifI.m
