Can not solve system of differential equations with ode45 taking into account continuity equation
    2 views (last 30 days)
  
       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 * S1
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);
0 Comments
Answers (1)
  Torsten
      
      
 on 30 Apr 2024
        
      Edited: Torsten
      
      
 on 30 Apr 2024
  
      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
  Torsten
      
      
 on 9 May 2024
				
      Edited: Torsten
      
      
 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
See Also
Categories
				Find more on Ordinary Differential Equations 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!







