solve the system of linear equation
Show older comments
I want to solve the system of linear equations, but one of these equations is a diffrential equation(the equation of Q), and one of these equations is described in if structure. I want to know the method of solving these, and my question is 'how can I solve this system'?
My simplified equations are written in below. My variables are Tg Tc Tb Ti Tw Q Tp and italic underlined alphabet are constant.
1)Tg=CTc
2)Tc=ETg+FTb
3)Tb=HTc+ITp
4)Ti=LTp
5)Tw=NTp
6)dQ/dt=OTb+PTp+QTi+RTw
7)if Q<Q_sol
Tp=DQ
elseif (Q_sol<Q) && (Q<Q_liq)
Tp=T_mp;
elseif Q > Q_liq
Tp=BQ;
end
3 Comments
That T_mp in the middle of the definition of T_p does not appear to be consistent with anything else??
Unfortunately the following approaches do not work (yet?)
syms Tb Tc Tg Ti Tp Tw T_mp
syms B_ubar C_ubar D_ubar E_ubar F_ubar H_ubar I_ubar L_ubar N_ubar O_ubar P_ubar Q_ubar R_ubar
syms Q(t) Q_sol Q_liq
eqn1 = Tg == C_ubar * Tc
eqn2 = Tc == E_ubar * Tg + F_ubar * Tb
eqn3 = Tb == H_ubar * Tc + I_ubar * Tp
eqn4 = Ti == L_ubar * Tp
eqn5 = Tw == N_ubar * Tp
dQ = diff(Q,t)
eqn6 = dQ(t) == O_ubar * Tb + P_ubar * Tp + Q_ubar * Ti + R_ubar * Tw
Tp_defined = piecewise(Q(t) < Q_sol, D_ubar * Q(t), Q(t) < Q_liq, T_mp, B_ubar * Q(t))
eqns = [eqn1, eqn2, eqn3, eqn4, eqn5, eqn6].'
augmented = simplify(subs(eqns, Tp, Tp_defined))
sol = solve(augmented)
Choose3 = @(var,LB,V1,UB,V2,V3) ((-V2 + V3)*heaviside(var - UB) - V1 + V2)*heaviside(var - LB) + V1
Tp_defined2 = Choose3(Q(t), Q_sol, D_ubar * Q(t), Q_liq, T_mp, B_ubar * Q(t))
augmented2 = simplify(subs(eqns, Tp, Tp_defined2))
sol2 = solve(augmented2)
Hannah Mohebi
on 22 Apr 2022
Edited: Walter Roberson
on 22 Apr 2022
Walter Roberson
on 22 Apr 2022
Choose3 = @(var,LB,V1,UB,V2,V3) is implementing
- var < LB --> output is V1
- var >= LB and var < UB --> output is V2
- var >= UB --> output is V3
in terms of heaviside instead of piecewise(), except that I do not promise that it is correct exactly at the boundaries (especially since I forget to set the HeavisideAtOrigin preference.)
Your image shows a three part test, and in each case it sets two output variables. The way to handle that in the context of using Choose3 is to use two different Choose3 calls, once for each output variable, with the var and LB and UB parameters the same for the two calls, but using different V1, V2, V3 values.
The reason I attempted with heaviside is that I know from experience that in some cases dsolve() can handle boundaries expressed as heaviside, but that it can rarely handle boundaries expressed as piecewise()
A problem is that we are invoking solve() rather than dsolve(). dsolve() fails on the linear parts, and solve() can fail on the differential parts.
It might be worth experimenting with breaking the system up into the three different conditions, getting out three systems that do not have internal conditions expressed in them. I would then follow that by seeing if it was possible to solve() the non-differential parts of that, hopefully leading to a single differential equation each that you would dsolve() .
When I look at your equations, it is not obvious to me that they are continuous equations -- not obvious to me that they have been chosen to match boundary conditions for the limit to be the same from both sides of the boundary.
Answers (1)
Q0 = 356;
tspan = [0,100];
Qsol = 300;
Qliq = 400;
T_mp = ...;
C = ...;
E = ...;
F = ...;
H = ...;
I = ...;
L = ...;
N = ...;
O = ...;
P = ...;
Q = ...;
R = ...;
D = ...;
B = ...;
[t,Q] = ode45(@(t,Q)fun(t,Q,Qsol,Qliq,T_mp,C,E,F,H,I,L,N,O,P,Q,R,D,B),tspan,Q0);
function dQ = fun(t,Q,Qsol,Qliq,T_mp,C,E,F,H,I,L,N,O,P,Q,R,D,B)
if Q < Qsol
Tp = D*Q;
elseif Q >= Qsol && Q <= Qliq
Tp = T_mp;
elseif Q > Qliq
Tp = B*Q;
end
Tw = N*Tp;
Ti = L*Tp;
Tc = F*I/(-F*H+1-E*C)*Tp;
Tg = C*Tc;
Tb = I*Tp+H*Tc;
dQ = O*Tb+P*Tp+Q*Ti+R*Tw;
end
1 Comment
Walter Roberson
on 22 Apr 2022
if Q < Qsol
Tp = D*Q;
elseif Q >= Qsol && Q <= Qliq
Tp = T_mp;
elseif Q > Qliq
Tp = B*Q;
end
That is likely to either fail or give wrong answers. Using if inside of an ode function only works mathematically under one of two conditions:
- Only a single branch of the if is used for any one ode*() function call; or
- The branches have direct values at the boundaries that are equal, and first derivatives at the boundaries that are equal, and second derivatives at the boundaries that are equal.
continuity at least.
Because the value being tested, Q, is not time-based, it would probably be necessary to use event functions to detect crossing a boundary, and have the system stop integration at that point; you would then call ode45() again to continue on the other side of the boundary. Sounds pointless, I know, but the ode*() functions are doing internal interpolation, and that interpolation becomes invalid each time you cross a boundary.
Categories
Find more on Programming 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!


