hw1
clearvars; clc; % hw1tx.m
random_N = 100;
seed = 0;
obj = gumbel(random_N, seed);
obj.plot();
test_N = 10000;
obj = confident(test_N);
obj.show_interval();
random_N = 108000;
obj = jacket(random_N, seed);
obj.plot();
% gumbel.m
classdef gumbel
properties (Access = private)
alpha;
beta;
random_N;
seed;
end
methods
function obj = gumbel(random_N, seed)
obj.alpha = 2000;
obj.beta = 300;
obj.random_N = random_N;
obj.seed = seed;
end
function plot(obj)
x_rad = obj.get_random_x();
y_rad = get_y_rad(obj);
x_range = (1500:0.1:4500)';
y_exp = obj.get_y_exp(x_range);
y_rel = obj.get_y_rel(x_range);
fig = figure(1);
plts = [];
strs = [];
plt = scatter(x_rad, y_rad); hold on;
plt.Marker = 'x';
plt.SizeData = 60;
plt.MarkerEdgeColor = 'Black';
str = "Sample";
plts = [plts; plt];
strs = [strs; str];
plt = plot(x_range, y_rel); hold on;
plt.LineStyle = '-';
plt.LineWidth = 1.5;
plt.Color = 'Black';
str = "True";
plts = [plts; plt];
strs = [strs; str];
plt = plot(x_range, y_exp); hold on;
plt.Color = 'Black';
plt.LineWidth = 1.5;
plt.LineStyle = '--';
str = "Empirical";
plts = [plts; plt];
strs = [strs; str];
fig.CurrentAxes.XLim = [1500, 4500];
leg = legend(plts, strs);
leg.FontSize = 17;
leg.Box = "off";
xl = xlabel("x");
yl = ylabel("y");
xl.FontSize = 17;
yl.FontSize = 17;
saveas(gcf, "fig1-1.png");
close(gcf);
end
function [alpha_exp, beta_exp] = get_exp(obj)
gumbel_x = obj.get_random_x();
mean_num = mean(gumbel_x,1);
std_num = std(gumbel_x,1);
beta_exp = std_num/1.28255;
alpha_exp = mean_num-0.57722*beta_exp;
end
end
methods (Access = private)
function gumbel_x = get_random_x(obj)
random_y = obj.get_random_y();
gumbel_x = obj.alpha-obj.beta*log(-log(random_y));
end
function y_rad = get_y_rad(obj)
random_y = obj.get_random_y();
y_rad = log(-log(random_y));
end
function y_rel = get_y_rel(obj, x_range)
y_rel = -(x_range-obj.alpha)/obj.beta;
end
function y_exp = get_y_exp(obj, x_range)
gumbel_x = obj.get_random_x();
mean_num = mean(gumbel_x,1);
std_num = std(gumbel_x,1);
beta_exp = std_num/1.28255;
alpha_exp = mean_num-0.57722*beta_exp;
y_exp = -(x_range-alpha_exp)/beta_exp;
end
function random_y = get_random_y(obj)
rng(obj.seed);
random_y = rand(obj.random_N, 1);
end
end
end
% jacket.m
classdef jacket
properties (Access = private)
random_N;
seed;
end
methods
function obj = jacket(random_N, seed)
obj.random_N = random_N;
obj.seed = seed;
end
function plot(obj)
strs = [];
plts = [];
fig = figure(1);
hold on;
[p_f, QcQ, xx] = get_Qcap(obj);
QcQ1 = QcQ(QcQ < 0);
xx1 = xx(QcQ < 0);
QcQ2 = QcQ(QcQ >= 0);
xx2 = xx(QcQ >= 0);
% bad
plt = scatter(xx1, QcQ1);
hold on;
plt.Marker = 'x';
plt.SizeData = 38;
plt.MarkerEdgeColor = 'Red';
plt.MarkerEdgeAlpha = 1.0;
plt.SizeData = 18;
str = "结构失效";
plts = [plts; plt];
strs = [strs; str];
% good
plt = scatter(xx2, QcQ2);
hold on;
plt.Marker = 'x';
plt.SizeData = 38;
plt.MarkerEdgeColor = 'Blue';
fig.CurrentAxes.XLim = [1,108000];
fig.CurrentAxes.YLim = [-350,350];
plt.MarkerEdgeAlpha = 1.0;
str = "结构不失效";
plts = [plts; plt];
strs = [strs; str];
hold on;
disp("p_f = " + p_f);
% zero
ze = zeros(size(xx, 1), 1);
plt = plot(xx, ze);
plt.LineStyle = '--';
plt.Color = 'Black';
plt.LineWidth = 1.5;
ld = legend(plts, strs);
ld.FontSize = 17;
ld.Box = 'off';
ld.Location = 'Best';
xl = xlabel("实验序号");
xl.FontSize = 17;
yl = ylabel("Qcap-Q");
yl.FontSize = 17;
saveas(fig, "fig1-3.png");
close(fig);
end
end
methods (Access = private)
function n = get_rad_n(obj)
rng(obj.seed);
n = rand(obj.random_N,4);
end
function [H, U, FB, RSR] = get_HU(obj)
m = obj.random_N;
n = obj.get_rad_n();
H = zeros(m,1);
U = zeros(m,1);
FB = zeros(m,1);
RSR = zeros(m,1);
for i = 1:m
H(i,1) = 30.1493-2.34*log(-log(n(i,1)));
U(i,1) = 0.4+0.6*n(i,2);
FB(i,1) = 24+2*n(i,3);
RSR(i,1) = norminv(n(i,4),2.2,0.1);
end
end
function [p_f, QcQ, xx] = get_Qcap(obj)
m = obj.random_N;
Q = zeros(m,1);
Qcap = zeros(m,1);
a1 = 0.03;
a2 = 3;
a3 = 2.2;
a4 = 0.65;
a5 = 0.25;
b = 80;
h001 = 28;
u01 = 1;
k = 0;
[H, U, FB, RSR] = obj.get_HU();
for j = 1:m
if 0.65*H(j,1) < FB(j,1)
Q(j,1) = a1*(H(j,1)+a2*U(j,1))^a3;
else
Q(j,1) = a1*(H(j,1)+a2*U(j,1))^a3+a5*b*(a4*H(j,1)-FB(j,1));
end
Qcap(j,1) = RSR(j,1)*a1*(h001+a2*u01)^a3;
if Q(j,1) > Qcap(j,1)
k = k+1;
end
end
p_f = k/m;
QcQ = Qcap-Q;
xx = (1:1:m)';
end
end
end
% confident.m
classdef confident
properties (Access = private)
test_N;
end
methods
function obj = confident(test_N)
obj.test_N = test_N;
end
function show_interval(obj)
[alpha, beta] = obj.get_exp();
alpha_mean = mean(alpha);
alpha_std = std(alpha);
beta_mean = mean(beta);
beta_std = std(beta);
alpha_left = alpha_mean-1.645*alpha_std/sqrt(obj.test_N);
alpha_right = alpha_mean+1.645*alpha_std/sqrt(obj.test_N);
beta_left = beta_mean-1.645*beta_std/sqrt(obj.test_N);
beta_right = beta_mean+1.645*beta_std/sqrt(obj.test_N);
disp("alpha_left = " + alpha_left);
disp("alpha_right = " + alpha_right);
disp("beta_left = " + beta_left);
disp("beta_right = " + beta_right);
end
end
methods (Access = private)
function [alpha, beta] = get_exp(obj)
alpha = zeros(obj.test_N,1);
beta = zeros(obj.test_N,1);
for ii = 1:obj.test_N
random_N = 100;
seed = ii;
obj_gumbel = gumbel(random_N, seed);
[alpha_exp, beta_exp] = obj_gumbel.get_exp();
alpha(ii, 1) = alpha_exp;
beta(ii, 1) = beta_exp;
end
end
end
end
hw4_prj1
clearvars; clc; % main.m
sampleN = 20;
obj = solution(sampleN);
obj.solve();
% solution.m
classdef solution
properties (Access = private)
sampleN;
end
methods
function obj = solution(sampleN)
obj.sampleN = sampleN;
end
function solve(obj)
samples = obj.get_samples();
objGumbel = gumbel(samples);
CDF_gumbel = objGumbel.solve();
objWeibull = weibull(samples);
CDF_weibull = objWeibull.solve();
fig = figure(1);
plts = [];
strs = [];
plt = plot(samples, CDF_gumbel);
hold on;
plt.LineStyle = '--';
plt.Color = 'Black';
plt.LineWidth = 1.5;
str = "Gumbel";
plts = [plts, plt];
strs = [strs, str];
plt = plot(samples, CDF_weibull);
plt.LineStyle = '-.';
plt.Color = 'Black';
plt.LineWidth = 1.5;
str = "Weibull";
plts = [plts, plt];
strs = [strs, str];
xl = xlabel('Heave/H');
yl = ylabel('CDF');
xl.FontSize = 17;
yl.FontSize = 17;
ld = legend(plts, strs);
ld.FontSize = 17;
ld.Box = 'off';
ld.Location = 'best';
saveas(fig, './fig1-5.png');
close(fig);
end
end
methods (Access = private)
function samples = get_samples(obj)
rng(1);
N = obj.sampleN;
samples = 0.6 + 0.01*rand(N, 1);
samples = sort(samples);
end
end
end
% gumbel.m
classdef gumbel
properties (Access = private)
samples;
end
methods
function obj = gumbel(samples)
obj.samples = samples;
end
function CDF = solve(obj)
x0 = obj.samples;
N = size(x0, 1);
hh = 1.0/(N+1);
y0 = 0.0:hh:1.0;
y0 = y0(2:end-1);
% Least square fitting
x = x0;
y = -log(-log(y0));
coefficient = polyfit(x, y, 1);
y1 = polyval(coefficient, x);
% 0.90 confidence
beta = 1/coefficient(1);
alpha = coefficient(2)*(-beta);
CDF = exp(-exp(-((x0-alpha)/beta)));
target = 0.90;
ultimate = interp1(CDF, x0, target, 'spline');
ultimate_left = ultimate - 1.645*beta/sqrt(N);
ultimate_right = ultimate + 1.645*beta/sqrt(N);
disp('gumbel method : ');
disp(['value = ', num2str(ultimate)]);
disp(['left = ', num2str(ultimate_left)]);
disp(['right = ', num2str(ultimate_right)]);
% plot figure
fig = figure(1);
plts = [];
strs = [];
plt = scatter(x, y);
hold on;
plt.Marker = 'x';
plt.MarkerEdgeColor = 'Black';
plt.LineWidth = 1.5;
str = "samples";
plts = [plts, plt];
strs = [strs, str];
plt = plot(x, y1);
hold on;
plt.Color = 'Black';
plt.LineStyle = '--';
plt.LineWidth = 1.5;
str = "fitting";
plts = [plts, plt];
strs = [strs, str];
xl = xlabel('Heave/H');
yl = ylabel('-ln(-ln(Fx))');
xl.FontSize = 17;
yl.FontSize = 17;
ld = legend(plts, strs);
ld.FontSize = 17;
ld.Box = 'off';
ld.Location = 'best';
saveas(fig, './fig1-3.png');
close(fig);
end
end
end
% weibull.m
classdef weibull
properties (Access = private)
samples;
end
methods
function obj = weibull(samples)
obj.samples = samples;
end
function CDF = solve(obj)
x0 = obj.samples;
N = size(x0, 1);
hh = 1.0/(N+1);
y0 = 0.0:hh:1.0;
y0 = y0(2:end-1);
y0 = y0';
% Least square fitting
x = log(x0);
y = log(-log(1-y0));
coefficient = polyfit(x, y, 1);
y1 = polyval(coefficient, x);
% plot figure
fig = figure(1);
plts = [];
strs = [];
plt = scatter(x, y);
hold on;
plt.Marker = 'x';
plt.MarkerEdgeColor = 'Black';
plt.LineWidth = 1.5;
str = "samples";
plts = [plts, plt];
strs = [strs, str];
plt = plot(x, y1);
hold on;
plt.Color = 'Black';
plt.LineStyle = '--';
plt.LineWidth = 1.5;
str = "fitting";
plts = [plts, plt];
strs = [strs, str];
xl = xlabel('ln(Heave/H)');
yl = ylabel('-ln(-ln(Fx))');
xl.FontSize = 17;
yl.FontSize = 17;
ld = legend(plts, strs);
ld.FontSize = 17;
ld.Box = 'off';
ld.Location = 'best';
saveas(fig, './fig1-4.png');
close(fig);
% 0.90 confidence
k = coefficient(1);
lamda = exp(coefficient(2)/(-k));
CDF = 1-exp(-(x0./lamda).^k);
target = 0.90;
ultimate = interp1(CDF, x0, target, 'spline');
% Bootstrapping m=100
hh = x0(1)/100.0;
x0 = [x0(1)-hh; x0; x0(end)+hh];
CDF_ex = [0; CDF; 1];
ultimates = zeros(100, 1);
rng(1);
CDF_MonteC = random('unif',0,1,N+2,100);
for i = 1 : 100
yi = CDF_MonteC(:,i);
xi = interp1(CDF_ex, x0, yi);
x = log(xi);
y = log(-log(1-yi));
coefficient = polyfit(x, y, 1);
k = coefficient(1);
lamda = exp(coefficient(2)/(-k));
CDFs = 1-exp(-(x0./lamda).^k);
ultimates(i) = interp1(CDFs, xi, target, 'spline');
end
ultimates = sort(ultimates,'descend');
ultimates = ultimates(6:95);
ultimate_right = ultimates(1);
ultimate_left = ultimates(end);
disp('weibull method : ');
disp(['value = ', num2str(ultimate)]);
disp(['left = ', num2str(ultimate_left)]);
disp(['right = ', num2str(ultimate_right)]);
end
end
end