# Function takes too long time to run and Warning in interpolation

4 views (last 30 days)
Ignatius Tommy Pratama on 30 Sep 2017
Hi I am a beginner in using Matlab, and right now, I am trying to do probability analysis using Monte Carlo Simulation with 1000000 simulations. The following code is the code that I tried to delete some elements in a matrix. Nevertheless, it took a very long time to finish the following code:
N = 1000000;
for i = N:-1:1
if or(Rand_dHw(i,:) <= 0,dHw_mu(i,:) <= 0) == 1
Rand_dHw(i,:) = [];
Rand_nominator(i,:) = [];
dHw_mu(i,:) = [];
Nominator_n(i,:) = [];
else
Rand_dHw(i,:) = Rand_dHw(i,:);
Rand_nominator(i,:) = Rand_nominator(i,:);
dHw_mu(i,:) = dHw_mu(i,:);
Nominator_n(i,:) = Nominator_n(i,:);
end
end
Is there any way to optimize it or make it run faster?
Moreover, I'm also trying to do interpolation, but with the following code, Matlab showed "The grid vectors are not strictly monotonic increasing." My complete code is shown as follows:
%%Step 2 calibrate FS vs pf
N = 1000000;
%Design parameters
Rand_di = rand(N,1);
Rand_He = 1.09 +(19.70-1.09)*rand(N,1);
Rand_Hp = 0.11 +(23.70-0.11)*rand(N,1);
%Random variables
gamma_sat_mu = 18 + (22-18)*rand(N,1); gamma_sat_cov = 0.05;
Rand_gamma_sat = gamma_sat_mu + gamma_sat_cov*gamma_sat_mu.*randn(N,1);
gamma_eff_mu = gamma_sat_mu - 9.80665;
Rand_gamma_eff = Rand_gamma_sat - 9.80665;
dj_mu = 6.8*rand(N,1); dj_cov = 0.74472;
Rand_dj = exp(log(dj_mu/sqrt(1+dj_cov^2))+sqrt(log(1+dj_cov^2))*randn(N,1));
Rand_M = exp(m+s*randn(N,1));
Rand_dHw = Rand_He+Rand_di-Rand_dj;
dHw_mu = Rand_He+Rand_di-dj_mu;
%Nominal Nominator and Random Nominator
Rand_nominator = 2*Rand_Hp-2*Rand_di+Rand_dHw;
Nominator_n = 2*Rand_Hp-2*Rand_di+dHw_mu;
for i = N:-1:1
if or(Rand_dHw(i,:) <= 0,dHw_mu(i,:) <= 0) == 1
Rand_dHw(i,:) = [];
Rand_nominator(i,:) = [];
dHw_mu(i,:) = [];
Nominator_n(i,:) = [];
else
Rand_dHw(i,:) = Rand_dHw(i,:);
Rand_nominator(i,:) = Rand_nominator(i,:);
dHw_mu(i,:) = dHw_mu(i,:);
Nominator_n(i,:) = Nominator_n(i,:);
end
end
%Nominal FS and Random FS
FS_n = zeros(length(Nominator_n),1);
Rand_FS = zeros(length(Rand_nominator),1);
for i = 1:length(Rand_nominator)
Rand_FS(i,:) = Rand_M(i,:).*Rand_nominator(i,:).*Rand_gamma_eff(i,:)./(9.80665*Rand_dHw(i,:));
FS_n(i,:) = Nominator_n(i,:).*gamma_eff_mu(i,:)./(9.80665*dHw_mu(i,:));
end
eta = [1:0.001:25];
for i=1:length(eta)
pf(i) = mean(FS_n./Rand_FS > eta(i));
end
pf_target = 0.1; FS_required = interpn(log(pf),eta,log(pf_target));
What kind of approach that I should do to fix that problem? I've tried to change the "eta" interval, but it still didn't work.
I really look forward to your help. Thank you.

Jan on 30 Sep 2017
Edited: Jan on 30 Sep 2017
Letting an array shrink iteratively is surprisingly expensive, exactly as the growing. Avoid this strictly.
The branch
Rand_dHw(i,:) = Rand_dHw(i,:);
Rand_nominator(i,:) = Rand_nominator(i,:);
dHw_mu(i,:) = dHw_mu(i,:);
Nominator_n(i,:) = Nominator_n(i,:);
is a complete waste of time: You replace all the values by themselves.
Note that the condition for if must be a scalar. In your case
if or(Rand_dHw(i,:) <= 0,dHw_mu(i,:) <= 0) == 1
if converted internally to
if all(or(Rand_dHw(i,:) <= 0, dHw_mu(i,:) <= 0))
Better do this explicitly and without a loop:
remove = all(or(Rand_dHw <= 0, dHw_mu <= 0), 2);
Rand_dHw(remove, :) = [];
Rand_nominator(remove,:) = [];
dHw_mu(remove,:) = [];
Nominator_n(remove, :) = [];
For the second problem: The posted code does not contain an interpolation. I cannot suggest a fix for a code, which I do not see. I cannot guess also, what "tried to change the "eta" interval" means. Please post the failing code.
Ignatius Tommy Pratama on 30 Sep 2017
Thank you very much for your response. I really appreciate it. I've updated the second problem. I hope it is more clear for you.

dpb on 30 Sep 2017
First section can be rewritten as
ix=any(Rand_dHw<= 0),2)|any(dHw_mu<=0,2)); % find any rows with zeros in either
Rand_dHw = Rand_dHw(~ix,:); % save the others...
Rand_nominator= Rand_nominator(~ix,:);
dHw_mu = dHw_mu(~ix,:);
Nominator_n = Nominator_n(~ix,:);
For the second, don't see where that error would have come from in the code you posted...but you can again eliminate the loop; Matlab "dot" operators work on more than vectors--
Rand_FS=Rand_M.*Rand_nominator.*Rand_gamma_eff./Rand_dHw/9.80665;
FS_n= Nominator_n.*gamma_eff_mu./dHw_mu/9.80665;
Ignatius Tommy Pratama on 30 Sep 2017
Thank you very much for your help. I've already edited the second problem. I hope it is more clear for you.

### Categories

Find more on Sources in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!