% Unit 5 (MATLAB Course @ UAM)
% (c) Alexander Knebe

%==================================================
% Euler method for ordinary differential equations
%==================================================
clear all
format long

% physical constants/input block
%--------------------------------
H0 = 8005;    % m
p0 = 101325;  % pascals
h0 = 0;       % m
hN = 20000;     % m

% define g(p,h) specifying the differential equation
%----------------------------------------------------
g  = @(p,h)(-p/H0);


% integrate ODE defined by g using Euler method
%-----------------------------------------------
[p,h] = euler(g,h0,p0,hN,5);       % 5 integration steps
plot(h,p,'red'), hold on

[p,h] = euler(g,h0,p0,hN,10);      % 10 integration steps
plot(h,p,'blue')

[p,h] = euler(g,h0,p0,hN,100);     % 100 integration steps
plot(h,p,'green')

% overplot analytical solution
h = linspace(h0,hN,100);
plot(h,p0.*exp(-h./H0),'black:')

% label and legend
xlabel('h')
ylabel('p(h)')
title('barometric pressure')
legend('5 Euler steps','10 Euler steps','100 Euler steps','analytical solution','Location','NorthEast')