%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% The code below concerns the ODE: x' = 3 sin x with x(2) = 6. %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Minimalist solution of ODE with plot using MATLAB: 2 commands
[t z] = ode23('myfunction', 2, 5, 6);
plot( t, z )
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Put the two lines below in a file called myfunction.m before
% typing the two commands above.
% The first line of the file has to start with the word function.
function zdot = myfunction( t , z )
zdot = 3 * sin ( z );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% Use stuff above (to top) OR stuff below (to bottom), not both.%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% More involved solution of a first order differential equation
% and a plot of the solution - using MATLAB.
% By Andy Ruina (8/30/96)
% (This solves the same problem as the minimalist code above
% but with more comments, more accuracy,
% more flexibility, and a nicer looking plot.
% --- But it looks scarier at first glance.)
% The commands in this part of the solution (down to the line of %s)
% could be enterred as individual commands or, probably better,
% put together in a script file.
global C % This allows information to be passed to a function
% without a direct pass.
% It is convenient here because the MATLAB function
% ODE23 does not include provision for passing
% parameter values to the 'function' which defines
% the right hand side of the differential equation.
C = 3; % assign a value to the constant C
tzero = 2; % define the start value of t as 2
ffinal = 5; % final value of t as 5
zzero = 6; % value of z at tzero is 6, ie., z(tzero) = 6
tol = 1e-8; % tol is an optional argument of ODE23 that sets
% the accuracy of the solution.
% The default is 1e-6. Smaller values of
% tol make the solution more accurate and slower
% to compute. Actually, the solutions get less
% accurate (because of numerical roundoff) if
% tol is too small.
% One way of working is to use tol as something
% big (like tol = 1e-3) while debugging your work
% to make the execution time less.
% Then set tol to something smaller once all
% your code works.
% the actual solution is done in this one line that follows these
% comments.
% The result of this command is two arrays: t and z, where
% t contains a list of times (chosen by the computer but with
% the first element tzero and the last tfinal) and z an array
% of values of z at those times.
% Note that ode23 is not capitalized even though it is
% capitalized in MATLAB documentation (such as if you
% type >help ode23 at the MATLAB prompt.
[t z] = ode23 ('myfunction', tzero, tfinal, zzero, tol ); % the key line
plot( t, z) ; % the plot command in its simplest form is this
xlabel('time') % this adds the text 'time' under the x axis
ylabel('z') % similar to above for z axis
title('Andys plot of an ODE solution: 8/30/96') % adds a title to the plot
% Note you cannot use an apostrophe in the title as that
% is the quote character used to dilineate the text.
% That is why it says Andys instead of Andy's.
% To get a hard copy of the plot type the single word >print at the
% MATLAB prompt some time after you see the plot that you like
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Put the block of code below (down to the line of %s)
% in a file called myfunction.m
% The first line of the file has to start with the word function
function zdot = myfunction( t, z )
% MYFUNCTION evaluates the right hand side of the ODE that is being
% solved. Whatever name you use in the place of 'myfunction'
% must appear in 3 places:
% 1) in quotes as the first argument of ODE23
% 2) as the name of the file that contains the
% right hand side, namely 'myfunction.m', and
% 3) as the first word after the equal sign in the first
% line of the function that defines the right hand side
% of the differential equation.
% ODE23 calls this function many times when finding its numerical
% solution to the differential equation.
global C % this matches the global command in the script file
% above. The value C can thus be used in this function
% even though it is not expliticly passed.
zdot = C * sin ( z ); % this gives the rate of change of z
% in terms of its present value
end % so long as you assign a value to the variable to the
% left of the equal sign in the first line of the function
% definition (in this case zdot) you don't need an end
% statement.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%