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
| matlab代码: (1)编辑mengte.m文件
function [f,g] = mengte(x) f = x(1)^2 + x(2)^2 + 3*x(3)^2 + 4*x(4)^2 + 2*x(5)^2 - 8*x(1) - 2*x(2) - 3*x(3) - x(4) - 2*x(5); g(1) = sum(x) - 400; g(2) = x(1) + 2*x(2) + 2*x(3) + x(4) + 6*x(5) - 800; g(3) = 2*x(1) + x(2) + 6*x(3) - 200; g(4) = x(3) + x(4) + 5*x(5) - 200; end (2)matlab执行代码
tic p0 = 0; for i = 1 : 10^5 x = 99 * rand(5,1); x1 = floor(x); x2 = ceil(x); [f,g] = mengte(x1); if sum(g <= 0) == 4 if f >= p0 x0 = x1; p0 = f; end end [f,g] = mengte(x2); if sum(g <= 0) == 4 if f >= p0; x0 = x2; p0 = f; end end end x0,p0 toc
|