%% YOURNAME (DUEDATE)
%
% *DO NOT CHANGE ANYTHING IN THIS SCRIPT AT ALL!*
%
% (Besides your name and due date above as needed.)
%
% Instead create a new function file CannonBall.m that
% contains the requested function CannonBall.  Then
% use this script to test whether your function works
% correctly.
%
%%
% <latex>
% % for Octave, or for Matlab published to LaTeX for proclatex
% \input{../setup.tex}
% </latex>
%
%% ORDINARY DIFFERENTIAL EQUATIONS, Exercise 1

if ~exist('___code___','var') ; clear ; end
format compact
more off


%% EXERCISE DESCRIPTION:
%
% Create a function |CannonBall| that can be used by
% |rk45| to solve the motion of a cannon ball in air.


%% Additional file: CannonBall.m
% <include>CannonBall.m</include>


%% TESTS:


%% See whether file CannonBall.m exists

disp('Checking for existence of ./CannonBall.m:')

status=exist('CannonBall')
if status==0
    disp('*** CannonBall does not exist!')
    error('    Check for typos in the name CannonBall!')
elseif status~=2
    error('*** CannonBall exists but is the wrong type!')
end

status=exist('./CannonBall.m')
if status~=2
    error('*** CannonBall exists but is not a file!')
end


%% Put in some arbitrary numbers

disp('Putting some trial numbers into your function:')

% call the function
result=CannonBall(10,[20 30 40 50]',6)

% make sure we get a 4 by 1 *column* vector
dims=size(result)
if dims(2) ~= 1
    error('*** The result is not a column vector!')
end
if ~isequal(dims,[4 1])
    error('*** This is not a 4-component column vector!')
end

% make sure the actual numbers are correct
correct=[40 50 -0.013483 -9.826854]';
if max(abs(result-correct)) > 0.000002
    disp('*** This should have produced the numbers:')
    disp(correct)
    disp('not:')
    disp(result)
    error('*** Fix the error in the function!')
end

%%
% The first test seems to be OK!


%% Try some other arbitrary numbers

disp('Putting other trial numbers into your function:')

% call the function
result=CannonBall(60,[50 40 30 20]',1)

% make sure we get a 4 by 1 *column* vector
dims=size(result)
if dims(2) ~= 1
    error('*** The result is not a column vector!')
end
if ~isequal(dims,[4 1])
    error('*** This is not a 4-component column vector!')
end

% make sure the actual numbers are correct
correct=[30 20 -0.034165 -9.832776]';
if max(abs(result-correct)) > 0.000002
    disp('*** This should have produced the numbers:')
    disp(correct)
    disp('not:')
    disp(result)
    error('*** Fix the error in the function!')
end

%%
% _Congratulations!_  Your function seems to _work_ OK.


%% See whether help works

disp('Trying if help works')

help CannonBall
