Create an Array with Nested Loops
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I wish to create a matrix from a set of arrays generated in a loop
So for example: I have a for loop that creates 10 values of y and stores them in a 10 element row vector.
If y is a function of a random variable, and I want to perform 50 evaluations of y over a distribution of it's random variable then I need to vertically concatenate 50 10-element row vectors into a 10X50 element matrix.
So I was trying to use nested loops and can't seem to successfully add all of the vectors into a matrix. The problem is probably notational. I would also be interested in any easier ways to do this.
here is the script: Don't know if i need to create the randomly distributed parameter vector in the beginning or if it can be taken care of in the nested loop.
%Script that samples for yield based on normal distribution and does a MC
% Initialize Random Sampling Values and Loop
MCnumb=500; % number of monte carlo iterations
MCits=(0:MCnumb); % generate MC loop variable
Y0=0.42 % mg VSSa/mgBODL
clear Y; % Clear Variable
Y(1)=Y0 % Initial value for yield
for j=1:length(MCits-1), %Set up for loop for MC
Y(j+1)=(Y(j)*rand(1)/Y(j));
end
%Script that demonstrates Euler integration for a Monod Model
% The problem set to be solved is: % S'=((-q*S(t))/(K+S(t)))*X(t);
% X'=Y*(((q*S(t))/(K+S(t))-b-(k2h/1.42))*X(t);
% Define Parameters;
q=10; % mgBODl/mgVSSa-day
K=20; % mgBODl/l
b=0.15; % 1/day
k2h=0.09; % mgCODP/mgVSSa-day
%Define Initial Conditions;
S0=500; % mg/L
X0=20; % mg/L
%Initialize Time Set and Step Size
h=0.01; %h is the time step.
tend=2; %tend is end of approximation in days
t=.1:h:tend; %initialize time variable.
clear Substrate; %wipe out old variables.
clear Biomass;
Sub(1)=S0; %initial condition (same for approximation).
Biomass(1)=X0;
for j=1:length(Y)-1,
for i=1:length(t)-1, %Set up "for" loop.
Srate(i)=-((q*Sub(i))/(K+Sub(i)))*Biomass(i); %Calculate derivative (rate);
Xrate(i)=Y(j)*((q*Sub(i))/(K+Sub(i))-b-(k2h/1.42))*Biomass(i);
Sub(i+1)=Sub(i)+h*Srate(i); %Estimate new concentrations;
Biomass(i+1)=Biomass(i)+h*Xrate(i);
Substrate(j+1)=Sub(i);
end
SJ(j)=vertcat(Substrate(j),Substrate(j+1))
end
0 Kommentare
Akzeptierte Antwort
Sean de Wolski
am 24 Apr. 2012
Could you boil this down to a small example? It looks like you're missing the ':' necessary to specify all columns or all rows. Not really sure though.
SJ(:,j) %all rows in column j
or similar
0 Kommentare
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!