MATLAB Script:
clear
clc
%Densities
rho = [800 1000 1400];
%Depth from surface of liquid to each interface
heights = [5 7 10];
h = 0:0.1:heights(end);
%Atmospheric Pressure in Pa
Patm = 1.013e5;
%Acceleration due to gravity in m/s^2
g = 9.81;
for i = 1:length(h)
if h(i) <= heights(1)
P(i) = Patm + (rho(1)*g*h(i));
pos1 = i;
elseif h(i) > heights(1) && h(i) <= heights(2)
P(i) = P(pos1) + (rho(2)*g*(h(i)-h(pos1)));
pos2 = i;
elseif h(i) > heights(2)
P(i) = P(pos2) + (rho(3)*g*(h(i)-h(pos2)));
end
end
plot(h(1:pos1),P(1:pos1)/1000,'linewidth', 1.25)
hold on
plot(h(pos1:pos2),P(pos1:pos2)/1000,'linewidth', 1.25)
hold on
plot(h(pos2:end),P(pos2:end)/1000,'linewidth', 1.25)
xlabel 'Y (m)'
ylabel 'Pressure (KPa)'
legend Liquid1 Liquid2 Liquid3
grid on
Depth = [0 heights]';
Pressure = [Patm P(pos1) P(pos2) P(end)]'/1000;
Density = [NaN 800 1000 1400]';
T = table(Depth,Pressure,Density);
T.Properties.VariableNames = {'Depth_in_m' 'Pressure_in_KPa'
'Density'}
Output of above script:
Graph of Pressure against depth from the surface