How to store value inside a for loop
Info
This question is closed. Reopen it to edit or answer.
Show older comments
I used interp1 to get x which corresponding to V=600 inside a for loop. How can I store all values for each r value. Also how can I improve my algorithm for my code? Thank you so much!
i=1;
for r = 1:10
[v,x] = ode45(@(v,x) diffeq(v,x,r),[0 610],0.0001);
x_val = interp1(v,x,600)
figure(1)
plot(v ,x);
hold on
i = i+1;
end
function dxdv = diffeq(v,x,r)
%choose the basis for NH3 in the product stream
beta = 0.00140;
k1 = 1.2; %1.2lbmol/ft^3hr
K = 4.67*10^-3;
P = 300/1.013; %atm
dxdv = zeros(1,1);
%mole of each species on the stream after reactor
syms n %x is the signle pass conversion, n is molar flow rate of Ar in
%RF stream
y =@(n)(n-(2./x+n)./(r+1).*0.00298)./((2./x+n)./(r+1).*r)==n./(2./x-2+n);
%y = @(n) 0.002893+r*n/(n+2/x-2) == (r+1)*n/(2/x+n);
n = double(solve(y,n));
nAr = n(2);
nN2 = (1./(2.*x)-1./2);
nH2 = (3.*nN2);
ntotal = nN2+nH2+1+nAr;
%Caculating the composition of each spiecies
yN2 = (nN2./ntotal);
yH2 = (nH2./ntotal);
yNH3 = (1./ntotal);
yAr = (nAr./ntotal);
% innitial condition
%Real kinetic
rN2 = (-k1.*(yN2.*yH2.^3-yNH3.^2./(K.^2.*P.^2))./(yNH3.*yH2.^0.5+beta.*yH2.^2).^(3./2));
dxdv(1)= -rN2./(1343./(2.*x));
end
Answers (1)
x_val = zeros(1,10)
VV = cell(10,1) ;
xx = cell(10,1) ;
for r = 1:10
[v,x] = ode45(@(v,x) diffeq(v,x,r),[0 610],0.0001);
x_val(r) = interp1(v,x,600) ;
VV{r} = v ;
xx{r} = x ;
figure(1)
plot(v ,x);
hold on
end
If exact size of v,x is known; it can be stored into a matrix.
3 Comments
Weichen Xing
on 30 Apr 2019
KSSV
on 30 Apr 2019
r = 1:0.01:10 ;
N = length(r) ;
x_val = zeros(1,N)
VV = cell(N,1) ;
xx = cell(N,1) ;
for i = 1:N
[v,x] = ode45(@(v,x) diffeq(v,x,r(i),[0 610],0.0001);
x_val(i) = interp1(v,x,600) ;
VV{i} = v ;
xx{i} = x ;
figure(1)
plot(v ,x);
hold on
end
Weichen Xing
on 30 Apr 2019
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!