Running an inner loop multiple times.

4 views (last 30 days)
Hi all,
I'm a novice with Matlab. I have the following code that works, however I want to run the loop multiple times (number_of_runs), (montecarlo sim).
I'm assuming i need another loop, but i'm having no luck, i.e. exceeding numbers of arrays or generating more columns.
For example if the nth_failure = 10, i want the output of 10 columns for x and t, with the number_of_runs being a defined number of rows. Averaging each column.
for i = 2:nth_failure
p(i) = rand;
r_t_T = (p(i) .* exp(-(v(i-1)./n).^b));
x(i) = ((n .* (-log(r_t_T)).^(1./b))-v(i-1));
t(i) = t(i-1) + x(i);
v(i)= qf*(v(i-1)+x(i));
end
any help appreciated.
Cheers
  2 Comments
KSSV
KSSV on 11 Oct 2021
Are you getting any error with the above code? What error? We don't know the dimensions of other varbales to help you.
Colin Gowling
Colin Gowling on 11 Oct 2021
Thanks for the quick response KSSV, no the above code works, sorry left out the intiator values, complete code shown below, i've added the outer loop i'm trying to get to work, inner loop works fine. i've got the output for t at the end of the code, its just populating the first column.
q = 0.3;
b = [1.3];
n = [35.42822637];
nth_failure = 10;
p = zeros(nth_failure,1);
x = zeros(nth_failure,1);
v = zeros(nth_failure,1);
t = zeros(nth_failure,1);
r_t_T= zeros(nth_failure,1);
qf = 1-q;
v0 = 0;
p(1) = rand();
x = n .* (-log(p(1))).^(1./b);
t = x;
v(1) = v0 + qf * x(1) ;
number_of_runs = 60;
for j = 1:number_of_runs
for i = 2:nth_failure
p(i) = rand;
r_t_T = (p(i) .* exp(-(v(i-1)./n).^b));
x(i) = ((n .* (-log(r_t_T)).^(1./b))-v(i-1));
t(i) = t(i-1) + x(i);
v(i)= qf*(v(i-1)+x(i));
hold on
end
t(i,:) = [j]
end
t =
0.4384 5.4148 7.1490 15.7259 38.6140 54.0290 67.8687 103.9010 111.0510 139.8454
67.9212 0 0 0 0 0 0 0 0 0
87.0763 0 0 0 0 0 0 0 0 0
90.8112 0 0 0 0 0 0 0 0 0
96.0525 0 0 0 0 0 0 0 0 0
157.9458 0 0 0 0 0 0 0 0 0
167.9089 0 0 0 0 0 0 0 0 0
200.6098 0 0 0 0 0 0 0 0 0
207.8494 0 0 0 0 0 0 0 0 0
60.0000 60.0000 60.0000 60.0000 60.0000 60.0000 60.0000 60.0000 60.0000 60.0000

Sign in to comment.

Accepted Answer

KSSV
KSSV on 11 Oct 2021
Edited: KSSV on 11 Oct 2021
You may proceed something like shown. Check your code and values once, t is coming as inf. This should give you some idea.
number_of_runs = 60;
q = 0.3;
b = [1.3];
n = [35.42822637];
nth_failure = 10;
p = zeros(number_of_runs,nth_failure);
x = zeros(number_of_runs,nth_failure);
v = zeros(number_of_runs,nth_failure);
t = zeros(number_of_runs,nth_failure);
r_t_T= zeros(number_of_runs,nth_failure);
qf = 1-q;
v0 = 0;
p(1) = rand();
x(:,1) = n .* (-log(p(number_of_runs,1))).^(1./b);
t(:,1) = x(:,1);
v(1,1) = v0 + qf * x(1) ;
for i = 1:number_of_runs
for j = 2:nth_failure
p(i,j) = rand;
r_t_T = (p(i,j) .* exp(-(v(i,j-1)./n).^b));
x(i,j) = ((n .* (-log(r_t_T)).^(1./b))-v(i,j-1));
t(i,j) = t(i,j-1) + x(i,j);
v(i,j)= qf*(v(i,j-1)+x(i,j));
end
end
The second loop can be removed and vectorised. Once you get this, try for that.
  1 Comment
Colin Gowling
Colin Gowling on 11 Oct 2021
thanks again, yeah still struggling re the infinity, to give some context re algorithm,
the p(1), v(1) etc, (first value in the nth_failure row) should only be performed again after the nth_failure for loop is completed, then a new rand generated for P(1) for the new row after and then perform the nth_failrue looo again.
This code works fine for just one row, no n_numbers_runs so values and algorithm at this level works.
Is my understanding regarding calling (j) for the outer loop completes (i) first then (j)?
cheers
q = 0.3;
b = [1.3];
n = [35.42822637];
nth_failure = 10;
p = zeros(nth_failure,1);
x = zeros(nth_failure,1);
v = zeros(nth_failure,1);
t = zeros(nth_failure,1);
r_t_T= zeros(nth_failure,1);
qf = 1-q;
v0 = 0;
Perform this update after nth_failure loop and number_of runs time.
p(1) = rand();
x = n .* (-log(p(1))).^(1./b);
t = x;
v(1) = v0 + qf * x(1) ;
for i = 2:nth_failure
p(i) = rand;
r_t_T = (p(i) .* exp(-(v(i-1)./n).^b));
x(i) = ((n .* (-log(r_t_T)).^(1./b))-v(i-1))
t(i) = t(i-1) + x(i)
v(i)= qf*(v(i-1)+x(i));
end

Sign in to comment.

More Answers (1)

Colin Gowling
Colin Gowling on 12 Oct 2021
Thanks again KSSV,
The code works, just had to call up the first rand(1) funciton.
Only question is how do i get the first value of each row to change each time number_of_runs is performed?
Cheers!
number_of_runs = 60;
q = 0.3;
b = [1.3];
n = [35.42822637];
nth_failure = 10;
p = zeros(number_of_runs,nth_failure);
x = zeros(number_of_runs,nth_failure);
v = zeros(number_of_runs,nth_failure);
t = zeros(number_of_runs,nth_failure);
r_t_T= zeros(number_of_runs,nth_failure);
qf = 1-q;
v0 = 0;
p(:,1) = rand(number_of_runs,1);
x(:,1) = n .* (-log(p(number_of_runs,1))).^(1./b);
t(:,1) = x(:,1);
v(1,1) = v0 + qf * x(1) ;
for i = 1:number_of_runs
for j = 2:nth_failure
p(i,j) = rand;
r_t_T = (p(i,j) .* exp(-(v(i,j-1)./n).^b));
x(i,j) = ((n .* (-log(r_t_T)).^(1./b))-v(i,j-1));
t(i,j) = t(i,j-1) + x(i,j);
v(i,j)= qf*(v(i,j-1)+x(i,j));
end
end
t =
107.3778 110.4133 118.4447 134.8809 144.9734 150.0457 159.6877 185.6765 220.3255 236.1304
107.3778 134.1565 141.1361 194.5945 227.2295 239.3157 275.6097 277.2163 291.2133 333.4897
107.3778 158.9574 162.8430 165.2538 172.2229 179.1937 210.4064 241.7658 242.8197 245.2460
107.3778 122.0798 122.4962 172.0600 194.4624 204.1411 214.0683 214.2605 226.2904 243.1027
107.3778 167.4689 201.4004 231.7185 237.5841 247.2832 247.9570 258.6507 281.6019 301.7420

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!