Solving simultaneous thermodynamic equations

10 views (last 30 days)
I am trying to find out Xb and Xl for a range of Temperatures (800-1400) while simultaneosly solving the two equations. I am having problems since I figured I would need to do it in a for loop to be able to get more temperatures into the mix. this is however causing errors since I tried defining the xbs, and xbl variables as an array with the same number of elements as a temperature array but it keeps saying reference to non existent field xbs.
tr=[800:50:1400];
xbs=sym('a',[1 length(tr)]);
xbl=sym('b',[1 length(tr)]);
Aol=-5000;
for i=1:length(tr)
gma=10000-10*tr;
gmb=12000-10*tr;
eqa=gma+(8.3144*tr(1,i)*log((1-xbl(1,i))/(1-xbs(1,i))))+Aol*(xbl(1,i)^2)==0
eqb=gmb+(8.3144*tr(1,i)*log((xbl(1,i)/xbs(1,i))))+Aol*((1-xbl(1,i))^2)==0
sol=solve([eqa,eqb],[xbs(1,i),xbl(1,i)])
xbs(1,i)=sol.xbs
sbl(1,i)=sol.xbl
end

Answers (1)

Pavan Guntha
Pavan Guntha on 17 Nov 2021
Hello Shreyas,
In the code attached, 'sol' is struct with fields 'a1' and 'b1'. There's no field named 'xbs' or 'xbl' in 'sol' due to which the error pops out stating reference to non existent field xbs. The modified code is as follows:
tr=[800:50:1400];
xbs=sym('a',[1 length(tr)]);
xbl=sym('b',[1 length(tr)]);
Aol=-5000;
gma=10000-10*tr; % Take this out of for loop since it's a one-time calculation.
gmb=12000-10*tr; % Take this out of for loop since it's a one-time calculation.
for i=1:length(tr)
eqa=gma(1,i)+(8.3144*tr(1,i)*log((1-xbl(1,i))/(1-xbs(1,i))))+Aol*(xbl(1,i)^2)==0;
eqb=gmb(1,i)+(8.3144*tr(1,i)*log((xbl(1,i)/xbs(1,i))))+Aol*((1-xbl(1,i))^2)==0;
sol=solve([eqa,eqb],[xbs(1,i),xbl(1,i)]);
fields = fieldnames(sol); % Get the field names from a structure.
xbs(1,i)= getfield(sol,fields{1}); % Use 'getfield' to get the value of a particular field from a structure.
xbl(1,i)= getfield(sol,fields{2});
end
Hope this helps!

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!