Solving a system of equations using the solve function in a for loop?
Show older comments
I am attempting to design a closed loop heat excahnger system where a transfer fluid transfers heat from a high constant temperature source to a phase changing material, I am using the solve function to solve some equations simultaneously but I am getting an error everytime I try to run the loop
Note: any variable seen here other than what is being calculated for is predifined (I can provide them if that helps in finding a solution.
%% Charging loop
%initial conditions
Epcm(1)=0;
Ef(1)=0;
Eoil(1)=0;
Toil_PCM_to_EG(1)=20;
Toil_EG_to_PCM(1)=20;
TPCM(1)=20;
q(1)=0;
time_step=1;
for t=2:time_step:3600
syms Toil_EG_to_PCM(t)
eqn=mdoto.*Co.*(Toil_EG_to_PCM(t)-Toil_PCM_to_EG(t-time_step))==((UE).*AsE.*(TE-((mdoto.*Co)./(mdotex.*Ca)).*(Toil_EG_to_PCM(t)-Toil_PCM_to_EG(t-time_step))-Toil_PCM_to_EG(t-time_step))-(TE-Toil_EG_to_PCM(t)))./log((TE-((mdoto.*Co)./(mdotex.*Ca)).*(Toil_EG_to_PCM(t)-Toil_PCM_to_EG(t-time_step))-Toil_PCM_to_EG(t-time_step))./(TE-Toil_EG_to_PCM(t)));
S=solve(eqn,Toil_EG_to_PCM(t));
if TPCM(t)<Tmelt
syms q(t) TPCM(t) Toil_PCM_to_EG(t)
eq1=Toil_PCM_to_EG(t)==S-q(t)./(mdoto.*Co);
eq2=q(t)==(((S+Toil_PCM_to_EG(t))./2)-((TPCM(t)+TPCM(t-time_step))./2)/Rtp);
eq3=q(t).*time_step==mPCM.*Cpl.*(TPCM(t)+TPCM(t-time_step));
sol=solve([e1 eq2 eq3],[q(t) Toil_PCM_to_EG(t) TPCM(t)]);
qsol=sol.q(t);
osol=sol.Toil_PCM_to_EG(t);
psol=sol.TPCM(t);
Epcm(t)=qsol.*time_step +Epcm(t-time_step);
Ef(t)=0;
Eoil(t)=(mdoto.*Co.*(Toil_EG_to_PCM(t)-Toil_PCM_to_EG(t-time_step))-qsol).*time_step-Eoil(t-time_step);
else
if Ef(t)<mPCM*Lf
TPCM(t)=Tmelt;
syms q(t) TPCM(t)
eq1=Toil_PCM_to_EG(t)==S-q(t)./(mdoto.*Co);
eq2=q(t)==(((S+Toil_PCM_to_EG(t))./2)-(Tmelt)/Rtp);
sol=solve([e1 eq2 ],[q(t) Toil_PCM_to_EG(t) ]);
qsol=sol.q(t);
osol=sol.Toil_PCM_to_EG(t);
Epcm(t)=qsol.*time_step +Epcm(t-time_step);
Ef(t)=qsol.*time_step +Ef(t-time_step);
Eoil(t)=(mdoto.*Co.*(Toil_EG_to_PCM(t)-Toil_PCM_to_EG(t-time_step))-qsol)*time_step-Eoil(t-time_step);
else
syms q(t) TPCM(t) Toil_PCM_to_EG(t)
eq1=Toil_PCM_to_EG(t)==S-q(t)./(mdoto.*Co);
eq2=q(t)==(((S+Toil_PCM_to_EG(t))./2)-((TPCM(t)+TPCM(t-time_step))./2)/Rtp);
eq3=q(t).*time_step==mPCM.*Cps.*(TPCM(t)+TPCM(t-time_step));
sol=solve([e1 eq2 eq3],[q(t) Toil_PCM_to_EG(t) TPCM(t)]);
qsol=sol.q(t);
osol=sol.Toil_PCM_to_EG(t);
psol=sol.TPCM(t);
Epcm(t)=qsol.*time_step +Epcm(t-time_step);
Ef(t)=mPCM.*Lf;
Eoil(t)=(mdoto.*Co.*(Toil_EG_to_PCM(t)-Toil_PCM_to_EG(t-time_step))-qsol).*time_step-Eoil(t-time_step);
end
end
end
Answers (1)
Torsten
on 3 Nov 2022
Remove all the (t) in the symbolic variables, so use
syms q TPCM Toil_PCM_to_EG
instead of
syms q(t) TPCM(t) Toil_PCM_to_EG(t)
After solving, you can write the results in numerical arrays that you can index with t.
6 Comments
mohamad Saheli
on 4 Nov 2022
mohamad Saheli
on 4 Nov 2022
Torsten
on 4 Nov 2022
In your eqn expression, there are still the indexed expressions for Toil_EG_to_PCM and Toil_PCM_to_EG which are symbolic variables.
The t-indexed symbolic variables can not be part the equation.
For usual numeric arrays this is not a problem.
mohamad Saheli
on 6 Nov 2022
mohamad Saheli
on 6 Nov 2022
syms q TPCM Toil_PCM_to_EG mdoto Co Toil_EG_to_PCM Rtp
eq1=Toil_PCM_to_EG(t)==Toil_EG_to_PCM-q(t)./(mdoto.*Co);
Don't you see that you still have the t-indexing for the symbolic variables Toil_PCM_to_EG and q ?
I didn't check for other instances in your code.
And don't name the numerical variables in which you write results equal to the symbolic ones. E.g you have symbolic q and numerical q(t). Same for Toil_PCM_to_EG and maybe others.
Categories
Find more on Assumptions 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!