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

%====================================================================
% comparing standard Euler vs. modified Euler vs. Runge-Kutta method
%====================================================================
clear all
close all
clc
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 modified Euler method
%--------------------------------------------------------
[p,h] = rk4(g,h0,p0,hN,5);          % 5 4th order Runge-Kutta integration steps
plot(h,p,'blue'), hold on

[p,h] = rk2(g,h0,p0,hN,5);          % 5 2nd order Runge-Kutta integration steps
plot(h,p,'red'), hold on

[p,h] = euler(g,h0,p0,hN,10);       % 10 Euler integration steps for comparison
plot(h,p,'green'), hold on

% 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 4th order RK steps','5 2nd order RK steps','10 Euler steps','analytical solution','Location','NorthEast')

