Can not solve system of differential equations with ode45 taking into account continuity equation
Show older comments
I have a schematic of the following situation.

I have described the fluid velocity (c1 and c2) through the 2 pipe diameters, pressure p and volume of air in tank V, all in function of the time, with the following 5 differential equations:


where Q = c1 * S1Note that the sum of static and dynamic pressure is the total pressure and the total pressure is equal through the whole pipe. This can be described with the following continuity equation.

There are 6 variables that change in time and therefore need to be solved with the differential equations and the continuity equation: p1L, p1R, p, c1, c2 and V.
I have made the following code.
% Functions that calculate friction losses due to pipes
zeta1 = @(c1) (0.11*(abs_rough/d1 + 68./(c1*d1/kin_visc)).^0.25)*L1/d1 * 1.1;
zeta2 = @(c2) (0.11*(abs_rough/d2 + 68./(c2*d2/kin_visc)).^0.25)*L2/d2 * 1.1;
% Continuity equation
syms p1L p1R c1 c2
convgl = p1L + rho .* c1.^2./2 == p1R + rho .* c2.^2./2;
f_equation = matlabFunction(convgl, 'Vars', {p1L, p1R, c1, c2});
% Define differential equations
dPdt = @(t,p) data_debiet(end)*(1-(p_v./p))/(1-(p_v/p_a)).*-p./V_0;
dCdt1 = @(t,c1,p1L) ((p_a - p1L)./rho - g*(H_1 - H_0) - (c1.^2)/2*(1+zeta_1(c1)))/L1;
dCdt2 = @(t,c2,p1R,p) ((p1R - p)./rho - g*(H_2 - H_1) - (c2.^2)/2*(1+zeta_2(c2)))/L2;
dVdt = @(t,V,c1) -c1 * S1;
% Define initial conditions
p0 = p_a;
c0 = 1e-6;
V0 = V_0;
% Solve differential equations
[t, y] = ode45(@(t, y) [dPdt(t, y(3)); dCdt1(t, y(4), y(1)); dCdt2(t, y(5), y(2), y(3)); dVdt(t, y(6), y(4))], tspan1, [p0; p0; p0; c0; c0; V0], options1);
% Extract results from y
p1L = y(:, 1);
p1R = y(:, 2);
p = y(:, 3);
c1 = y(:, 4);
c2 = y(:, 5);
V = y(:, 6);
Assume that all the other variables are known constants. I have the following error message.
Error using odearguments
@(T,Y)[DPDT(T,Y(3));DCDT1(T,Y(4),Y(1));DCDT2(T,Y(5),Y(2),Y(3));DVDT(T,Y(6),Y(4))] returns a vector of length 4, but the length of initial conditions vector is 6. The vector returned by
@(T,Y)[DPDT(T,Y(3));DCDT1(T,Y(4),Y(1));DCDT2(T,Y(5),Y(2),Y(3));DVDT(T,Y(6),Y(4))] and the initial conditions vector must have the same number of elements.
Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
Error in Model_v4 (line 81)
[t, y] = ode45(@(t, y) [dPdt(t, y(3)); dCdt1(t, y(4), y(1)); dCdt2(t, y(5), y(2), y(3)); dVdt(t, y(6), y(4))], tspan1, [p0; p0; p0; c0; c0; V0], options1);
Answers (1)
Where do you specify the equations for p1L and p1R ?
In your vector of time derivatives
[dPdt(t, y(3)); dCdt1(t, y(4), y(1)); dCdt2(t, y(5), y(2), y(3)); dVdt(t, y(6), y(4))]
I only see equations for P, C1, C2 and V.
Even if you included the Bernoulli equation, one further equation had to be supplied.
13 Comments
Wout Suykerbuyk
on 1 May 2024
Torsten
on 1 May 2024
Please include your modified code.
Wout Suykerbuyk
on 1 May 2024
Edited: Walter Roberson
on 2 May 2024
The equation
c1 * S1 - c2 * S2 = 0
does not help.
On the contrary: Most probably, it will contradict the results of the two differential equations where you determine c1 and c2.
You have the Bernoulli equation in which p_1,L and p_1,R appear. But you need a second equation that relates p_1,L and p_1,R to close the system.
There are empirical correlations for pressure losses over regions where the tube radius changes. You should look them up in an engineering handbook (e.g. VDI Wärmeatlas).
What I don't understand is why you use time-dependent equations (p,c1,c2,V) together with stationary ones (Bernoulli equation, static pressure loss over a tube expansion). Isn't that inconsistent ?
By the way:
If you want to include algebraic equations in the system for ODE45 (like equations 5 and 6 of your system), you must modify the mass matrix M.
Usually, ODE45 solves a system of the form
M*y' = f(t,y)
where M is the identity matrix (pure system of ordinary differential equations).
If equations 5 and 6 are algebraic equations like in your case, you have to define
M = eye(6)
M(5,5) = 0;
M(6,6) = 0;
and pass M to ODE45 via the mass matrix option:
options = odeset('Mass',M)
Wout Suykerbuyk
on 2 May 2024
Torsten
on 2 May 2024
Wout Suykerbuyk
on 3 May 2024
As you can see in the video, a simplifying assumption is that there is a pressure jump from p_inlet to p_outlet at the position of pipe enlargement. Friction losses in the pipe are neglected, but you can include them in the Bernoulli equation together with the influence of gravity because of height differences. I think this stationary approach to compute the volume flow rate in steady-state would be a good starting point.
Concerning your code:
As I already wrote, c1 and c2 are determined by the differential equations dCdt1 and dCdt2. The continuity equation c1*S1 - c2*S2 cannot be used to determine a variable different from c1 or c2. Thus either you remove one of the differential equations and replace it by the continuity equation or you will get problems with your system. You definitly need an equation that computes p1.
Wout Suykerbuyk
on 8 May 2024
Wout Suykerbuyk
on 9 May 2024
Edited: Wout Suykerbuyk
on 9 May 2024
y0 = [P20,c10]; % Initial values for P2 and c1
[T,Y] = ode45(@fun,[0 10],y0)
function dy = fun(t,y)
% Given constants
V_0 = 12; % [m³]
p_a = 101325; % [Pa]
g = 9.81; % N/kg of m/s²
H_0 = 1; % m
H_1 = 4; % m
H_2 = 6; % m
L1 = 10; % m
L2 = 5; % m
d1 = 0.10; % m
d2 = 0.15; % m
abs_rough1 = 0.0002; % m
abs_rough2 = 0.0002; % m
kin_visc = 10^-6; % m²/s
rho = 1000; % kg/m³
S1 = pi*d1^2/4; % m²
S2 = pi*d2^2/4; % m²
p_v = 20000; % Pa
zeta1 = @(f1,K_exp) f1 * L1 / d1 + K_exp;
zeta2 = @(f2,K_bocht) f2 * L2 / d2 + 2 * K_bocht;
f1 = @(Re1) 0.11 * (abs_rough1/d1 + 68/Re1)^0.25;
f2 = @(Re2) 0.11 * (abs_rough2/d2 + 68/Re2)^0.25;
Re1 = @(c1) c1 * d1 / kin_visc;
Re2 = @(c2) c2 * d2 / kin_visc;
K_exp = @(f1) (1 + 0.8 * f1) * (1 - (d1/d2)^2)^2; % sudden widening
K_bocht = 0.45; % R/D = 1.5 van 90°
P2 = y(1);
c1 = y(2);
c2 = c1*S1/S2;
P1 = P2 + rho * g * (H_2 - H_1) + rho * c2^2 / 2 * (1 + zeta2(f2(Re2(c2)),K_bocht));
dP2 = data_debiet(end)*(1-(p_v./P2))/(1-(p_v/p_a)).*-P2./V_0;
dc1 = ((p_a - P1) - rho * g * (H_1 - H_0) - rho * c1^2 / 2 * (1 + zeta1(f1(Re1(c1)),K_exp(f1(Re1(c1)))))) / L1;
dy = [dP2;dc1];
end
Wout Suykerbuyk
on 13 May 2024
Categories
Find more on Particle & Nuclear Physics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



