Getting different values when I run for loop twice

2 次查看(过去 30 天)
Hi, I'm having a peculiar problem. My lam variable only generates correctly if I run the code twice. I need a 3x3 matrix of calculated values, but when I first run it, it only gives me a 3x2 matrix. I should be getting:
0.0000 0.0000 0.0000
3.5933 3.3157 0.0000
4.2864 4.0088 0.0000
%% Fragility functions
% Define degradation states (existing damage states prior to the flood)
% Creating a cell array called DegState DegState = {'Acceptable', 'Degraded', 'Failure-Missing'}
DgS1=1; DegState{DgS1}='Acceptable';
DgS2=2; DegState{DgS2}='Degraded';
DgS3=3; DegState{DgS3}='Failure';
% Define HCLPF capacity reduction factors due to degraded seals (different capacity for each degradation level)
DegCapRed=[0.99; 0.75; 1E-17]; %Acceptable, degraded, failure-missing
%The last one
% Define damage states
DmS1=1; DamState{DmS1}='Ds_0 = No Damage'; % lowest damage state must be no damage for code that follows to work
DmS2=2; DamState{DmS2}='Ds_1 = Partial';
DmS3=3; DamState{DmS3}='Ds_2 = Failure';
% Area of seal as a function of damage state
% Penetration Seal Area Calculation
Pipe_OD = 0.24; %[ft] Annulus nominal ID
Sleeve_ID = 0.3355; %[ft] Annulus nominal OD
Aopen = pi()*(((Sleeve_ID/2)^2)-((Pipe_OD/2)^2));
Amean(DmS1,1)=0.01*Aopen;
Amean(DmS2,1)=0.5*Aopen;
Amean(DmS3,1)=0.99*Aopen;
% Define baseline capacity values for each damage state for non-degraded seal
BaselineHCLPF(DmS1)= NaN; % capacity against no damage or more damage
BaselineHCLPF(DmS2)=0.50*50; % capacity against moderate/partial or more damage
BaselineHCLPF(DmS3)=50; % capacity against complete failure
%% Calculate all fragility parameters for Lognormal Distribution
zeta=0.3; % assumed value (composite standard distribution)
pstar=0.1; % non-exceedance probability for assumed component capacity HCLPF
% FIX, for some reason it only works if you run this twice?
for iDG=1:length(DegState)
for iDM=1:length(DamState)
if iDM==DmS1|| iDG==DgS3 %if damage state is in the DmS1, the lamda is almost 0 (cannot be zero bc lognormal)
lam(iDM,iDG)=1e-17; %artificial -> probability will be 1.0 later
else
lam(iDM,iDG)=log(BaselineHCLPF(iDM)*DegCapRed(iDG))-zeta*norminv(pstar)
end
end
end
% Define probability objects for each damage state/degradation combination
for iDG=1:length(DegState)
for iDM=1:length(DamState)
pd_frag{iDG}{iDM}=makedist('Lognormal',lam(iDM,iDG),zeta); % Creating a 1xlength(DegState) cell, within each cell is another 1xlength(DamState)
end
end
% Plot fragility PMFs
figure; legct=0;
for iDG=1:2%length(DegState)
for iDM=2:length(DamState)
x=[]; x=linspace(0,20,1000);
Fx=[]; Fx=cdf(pd_frag{iDG}{iDM},x);
plot(x,Fx); hold on
legct=legct+1; leg{legct}=strcat(DegState{iDG},'; ',DamState{iDM});
end
end
legend(leg)
title('Family of Fragility Functions for a Penetration Seal')
%subtitle('Assuming Lognormal Distribution')
xlabel('\Delta\eta_1 (ft)')
ylabel('P(DS\geqds_i|\Delta\eta_1=\Deltan)')

采纳的回答

Dyuman Joshi
Dyuman Joshi 2023-6-28
Your code is working properly. However, since you have not used a semi-colon to supress the statement defined for the else condition, it shows the output corresponding to that statement.
If you check out lam after the completion of the whole loop, it is what you expect it to be -
%% Fragility functions
% Define degradation states (existing damage states prior to the flood)
% Creating a cell array called DegState DegState = {'Acceptable', 'Degraded', 'Failure-Missing'}
DgS1=1; DegState{DgS1}='Acceptable';
DgS2=2; DegState{DgS2}='Degraded';
DgS3=3; DegState{DgS3}='Failure';
% Define HCLPF capacity reduction factors due to degraded seals (different capacity for each degradation level)
DegCapRed=[0.99; 0.75; 1E-17]; %Acceptable, degraded, failure-missing
%The last one
% Define damage states
DmS1=1; DamState{DmS1}='Ds_0 = No Damage'; % lowest damage state must be no damage for code that follows to work
DmS2=2; DamState{DmS2}='Ds_1 = Partial';
DmS3=3; DamState{DmS3}='Ds_2 = Failure';
% Area of seal as a function of damage state
% Penetration Seal Area Calculation
Pipe_OD = 0.24; %[ft] Annulus nominal ID
Sleeve_ID = 0.3355; %[ft] Annulus nominal OD
Aopen = pi()*(((Sleeve_ID/2)^2)-((Pipe_OD/2)^2));
Amean(DmS1,1)=0.01*Aopen;
Amean(DmS2,1)=0.5*Aopen;
Amean(DmS3,1)=0.99*Aopen;
% Define baseline capacity values for each damage state for non-degraded seal
BaselineHCLPF(DmS1)= NaN; % capacity against no damage or more damage
BaselineHCLPF(DmS2)=0.50*50; % capacity against moderate/partial or more damage
BaselineHCLPF(DmS3)=50; % capacity against complete failure
%% Calculate all fragility parameters for Lognormal Distribution
zeta=0.3; % assumed value (composite standard distribution)
pstar=0.1; % non-exceedance probability for assumed component capacity HCLPF
% FIX, for some reason it only works if you run this twice?
for iDG=1:length(DegState)
for iDM=1:length(DamState)
if iDM==DmS1 || iDG==DgS3 %if damage state is in the DmS1, the lamda is almost 0 (cannot be zero bc lognormal)
lam(iDM,iDG)=1e-17; %artificial -> probability will be 1.0 later
else
lam(iDM,iDG)=log(BaselineHCLPF(iDM)*DegCapRed(iDG))-zeta*norminv(pstar); %output supressed
end
end
end
lam
lam = 3×3
0.0000 0.0000 0.0000 3.5933 3.3157 0.0000 4.2864 4.0088 0.0000

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by