## How do I store data in multiple loops?

### Scott Powers (view profile)

on 2 Nov 2017
Latest activity Answered by Roman Müller-Hainbach

### Roman Müller-Hainbach (view profile)

on 2 Nov 2017
So I have the task to solve a system of equations over a range of temperatures and a range of volumes. From this, I am supposed to make surface plots of my results. The problem is, I can only get one of my loops to output it's proper range of data, because I do not know how to get matlab to store the data from the loops in a matrix, instead of creating one long vector. If anyone could help, that would be wonderful. Thanks
%this first section is just constants the equations need to run
mdot=1; %L/min
A=2; %2mol/L concen A
E=1; %mol/L concen Enzyme
%therefore molar flow rate is 2 mol/min in and out
ndot=2;
%knb=kb-1, ktb=kb2
kb=0.4;
knb=0.03;
ktb=0.005;
kc=0.05;
knc=0.045;
ktc=0.03;
KMB=(knb+ktb)/kb;
KMC=(knc+ktc)/kc;
v=20;
x1=[];
x2=[];
x3=[];
x4=[]; %this is just to check the total mol balance
%Out of the cstr
x5=[];
xa=[];
xb=[];
xc=[];
xd=[];
xf=[];
xg=[];
xUU=[];
%VCSTR 20->500 L
DH1=9000; %in J/mol
DG1=-10000;
DH2=-15000;
DG2=500;
DH3=30000;
DG3=500;
P=0.5;
R=8.314;
spaces=24;
n=((500-20)/spaces)+1;
J=linspace(20,500,n);
T=273
%so here is the problem. I need to run temperature from 273 to 423.
%However when I try to loop temperatures, it spits out all of my x values linearly instead of in a grid.
for i=1:1
Lk1=(-1/R)*(((DG1-DH1)/298)+(DH1/T));
k1=exp(Lk1)
Lk2=(-1/R)*(((DG2-DH2)/298)+(DH2/T));
k2=exp(Lk2)
Lk3=(-1/R)*(((DG3-DH3)/298)+(DH3/T));
k3=exp(Lk3)
%y(1)=a y(2)=b y(3)=c y(4)=d y(5)=f y(6)=G y(7)=Z1 y(8)=Z2 y(9)=Z3
for w=1:length(J)
fun=@(x)ndot-x-(ktb*E*x/(x+KMB*mdot))*v-(ktc*E*x/(x+KMC*mdot))*v;
x0=1;
[x]=fsolve(fun,x0); %A
v=v+spaces;
xtwo=(ktb*E*x/(x+KMB*mdot))*v; %B
xthree=(ktc*E*x/(x+KMC*mdot))*v; %C
xfour=[x]+xtwo+xthree; %Checks that CSTR moles stay the same
Eqsys=@(y)[(k1*(y(1)^2)*y(2)*y(3))-((y(4)^3)),
(k2*y(1)*y(2))-(y(3)*(y(5)^3)*(P^2)),
(k3*P*y(1)*y(2))-y(6),
([x]-2*y(7)-y(8)-y(9)-y(1)),
(xtwo-y(7)-y(8)-y(2)),
(xthree-y(7)+y(8)-y(9)-y(3)),
((3*y(7))-y(4)),
((3*y(8))-y(5)),
(y(9)-y(6))];
b=[1,1,1,1,1,1,1,1,1];
q=fsolve(Eqsys,b);
x1=[x1;[x]]; %A range of vol
x2=[x2 xtwo]; %B ''
x3=[x3 xthree]; %c"
x4=[x4 xfour];
x5=[x5;q];
xa=[xa;q(:,1)];
xb=[xb;q(:,2)];
xc=[xc;q(:,3)];
xd=[xd;q(:,4)];
xf=[xf;q(:,5)];
xg=[xg;q(:,6)];
end
T=T+(150/(n));
end
% so if anyone can get the values to run from v=20 to 500 and T=273 to 423, that would be wonderful. Thanks

KSSV

### KSSV (view profile)

on 2 Nov 2017
Your code is pretty messy....you have to tell what you are trying to solve....or give the complete code which tried and your expectations.
Scott Powers

### Scott Powers (view profile)

on 2 Nov 2017
I am trying to solve y(1) through y(9) for all volumes v=20 to v=500 with a temperature range of 273-423. This should be able to give me a surface plot of all of the values of y(1) (or the other variables) at all the temps and volumes. Basically it should be a 3D plot of the solved variables against the volumes and temps.