%Email:just_for_h264@163.com
%进而可在屏幕上任意取点,程序可输出属于第一类,还是第二类
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
close all
%读入两类训练样本数据
load data
%求两类训练样本的均值和方差
u1=mean(Sample1)
u2=mean(Sample2)
sigm1=cov(Sample1)
sigm2=cov(Sample2)
%计算两个样本的密度函数并显示
x=-20:0.5:40
y= -20:0.5:20
[X,Y] = meshgrid(x,y)
F1 = mvnpdf([X(:),Y(:)],u1,sigm1)
F2 = mvnpdf([X(:),Y(:)],u2,sigm2)
P1=reshape(F1,size(X))
P2=reshape(F2,size(X))
figure(2)
surf(X,Y,P1)
hold on
surf(X,Y,P2)
shading interp
colorbar
title('条件概率密度函数曲线')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%以下为测试部分
%利用ginput随机选取屏幕上的点(可连续取10个点)
%程序可根据点的位置自动地显示出属于那个类
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pw1=0.4pw2=0.6
figure(1)
plot(Sample1(:,1),Sample1(:,2),'r.')
hold on
plot(Sample2(:,1),Sample2(:,2),'b.')
for i=1:10
[u,v]=ginput(1)
plot(u,v,'m*')
P1=pw1*mvnpdf([u,v],u1,sigm1)
P2=pw2*mvnpdf([u,v],u2,sigm2)
hold all
if(P1>P2)
disp('it belong to the first class')
else
disp('it belong to the second class')
end
end
1、首先,建立一个自定义函数文件。如ex_fun.m,其文件内容
function [C,R]=ex_fun(X,Y,A)
syms C R
eq1=((C/((1/2)+X+(1/(0.013*R))))*(1-Y))-R
eq2=((A*(11*(C-40)))/(8*(C+80)))-R
s=solve(eq1,eq2)
C=vpa(s.C,5)
R=vpa(s.R,5)
end
2、再命令窗口或程序文件中,输入下列命令
X=5Y=10A=3
[C,R]=ex_fun(X,Y,A)
3、计算结果
C = 40., -6.929, -84.139
R = 0., -2.649, 123.70
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)