solving system of linear equations in for loop.

2 Ansichten (letzte 30 Tage)
Hannah Mohebi
Hannah Mohebi am 18 Jul. 2022
Kommentiert: Hannah Mohebi am 19 Jul. 2022
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 Kommentare
Torsten
Torsten am 18 Jul. 2022
Bearbeitet: Torsten am 18 Jul. 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.
Hannah Mohebi
Hannah Mohebi am 19 Jul. 2022
Hi dear Toresten
Everything you said is correct.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 18 Jul. 2022
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 Kommentar
Hannah Mohebi
Hannah Mohebi am 19 Jul. 2022
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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by