这是数学建模topsis部分的代码,画图部分为了得到想要的图形,查了不少资料,记录一下。
clc;
clear all;
% Taking the input method to initialize the marix
%disp('Input metrix please:');
%X=input('X=');
X=[50.8 4.3 8.7;200.0 4.9 7.2;71.4 2.5 5.0;98.5 3.7 2.7;10.2 2.4 0.3];
% Geting the rows and columns of the matrix
[n,m]=size(X);
% A special way to format and display data
%disp(['There are ' num2str(n) ' evaluation indexes and ' num2str(m) ' evaluation objec']);
% Directly forward data without judgement
%Position=input('Input the column of indictor to be forword processed. For example, columns 2, 3 and 6, enter [2,3,6] please!');
% As we know, the lower the 3 indictors, the batter. All of the are miniature indector.
% Convert minimum to maximum
posit_x=max(X)-X;
disp('After forwarding, the matrix is: ');
disp(posit_x);
% Add the weight to the caculation results
weight = 1/m;
% Normalize the forward matrix
Z=posit_x./repmat(sum(posit_x.*posit_x).^0.5,n,1);
disp('The normalized matrix is:');
disp(Z);
% Calculating the distance from the maximum and the minimum, and calculating the score
D_P=sum([(Z-repmat(max(Z),n,1)).^2].*repmat(weight,n,1),2).^0.5;
D_N=sum([(Z-repmat(min(Z),n,1)).^2].*repmat(weight,n,1),2).^0.5;
S=D_N./(D_P+D_N);
disp('The final score is:(the higher the score, the greater the safety factor!)');
stand_S=S/sum(S);
% [sorted_S,index = sort(stand_S);
% index is the location of the elements in sorted_S
% disp(sorted_S);
disp(stand_S);
% disp('The security ranking of these factories is: ');
% disp();
All_value={'白沙湘水','沈阳田师傅','抚顺龙凤','大同同家山','扎诺尔南山'};
valueset={'白沙湘水','沈阳田师傅','抚顺龙凤','大同同家山','扎诺尔南山'};
x_g=categorical(All_value,valueset,'Ordinal',true);
b=bar(x_g,stand_S);
title('各煤矿厂安全系数得分情况');
for i = 1:5
% text(x_g(i),stand_S(i)+0.001,num2str(stand_S(i))),'HorizontalAlignment','center','VerticalAlignment','bottom','FontSize',8,'FontName','Times New Roman')
text(x_g(i),stand_S(i)+0.001,num2str(stand_S(i)),'HorizontalAlignment','center','VerticalAlignment','bottom','FontSize',8,'FontName','Times New Roman')
end;
运行结果如下图所示:
另外所绘的图如下:
注:
All_value={'白沙湘水','沈阳田师傅','抚顺龙凤','大同同家山','扎诺尔南山'};
valueset={'白沙湘水','沈阳田师傅','抚顺龙凤','大同同家山','扎诺尔南山'};
x_g=categorical(All_value,valueset,'Ordinal',true);
这是用来规定数组的顺序的,不然categorical会按照自己的规则来进行排序;