作业帮 > 综合 > 作业

matlab 用subplot和hold on 以后无法把绘图放在一个窗口里,

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/13 04:48:09
matlab 用subplot和hold on 以后无法把绘图放在一个窗口里,
试验程序运行出来是个效果:

clear all;
n=20;
a=(round(100*rand(n,1))/100)*2.2-0.9;
figure;hold on;subplot(2,2,1);
plot(1:n,a,'rx');
xlabel('样本数');
ylabel('生化化验值');
title('样本数据:生化化验值');hold on;
pause;
%%先验概率
P=[0.9 0.1];
%%作类条件概率曲线p(x/wi)
x=-0.9:0.01:1.3;
px(1,:)=(1/(sqrt(2*pi)*0.3))*exp(-0.5*(x/0.3).^2);
px(2,:)=(1/(sqrt(2*pi)*0.18))*exp(-0.5*((x-1)/0.1).^2);
figure;hold on;subplot(2,2,2);
plot(x,px(1,:),'b',x,px(2,:),'r--');
xlabel('生化化验值');
ylabel('概率密度');
title('类条件概率密度曲线');
axis tight;hold on;
pause;
%%做后验概率曲线
for i=1:2
pwx(i,:)=px(i,:)*P(i)./(px(1,:)*P(1)+px(2,:)*P(2));
end
figure(3);hold on;subplot(2,2,3);
plot(x,pwx(1,:),'b',x,pwx(2,:),'r--');
xlabel('生化化验值')
ylabel('后验概率')
title('后验概率曲线');
axis tight;hold on;
pause
%损失函数
r=[0,1000,000; 200,000,0];
%作条件风险曲线
for i=1:2
    R(i,:)=r(i,1)*pwx(1,:)+r(i,2)*pwx(2,:);
end
figure;hold on;subplot(2,2,4);
plot(x,R(1,:),'b',x,R(2,:),'r--');
xlabel('生化化验值');
ylabel('条件风险');
title('条件风险曲线')
axis tight;hold on;
pause
   
%%计算给定检验值的类条件概率密度
for j=1:n
s=a(j);
PXW1=spline(x,px(1,:),s);
PXW2=spline(x,px(2,:),s);
PXW=[PXW1,PXW2];
disp('样本');
s
%%计算后验概率?判别输出
for i=1:2
Pwx(i)=PXW(i)*P(i)/(PXW(1)*P(1)+PXW(2)*P(2));
end
%计算条件风险,判别输出
for i=1:2
    Rx(i)=r(i,1)*Pwx(1)+r(i,2)*Pwx(2);
end
disp('条件风险R(ai|x)=')
Rx
plot(x,R(1,:),'b',x,R(2,:),'r--');
xlabel('生化化验值');
ylabel('条件风险');
title('条件风险曲线')
hold on
plot(s,Rx(1),'or',s,Rx(2),'ob');
axis tight;
hold off
if Rx(1)<Rx(2)
w(j,1)=s;
disp('正常人')
else
w(j,2)=s;
disp('感染病人')
end
pause;
end
disp('')
disp('正常人 感染病人 ')
w
clear all;
n=20;
a=(round(100*rand(n,1))/100)*2.2-0.9;
figure;
%hold on;
subplot(2,2,1);
plot(1:n,a,'rx');
xlabel('样本数');
ylabel('生化化验值');
title('样本数据:生化化验值');
hold on;
pause;
%%先验概率
P=[0.9 0.1];
%%作类条件概率曲线p(x/wi)
x=-0.9:0.01:1.3;
px(1,:)=(1/(sqrt(2*pi)*0.3))*exp(-0.5*(x/0.3).^2);
px(2,:)=(1/(sqrt(2*pi)*0.18))*exp(-0.5*((x-1)/0.1).^2);
%figure;
%hold on;
subplot(2,2,2);
plot(x,px(1,:),'b',x,px(2,:),'r--');
xlabel('生化化验值');
ylabel('概率密度');
title('类条件概率密度曲线');
axis tight;
hold on;
pause;
%%做后验概率曲线
for i=1:2
pwx(i,:)=px(i,:)*P(i)./(px(1,:)*P(1)+px(2,:)*P(2));
end
%figure(3);
%hold on;
subplot(2,2,3);
plot(x,pwx(1,:),'b',x,pwx(2,:),'r--');
xlabel('生化化验值')
ylabel('后验概率')
title('后验概率曲线');
axis tight;
hold on;
pause
%损失函数
r=[0, 1000, 000; 200, 000, 0];
%作条件风险曲线
for i=1:2
R(i,:)=r(i,1)*pwx(1,:)+r(i,2)*pwx(2,:);
end
%figure;
%hold on;
subplot(2,2,4);
plot(x,R(1,:),'b',x,R(2,:),'r--');
xlabel('生化化验值');
ylabel('条件风险');
title('条件风险曲线')
axis tight;
hold off;
pause

%%计算给定检验值的类条件概率密度
for j=1:n
s=a(j);
PXW1=spline(x,px(1,:),s);
PXW2=spline(x,px(2,:),s);
PXW=[PXW1,PXW2];
disp('样本');
s
%%计算后验概率?判别输出
for i=1:2
Pwx(i)=PXW(i)*P(i)/(PXW(1)*P(1)+PXW(2)*P(2));
end
%计算条件风险,判别输出
for i=1:2
Rx(i)=r(i,1)*Pwx(1)+r(i,2)*Pwx(2);
end
disp('条件风险R(ai|x)=')
Rx
plot(x,R(1,:),'b',x,R(2,:),'r--');
xlabel('生化化验值');
ylabel('条件风险');
title('条件风险曲线')
hold on
plot(s,Rx(1),'or',s,Rx(2),'ob');
axis tight;
hold off
if Rx(1)
再问: 没出图 ,但还是谢谢
再答: 你的程序中加pause,所以,每进行一步,必须按任一键,才能进行下一步,图是一点一点出的
再问: 我这边还是没跑出来图啊……
再答: 运行时,看到左下角出现paused,press any key,就按任一键