How to store answers of variables from for loop in structures

6 次查看(过去 30 天)
I need to solve some equations for symbolic variables for multiple set of data so for loop is used, while storing the output I am facing an issue in creating suitable structures. Here are the code statements. If anyone can please help.
syms a b c d e
x1=[0.5096 0.5092 0.5087 ];
x2=[0.0963 0.0964 0.0965 ];
x3=[0.3941 0.3944 0.3948];
T=[394.15 399.15 404.15 ];
N=length(x1);
K1=zeros(1,N);K2=zeros(1,N);K3=zeros(1,N);K4=zeros(1,N);K5=zeros(1,N);
A=zeros(1,N);B=zeros(1,N);C=zeros(1,N);E=zeros(1,N);D=zeros(1,N);x1n=zeros(1,N);x2n=zeros(1,N);x3n=zeros(1,N);
X1=zeros(1,N);X2=zeros(1,N);X3=zeros(1,N);X4=zeros(1,N);X5=zeros(1,N);X6=zeros(1,N);X7=zeros(1,N);
X12=zeros(1,N);X16=zeros(1,N);
for i=1:N
Sout=struct();
K1(i)=exp((-8.549)+(6692/T(i)))
K2(i)=9.34*10^-7
K3(i)=exp((16.93565)+((1250)/T(i))+(-2.575*log(T(i))))
%Equilibrium constant for reaction 4(Polyiodide formation a)
K4(i)=exp((-936.28)+((40216.27)/T(i))+(151.983*(log(T(i))))+(-0.1675*(T(i))))
K5(i)=exp((1044.78)+(-45171.42/T(i))+(-165.20*log(T(i)))+(0.1511*(T(i))))
eqns=[(b*b)/((x3(i)-b)*(a-b))==K5(i),(a*(a-b))/((x1(i)-a)^4)*(x2(i)-a)==K4(i),(c/((x1(i)-c)^5)*(x2(i)-c))==K1(i),(e^2)/((x1(i)-e)^2)==K2(i),(d^2)/((x1(i)-d)*(x2(i)-d))==K3(i)]
S=vpasolve(eqns,[a,b,c,d,e]);
mask = S.a < 0 |S.b<0| S.c < 0 | S.d < 0 | S.e < 0 ;
S.a(mask)=[];
S.b(mask)=[];
S.c(mask)=[];
S.d(mask)=[];
S.e(mask)=[];
Sout(i).a=min(S.a)
Sout(i).b=min(S.b)
Sout(i).c=min(S.c)
Sout(i).d=min(S.d)
Sout(i).e=min(S.e)
A(i)=(Sout(i).a)
B(i)=(Sout(i).b)
C(i)=(Sout(i).c)
D(i)=(Sout(i).d)
E(i)=(Sout(i).e)
x1n(i)=x1(i)-A(i)-D(i)-E(i)-C(i);
x2n(i)=x2(i)-A(i)+B(i)-C(i)-D(i);
x3n(i)=x3(i)-B(i);
X1(i)=x1n(i)/(x1n(i)+x2n(i)+x3n(i)+A(i)+B(i)+C(i)+D(i)+E(i));
X2(i)=x2n(i)/(x1n(i)+x2n(i)+x3n(i)+A(i)+B(i)+C(i)+D(i)+E(i));
X3(i)=x3n(i)/(x1n(i)+x2n(i)+x3n(i)+A(i)+B(i)+C(i)+D(i)+E(i));
X4(i)=A(i)+D(i)+E(i)/(x1n(i)+x2n(i)+x3n(i)+A(i)+B(i)+C(i)+D(i)+E(i));
X5(i)=C(i)/(x1n(i)+x2n(i)+x3n(i)+A(i)+B(i)+C(i)+D(i)+E(i));
X6(i)=A(i)-B(i)/(x1n(i)+x2n(i)+x3n(i)+A(i)+B(i)+C(i)+D(i)+E(i));
X7(i)=B(i)/(x1n(i)+x2n(i)+x3n(i)+A(i)+B(i)+C(i)+D(i)+E(i));
X12(i)=E(i)/(x1n(i)+x2n(i)+x3n(i)+A(i)+B(i)+C(i)+D(i)+E(i));
X16(i)=C(i)+D(i)/(x1n(i)+x2n(i)+x3n(i)+A(i)+B(i)+C(i)+D(i)+E(i));
end
K1 = 1×3
1.0e+03 * 4.5793 0 0
<mw-icon icon-id="meatballMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
<mw-icon icon-id="kebabMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
K2 = 1×3
1.0e-06 * 0.9340 0 0
<mw-icon icon-id="meatballMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
<mw-icon icon-id="kebabMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
K3 = 1×3
111.8264 0 0
<mw-icon icon-id="meatballMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
<mw-icon icon-id="kebabMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
K4 = 1×3
1.0e+03 * 3.2760 0 0
<mw-icon icon-id="meatballMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
<mw-icon icon-id="kebabMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
K5 = 1×3
10.7550 0 0
<mw-icon icon-id="meatballMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
<mw-icon icon-id="kebabMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
eqns = 
Sout = struct with fields:
a: 0.45644511101436436229583126272584
Sout = struct with fields:
a: 0.45644511101436436229583126272584 b: 0.32634425642891616456663020396749 - 0.0085063316052863842253167082483287i
Sout = struct with fields:
a: 0.45644511101436436229583126272584 b: 0.32634425642891616456663020396749 - 0.0085063316052863842253167082483287i c: 0.40499614171701609798318306855559 - 0.064873041847274051977377108187709i
Sout = struct with fields:
a: 0.45644511101436436229583126272584 b: 0.32634425642891616456663020396749 - 0.0085063316052863842253167082483287i c: 0.40499614171701609798318306855559 - 0.064873041847274051977377108187709i d: 0.096100276263303125736310619771794
Sout = struct with fields:
a: 0.45644511101436436229583126272584 b: 0.32634425642891616456663020396749 - 0.0085063316052863842253167082483287i c: 0.40499614171701609798318306855559 - 0.064873041847274051977377108187709i d: 0.096100276263303125736310619771794 e: 0.00049202066312225458567174360052425
A = 1×3
0.4564 0 0
<mw-icon icon-id="meatballMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
<mw-icon icon-id="kebabMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
B =
0.3263 - 0.0085i 0.0000 + 0.0000i 0.0000 + 0.0000i
C =
0.4050 - 0.0649i 0.0000 + 0.0000i 0.0000 + 0.0000i
D = 1×3
0.0961 0 0
<mw-icon icon-id="meatballMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
<mw-icon icon-id="kebabMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
E = 1×3
1.0e-03 * 0.4920 0 0
<mw-icon icon-id="meatballMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
<mw-icon icon-id="kebabMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
K1 = 1×3
1.0e+03 * 4.5793 3.7020 0
<mw-icon icon-id="meatballMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
<mw-icon icon-id="kebabMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
K2 = 1×3
1.0e-06 * 0.9340 0.9340 0
<mw-icon icon-id="meatballMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
<mw-icon icon-id="kebabMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
K3 = 1×3
111.8264 104.0385 0
<mw-icon icon-id="meatballMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
<mw-icon icon-id="kebabMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
K4 = 1×3
1.0e+03 * 3.2760 2.6828 0
<mw-icon icon-id="meatballMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
<mw-icon icon-id="kebabMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
K5 = 1×3
10.7550 11.9893 0
<mw-icon icon-id="meatballMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
<mw-icon icon-id="kebabMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
eqns = 
Sout = 1x2 struct array with fields:
a
Sout = 1x2 struct array with fields:
a b
Sout = 1x2 struct array with fields:
a b c
Sout = 1x2 struct array with fields:
a b c d
Sout = 1x2 struct array with fields:
a b c d e
A = 1×3
0.4564 0.4546 0
<mw-icon icon-id="meatballMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
<mw-icon icon-id="kebabMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
B =
0.3263 - 0.0085i 0.3299 - 0.0088i 0.0000 + 0.0000i
C =
0.4050 - 0.0649i 0.4003 - 0.0670i 0.0000 + 0.0000i
D = 1×3
0.0961 0.0962 0
<mw-icon icon-id="meatballMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
<mw-icon icon-id="kebabMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
E = 1×3
1.0e-03 * 0.4920 0.4916 0
<mw-icon icon-id="meatballMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
<mw-icon icon-id="kebabMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
K1 = 1×3
1.0e+03 * 4.5793 3.7020 3.0085
<mw-icon icon-id="meatballMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
<mw-icon icon-id="kebabMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
K2 = 1×3
1.0e-06 * 0.9340 0.9340 0.9340
<mw-icon icon-id="meatballMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
<mw-icon icon-id="kebabMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
K3 = 1×3
111.8264 104.0385 96.9274
<mw-icon icon-id="meatballMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
<mw-icon icon-id="kebabMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
K4 = 1×3
1.0e+03 * 3.2760 2.6828 2.2141
<mw-icon icon-id="meatballMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
<mw-icon icon-id="kebabMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
K5 = 1×3
10.7550 11.9893 13.2375
<mw-icon icon-id="meatballMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
<mw-icon icon-id="kebabMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
eqns = 
Sout = 1x3 struct array with fields:
a
Sout = 1x3 struct array with fields:
a b
Sout = 1x3 struct array with fields:
a b c
Sout = 1x3 struct array with fields:
a b c d
Sout = 1x3 struct array with fields:
a b c d e
A = 1×3
0.4564 0.4546 0.4527
<mw-icon icon-id="meatballMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
<mw-icon icon-id="kebabMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
B =
0.3263 - 0.0085i 0.3299 - 0.0088i 0.3331 - 0.0092i
C =
0.4050 - 0.0649i 0.4003 - 0.0670i 0.3956 - 0.0691i
D = 1×3
0.0961 0.0962 0.0963
<mw-icon icon-id="meatballMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
<mw-icon icon-id="kebabMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
E = 1×3
1.0e-03 * 0.4920 0.4916 0.4912
<mw-icon icon-id="meatballMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>
<mw-icon icon-id="kebabMenuUI" icon-width="16" icon-height="16" icon-config="{}" svg-path=""></mw-icon>

回答(1 个)

Rushil
Rushil 2025-3-28
编辑:Rushil 2025-3-28
Hello
To ensure that the values from each iteration of the loop are saved correctly, you may consider the following improvements:
  • Since each iteration produces new data, you may consider storing the values in a struct outside the loop instead of inside.
  • This will allow you to group related values together and access them easily. For example, create a structure results(i) and store each variable as a field, like results(i).a
Here is a demonstration of the above mentioned idea:
syms a b c d e
% initialization of x_i values
N = length(x1);
results(N) = struct('a', [], 'b', [], 'c', [], 'd', [], 'e', []);
for i = 1:N
% main loop with calculations
S = vpasolve(eqns, [a, b, c, d, e]);
% create mask and filter solutions
results(i).a = min(S.a);
results(i).b = min(S.b);
results(i).c = min(S.c);
results(i).d = min(S.d);
results(i).e = min(S.e);
% terminating loop and recomputing variables
end
Hope it helps out

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by