user3172906
user3172906

Reputation: 5

Monte Carlo simulation in matlab

i'm trying to make a time sequence monte carlo simulation for reliability analysis. The failure rate/year λ of my system is a constant so i generate random numbers from the exponential distribution with exprnd(1/λ) to calculate possible future failure times(years).The monte carlo simulation period will be 30 years and the number of experiments will be user defined -k.The results from each iteration will be stored in matrix and each column will be the failure times of my system during its 30 years life cycle.The problem is that i don't know the exact iterations of the while loop.The code for one 30year period will be similar to the following

MTTF=0.6;         % enter mean time to failure 1/λ
A=zeros(300,1);  %preallocate a large enough vector
t=0;
n=1;
while t<30        %time is less than 30 years of simulation
    hup=exprnd(MTTF);%generate random failure time
    t=t+hup;
    A(n)=t;
    n=n+1;

end

Upvotes: 0

Views: 3859

Answers (1)

Jonas
Jonas

Reputation: 74930

You may want to use a cell array for storing your results:

MTTF=0.6;         % enter mean time to failure 1/λ
nExperiments = 10;% enter # of experiments

results = cell(nExperiments,1);

for iExperiment = 1:nExperiments
A=zeros(30000,1);  %preallocate a large enough vector
t=0;
n=1;
while t<30        %time is less than 30 years of simulation
    hup=exprnd(MTTF);%generate random failure time
    t=t+hup;
    A(n)=t;
    n=n+1;

end

results{iExperiment} = A(1:n-1);
end % loop experiments

% calculate number of failures in each experiment
nFailures = cellfun(@numel,results);

Upvotes: 1

Related Questions