本文将记录《结构可靠度理论》的一些代码及课堂笔记,采用Matlab编程

1 概率分布密度(pdf、cdf)

自己编写函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
clc
clear
close all
% unif
a=1;
b=3;
x=1:0.1:3;
for i=1:length(x)
fx(i)=1/(b-a);
Fx(i)=(x(i)-a)/(b-a);
end
h1=figure(1);
ax1=axes('Parent',h1,'Position',[0.1,0.4,0.35,0.35],'FontSize',10);
plot(x,fx,'r-')
xlabel('x','fontsize',10)
ylabel('pdf','FontSize',10)

ax2=axes('Parent',h1,'Position',[0.55,0.4,0.35,0.35],'FontSize',10);
plot(x,Fx,'r-')
xlabel('x','fontsize',10)
ylabel('cdf','FontSize',10)

%-------
mux=1;sigx=1;
x=-2:0.1:4;
syms t
for i=1:length(x)
tx=x(i);
a1=1/(sigx*(2*pi)^0.5);
b1=exp(-0.5*((tx-mux)/sigx)^2);
fx(i)=a1*b1;
c1=int(exp(-0.5*((t-mux)/sigx)^2),t,-inf,tx);
Fx(i)=a1*c1;
end

h1=figure(2);
ax1=axes('Parent',h1,'Position',[0.1,0.4,0.35,0.35],'FontSize',10);
plot(x,fx,'r-')
xlabel('x','fontsize',10)
ylabel('pdf','FontSize',10)

ax2=axes("Parent",h1,"Position",[0.55,0.4,0.35,0.35],"FontSize",10);
plot(x,Fx,'r-')
xlabel('x','fontsize',10)
ylabel('cdf','FontSize',10)

20220320173541-2022-03-20-17-35-41

20220320173606-2022-03-20-17-36-07

调用Matlab自带函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
clc
clear
close all
%-----
a=1;b=3;
x=-2:0.1:5;
fx=unifpdf(x,a,b);
Fx=unifcdf(x,a,b);
figure(1)
plot(x,fx,'-r')
hold on
plot(x,Fx,'--b')

%---------
mux=0;sigx=1;
x=-3:0.1:3;
fx=normpdf(x,mux,sigx);
Fx=normcdf(x,mux,sigx);

figure(2)
plot(x,fx,'r-')
hold on
plot(x,Fx,'r-')

20220320174416-2022-03-20-17-44-17

20220320174633-2022-03-20-17-46-33

调用Matlab函数抽样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
clc
clear
close all
%-----
a=1;b=3;
ns=1e7;
x=unifrnd(a,b,ns,1);
figure(1)
subplot(1,2,1)
histogram(x)

mux=0;sigx=1;
y=normrnd(mux,sigx,ns,1);
subplot(1,2,2)
histogram(y)

20220320175010-2022-03-20-17-50-11

课程作业

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
clc
clear
close all
%-----
mux1=0;sigx1=1;
mux2=1;sigx2=2;
x=-5:0.1:7;
fx=normpdf(x,mux2,sigx2);
Fx=normcdf(x,mux2,sigx2);
h1=figure(1);
ax1=axes('Parent',h1,'Position',[0.2,0.55,0.6,0.35],'FontSize',10);
plot(x,fx,'r-')
xlabel('x2','FontSize',10)
ylabel('x2-pdf','FontSize',10)

ax2=axes('Parent',h1,'Position',[0.2,0.1,0.6,0.35],'FontSize',10);
plot(x,Fx,'r-')
xlabel('x2','FontSize',10)
ylabel('x2-cdf','FontSize',10)

ns=1e7;

y1=normrnd(mux1,sigx1,ns,1);
y2=normrnd(mux2,sigx2,ns,1);
y=y1+2*y2;
figure(2)
histogram(y)

20220320175543-2022-03-20-17-55-44

20220320175559-2022-03-20-17-55-59