How to store answers of variables from for loop in structures
1 次查看(过去 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
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!