solving system of linear equations in for loop.

I have three linear equations and want to solve these equations. I attached my equations. In the image, those with a red circle around them are my variables ,and those with a blue circle around them are time varying parameters which are known and are matrices with 1 row and 6120 columns, other parameters are constant. I want to know how to solve these equations to obtain:
Tg=f(Tp)
Tc=f(Tp)
Tb=f(Tp)
This is my code:
clc; clear; close all;
Iinitial=[452.972 540.965 640.785 711.065 781.366 813.269 848.145 847.597 852.972 790.39 730.801 567.911 407.932 357.199 315.321 279.328 246.327];
T_e_initial=[292.8147 293.4165 294.0183 294.5613 295.1044 295.4712 295.8086 295.9698 296.1163 296.1747 296.2331 295.1019 294.0147 293.6912 293.3825 293.1912 293.0146];
time=[9 9.5 10 10.5 11 11.5 12 12.5 13 13.5 14 14.5 15 15.5 16 16.5 17];
xq = 9:0.0002777778:17;
I = interp1(time,Iinitial,xq);
T_e=interp1(time,T_e_initial,xq);
vf=2;
Fc=0.83;
N=length(I);
alpha_g=0.05;
delta_g=0.003;
landa_g=1;
h_ef=5.7+(3.8*vf);
delta_c=0.0003;
landa_c=148;
h_ge=((delta_g/landa_g)+(1/h_ef))^(-1);
U_gc=((delta_g/landa_g)+(delta_c/landa_c))^(-1);
alpha_c=0.8;
tha_g=0.91;
etha_ref=0.12;
beta=0.0045;
T_ref=293;
delta_b=0.0005;
landa_b=144;
U_cb=((delta_c/landa_c)+(delta_b/landa_b))^(-1);
alpha_b=0.4;
tha_c=0.09;
delta_PCM=0.05;
landa_PCM=0.2;
U_bp=((delta_b/landa_b)+(delta_PCM/landa_PCM))^(-1);
%%prelocating
T_g=zeros(1,N);
T_b=zeros(1,N);
T_c=zeros(1,N);
for i=1:N
syms T_p
Eq1=(alpha_g*I(i))+(h_ge*(T_e(i)-T_g(i)))+(U_gc*(T_c(i)-T_g(i)));
Eq2=Fc*alpha_c*tha_g*I(i)*((1/Fc)-etha_ref-(etha_ref*beta*T_ref)+(etha_ref*beta*T_c(i)))+(U_gc*(T_g(i)-T_c(i)))+(U_cb*(T_b(i)-T_c(i)));
Eq3=(((Fc*tha_c)-Fc+1)*alpha_b*tha_g*I(i))+(U_cb*(T_c(i)-T_b(i)))+(U_bp*(T_p-T_b(i)));
[T_g,T_c,T_b]=solve([Eq1,Eq2,Eq3],[T_g,T_c,T_b]);
end

7 commentaires

I ran my code and I faced this error. Would tou please help me?
Error using sym.getEqnsVars>checkVariables (line 92)
Second argument must be a vector of symbolic variables.
Error in sym.getEqnsVars (line 56)
checkVariables(vars);
Error in sym/solve>getEqns (line 429)
[eqns, vars] = sym.getEqnsVars(argv{:});
Error in sym/solve (line 226)
[eqns,vars,options] = getEqns(varargin{:});
Error in Untitled3 (line 41)
[T_g,T_c,T_b]=solve([Eq1,Eq2,Eq3],[T_g,T_c,T_b]);
Torsten
Torsten le 18 Juil 2022
It doesn't make sense to solve these three equations separately because they are not self-contained. They depend on an unknown T_p that has to be calculated from a fourth equation.
You will have to solve your complete set of 6 equations together as a system of differential-algebraic equations using ODE45. But I don't understand your differential equation with the distinction of cases and the different solution variables. If m_p is a further unknown, you have 6 equations for 7 unknowns ?
Thanks @Torsten
I attached The discretized form of Eq4. I have 6 equations with 6 unknowns, because equation 4 is a two-part equation, we must use if. If the first equation of the attached file is true, mp is known and Tp is unknown. If the second equation of the attached file is used, Tp is known and mp is unknown. Homogenous must be discretized to solve the differential equation. Also, to solve the differential equation, it must be discretized.
Torsten
Torsten le 18 Juil 2022
Modifié(e) : Torsten le 18 Juil 2022
So you know at the beginning of each simulation which equation has to be solved in (4) and this choice doesn't change during the computation ? T_p remains at a fixed value, namely T_mp which is given ?
According to the if condition, we know which equation has to be be solved in (4). I can not understand your question. Also yo are right, T_mp is given.
Torsten
Torsten le 18 Juil 2022
Modifié(e) : Torsten le 18 Juil 2022
Say we have T_p at t = 0 with is not equal to T_mp. So we solve the first differential equation for T_p. Say during the solution process - at t = 12 - T_p equals T_mp. Do we now switch to the second differential equation for m_p and keep T_p = T_mp for t > 12 ?
Say we have T_p at t = 0 which is equal to T_mp. So we solve the second differential equation for all t>0 and keep T_p = T_mp in the equations ?
All this is not clear to me.
Hi dear Toresten
Everything you said is correct.

Connectez-vous pour commenter.

Réponses (1)

T_g=zeros(1,N);
T_b=zeros(1,N);
T_c=zeros(1,N);
You create T_g T_b T_c as numeric variables.
syms T_p
That is your only symbolic variable, and it is a scalar.
Eq1=(alpha_g*I(i))+(h_ge*(T_e(i)-T_g(i)))+(U_gc*(T_c(i)-T_g(i)));
Eq2=Fc*alpha_c*tha_g*I(i)*((1/Fc)-etha_ref-(etha_ref*beta*T_ref)+(etha_ref*beta*T_c(i)))+(U_gc*(T_g(i)-T_c(i)))+(U_cb*(T_b(i)-T_c(i)));
Eq3=(((Fc*tha_c)-Fc+1)*alpha_b*tha_g*I(i))+(U_cb*(T_c(i)-T_b(i)))+(U_bp*(T_p-T_b(i)));
The first time through the loop (at least), T_g(i), T_b(i), T_c(i) are all numeric zeros.
Eq1 is independent of the symbolic variable T_p so it is effectively a constant the first time through. The same is true about Eq2, independent of all symbolic variables (at least in the first pass). Eq3 does depend upon T_p, in a relatively trivial way.
[T_g,T_c,T_b]=solve([Eq1,Eq2,Eq3],[T_g,T_c,T_b]);
At the time of the first pass through the loop, T_g, T_c, T_b are all numeric vectors of zeros, but they appear in the slot reserved for lists of variables to solve for. You have three equations in one unknown, and two of the equations are numeric constants. If the constants just happen to equal 0 exactly then it might be possible to solve for T_p... but not while the [T_g,T_c,T_b] are numeric.
In MATLAB, if you are not using inequalities you should have the same number of equations as you have variables being solved for.
If, hypothetically, your solve() worked, then you overwrite all of T_g, T_c, and T_b . Unless they happen to get overwritten with at least N values, you would have trouble in the next iteration of the loop when you try to use T_g(i) and so on.

1 commentaire

Would you please check my question in: https://www.mathworks.com/matlabcentral/answers/1762345-how-can-i-solve-system-of-equations-with-five-linear-equations-and-one-differential-equation

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Produits

Version

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by