正剧开始:
星历2016年04月17日 10:30:26, 银河系厄尔斯星球中华帝国江南行省。
[工程师阿伟]正在和[机器小伟]一起研究[不等式]。
图中绿色部分取值表示A公司划算,否则就是B公司划算了。
<span style="font-size:18px;"> if (1) { var r = 20; config.setSector(1,1,1,1); config.graphPaper2D(0, 0, r); config.axis2D(0, 0,190); //坐标轴设定 var scaleX = 2*r, scaleY = 2*r; var spaceX = 2, spaceY = 2; var xS = -10, xE = 10; var yS = -10, yE = 10; config.axisSpacing(xS, xE, spaceX, scaleX, 'X'); config.axisSpacing(yS, yE, spaceY, scaleY, 'Y'); // var arrayYes = [], arrayNo= []; //系数阵列 ax^2+bx+c var coeffecient = [1, -5, 0]; var yVal = 0; for (var x = xS; x <= xE; x+=0.3) { yVal = 0; for (var i = 0; i < coeffecient.length; i++) { yVal *= x; yVal += coeffecient[i]; } if (yVal <= 0) { arrayYes.push([x, yVal]); } else { arrayNo.push([x, yVal]); } } var transform = new Transform(); var tmp = []; if (arrayNo.length > 0) { arrayNo = transform.scale(transform.translate(arrayNo, 0, 0), scaleX/spaceX, scaleY/spaceY); //不满足条件的点 tmp = [].concat(arrayNo); shape.pointDraw(tmp, 'blue'); tmp = [].concat(arrayNo); shape.strokeDraw(tmp, 'blue'); } if (arrayYes.length > 0) { arrayYes = transform.scale(transform.translate(arrayYes, 0, 0), scaleX/spaceX, scaleY/spaceY); //满足条件的点 --> 解集 tmp = [].concat(arrayYes); shape.pointDraw(tmp, 'green'); tmp = [].concat(arrayYes); shape.strokeDraw(tmp, 'green'); } } </span>
<span style="font-size:18px;"> if (1) { var mathText = new MathText(); var s = [ '(-b± |Δ^[0.5])/2a', '-b/2a', '[-[INF], (-b- |Δ^[0.5])/2a), ((-b+ |Δ^[0.5])/2a, [INF]]', 'R', '( (-b- |Δ^[0.5])/2a, (-b+ |Δ^[0.5])/2a )', ]; var x =40, y=40; var r1 = 40; var len = s.length; for (var i = 0; i < len; i++) { if (s[i] == '') { if (x < 100) { x += 300; y-=r1*3; } else { x = 20; y += r1; } } else { mathText.printSet(s[i], x, y, 'red'); y+=r1; } } }</span>
<span style="font-size:18px;">function Equation() { this.quadratic = function(array) { //array = [a, b, c] //满足ax2 + bx + c = 0 (a!=0) var a = array[0], b = array[1], c = array[2]; if (a < 0) { a = -a; b = -b; c = -c; } var p = 0, q = 0, delta = 0; var x1 = 0, x2 = 0; var s = ''; if (a == 0) { s = '方程 '+b.toString()+'x + '+c.toString()+' = 0 =>'; document.write(s+'<br/>'); x1 = -c / b; s = '方程的解为:x = '+x1.toString()+' ;'; } else { s = '方程 '+a.toString()+'x^[2] + '+b.toString()+'x + '+c.toString()+' = 0 =>'; document.write(s+'<br/>'); delta = b*b-4*a*c; s = 'Δ = b^[2] - 4ac = ' + delta.toString()+' ;'; document.write(s+'<br/>'); if (delta < 0) { s = '没有实数解。'; document.write(s+'<br/>'); } else { if (Math.abs(delta) < 0.000001) { x1 = x2 = -b/(2*a); s = '方程的解为:x1 = x2 = '+x1.toString()+' ;'; } else if (delta > 0) { x1 = (-b-Math.sqrt(delta))/(2*a); x2 = (-b+Math.sqrt(delta))/(2*a); s = '方程的解为:x1 = '+x1.toString()+', x2 = '+x2.toString()+' ;'; } document.write(s+'<br/>'); p = x1+x2; q = x1*x2; s = '方程根与系数的关系:x1 + x2 = ' + p.toString()+', x1 * x2 = '+q.toString()+' ;'; document.write(s+'<br/>'); document.write(''+'<br/>'); } } } } if (1) { var equation = new Equation(); equation.quadratic([4, -4, 1]); } 方程 4x^[2] + -4x + 1 = 0 => Δ = b^[2] - 4ac = 0 ; 方程的解为:x1 = x2 = 0.5 ; 方程根与系数的关系:x1 + x2 = 1, x1 * x2 = 0.25 ; if (1) { var equation = new Equation(); equation.quadratic([-1, 2, -3]); } 方程 1x^[2] + -2x + 3 = 0 => Δ = b^[2] - 4ac = -8 ; 没有实数解。 </span>
<span style="font-size:18px;"> if (1) { var r = 20; config.setSector(1,1,1,1); config.graphPaper2D(0, 0, r); config.axis2D(0, 0,190); //坐标轴设定 var scaleX = 2*r, scaleY = 2*r; var spaceX = 2, spaceY = 2; var xS = -10, xE = 10; var yS = -10, yE = 10; config.axisSpacing(xS, xE, spaceX, scaleX, 'X'); config.axisSpacing(yS, yE, spaceY, scaleY, 'Y'); // var arrayYes = [], arrayNo= []; //系数阵列 ax^2+bx+c var coeffecient = [4, -4, 1]; var yVal = 0; for (var x = xS; x <= xE; x+=0.3) { yVal = 0; for (var i = 0; i < coeffecient.length; i++) { yVal *= x; yVal += coeffecient[i]; } if (yVal > 0) { arrayYes.push([x, yVal]); } else { arrayNo.push([x, yVal]); } } var transform = new Transform(); var tmp = []; if (arrayNo.length > 0) { arrayNo = transform.scale(transform.translate(arrayNo, 0, 0), scaleX/spaceX, scaleY/spaceY); //不满足条件的点 tmp = [].concat(arrayNo); shape.pointDraw(tmp, 'blue'); tmp = [].concat(arrayNo); shape.strokeDraw(tmp, 'blue'); } if (arrayYes.length > 0) { arrayYes = transform.scale(transform.translate(arrayYes, 0, 0), scaleX/spaceX, scaleY/spaceY); //满足条件的点 --> 解集 tmp = [].concat(arrayYes); shape.pointDraw(tmp, 'green'); tmp = [].concat(arrayYes); shape.strokeDraw(tmp, 'green'); } } </span>
蓝色表示非解集,所以这个无解。
<span style="font-size:18px;"> if (1) { var r = 20; config.setSector(1,1,1,1); config.graphPaper2D(0, 0, r); config.axis2D(0, 0,190); //坐标轴设定 var scaleX = 2*r, scaleY = 2*r; var spaceX = 2, spaceY = 2; var xS = -10, xE = 10; var yS = -10, yE = 10; config.axisSpacing(xS, xE, spaceX, scaleX, 'X'); config.axisSpacing(yS, yE, spaceY, scaleY, 'Y'); // var arrayYes = [], arrayNo= []; //系数阵列 ax^2+bx+c var coeffecient = [-1, 2, -3]; var yVal = 0; for (var x = xS; x <= xE; x+=0.3) { yVal = 0; for (var i = 0; i < coeffecient.length; i++) { yVal *= x; yVal += coeffecient[i]; } if (yVal > 0) { arrayYes.push([x, yVal]); } else { arrayNo.push([x, yVal]); } } var transform = new Transform(); var tmp = []; if (arrayNo.length > 0) { arrayNo = transform.scale(transform.translate(arrayNo, 0, 0), scaleX/spaceX, scaleY/spaceY); //不满足条件的点 tmp = [].concat(arrayNo); shape.pointDraw(tmp, 'blue'); tmp = [].concat(arrayNo); shape.strokeDraw(tmp, 'blue'); } if (arrayYes.length > 0) { arrayYes = transform.scale(transform.translate(arrayYes, 0, 0), scaleX/spaceX, scaleY/spaceY); //满足条件的点 --> 解集 tmp = [].concat(arrayYes); shape.pointDraw(tmp, 'green'); tmp = [].concat(arrayYes); shape.strokeDraw(tmp, 'green'); } }</span>
<span style="font-size:18px;"> if (1) { var r = 20; config.setSector(1,1,1,1); //坐标轴设定 var scaleX = 2*r, scaleY = 2*r; var spaceX = 2, spaceY = 2; var xS = -10, xE = 10; var yS = -10, yE = 10; // var arrayYes = [], arrayNo= []; //系数阵列 ax + by + c var coeffecient = [1, 4, -4]; var yes = 0, no = 0; for (var x = xS; x <= xE; x+=0.5) { for (var y = yS; y <= yE; y+=0.5) { //条件 yes = (coeffecient[0]*x+coeffecient[1]*y+coeffecient[2] < 4); if (yes) { arrayYes.push([x, y]); } else { arrayNo.push([x, y]); } } } var transform = new Transform(); var tmp = []; if (arrayNo.length > 0) { arrayNo = transform.scale(transform.translate(arrayNo, 0, 0), scaleX/spaceX, scaleY/spaceY); //不满足条件的点 tmp = [].concat(arrayNo); shape.pointDraw(tmp, '#CC88FF'); /*tmp = [].concat(arrayNo); shape.strokeDraw(tmp, 'blue'); */ } if (arrayYes.length > 0) { arrayYes = transform.scale(transform.translate(arrayYes, 0, 0), scaleX/spaceX, scaleY/spaceY); //满足条件的点 --> 解集 tmp = [].concat(arrayYes); shape.pointDraw(tmp, '#CCFF88'); /*tmp = [].concat(arrayYes); shape.strokeDraw(tmp, 'green'); */ } config.graphPaper2D(0, 0, r); config.axis2D(0, 0,190); config.axisSpacing(xS, xE, spaceX, scaleX, 'X'); config.axisSpacing(yS, yE, spaceY, scaleY, 'Y'); } </span>
<span style="font-size:18px;"> if (1) { var r = 20; config.setSector(1,1,1,1); //坐标轴设定 var scaleX = 2*r, scaleY = 2*r; var spaceX = 3, spaceY = 5; var xS = -10, xE = 20; var yS = -10, yE = 20; // var arrayYes = [], arrayNo= []; //系数阵列 ax + by + c var coeffecient = []; var yes = 0, no = 0; for (var x = xS; x <= xE; x+=0.5) { for (var y = yS; y <= yE; y+=0.5) { //条件 yes = (2*x + y >= 15) && (x+2*y>=18) && (x+3*y>=27) && (x >= 0) && (y >= 0); if (yes) { arrayYes.push([x, y]); } else { arrayNo.push([x, y]); } } } var transform = new Transform(); var tmp = []; if (arrayNo.length > 0) { arrayNo = transform.scale(transform.translate(arrayNo, 0, 0), scaleX/spaceX, scaleY/spaceY); //不满足条件的点 tmp = [].concat(arrayNo); shape.pointDraw(tmp, '#CC88FF'); /*tmp = [].concat(arrayNo); shape.strokeDraw(tmp, 'blue'); */ } if (arrayYes.length > 0) { arrayYes = transform.scale(transform.translate(arrayYes, 0, 0), scaleX/spaceX, scaleY/spaceY); //满足条件的点 --> 解集 tmp = [].concat(arrayYes); shape.pointDraw(tmp, '#CCFF88'); /*tmp = [].concat(arrayYes); shape.strokeDraw(tmp, 'green'); */ } config.graphPaper2D(0, 0, r); config.axis2D(0, 0,190); config.axisSpacing(xS, xE, spaceX, scaleX, 'X'); config.axisSpacing(yS, yE, spaceY, scaleY, 'Y'); } </span>
<span style="font-size:18px;"> if (1) { var r = 20; config.setSector(1,1,1,1); //坐标轴设定 var scaleX = 2*r, scaleY = 2*r; var spaceX = 1, spaceY = 1; var xS = 0, xE = 5; var yS = 0, yE = 5; // var arrayYes = [], arrayNo= []; //系数阵列 ax + by + c var coeffecient = []; var yes = 0, no = 0; for (var x = xS; x <= xE; x+=1) { for (var y = yS; y <= yE; y+=1) { //条件 yes = (x+2*y<=8) && (4*x<=16) && (4*y<=12) && (x >= 0) && (y >= 0); if (yes) { arrayYes.push([x, y]); } else { arrayNo.push([x, y]); } } } //可行域与最优解 var size = arrayYes.length; var feasible = []; for (var i = 0; i < size; i++) { feasible.push([arrayYes[i][0], arrayYes[i][1], arrayYes[i][0]*2+arrayYes[i][1]*3]); } feasible.sort(function(a, b) { //求最小值为最优时不加负号 //最大值为最优时加负号 return -(a[2]-b[2]); }); var best = [feasible[0][0]*scaleX/spaceX, feasible[0][1]*scaleY/spaceY]; var s = '最优解是:'+feasible[0][0].toFixed(3)+' , ' + feasible[0][1].toFixed(3)+' , ' +feasible[0][2].toFixed(3); plot.fillText(s, -270, -170, 200); //区域图 var transform = new Transform(); var tmp = []; if (arrayNo.length > 0) { arrayNo = transform.scale(transform.translate(arrayNo, 0, 0), scaleX/spaceX, scaleY/spaceY); //不满足条件的点 tmp = [].concat(arrayNo); shape.pointDraw(tmp, '#CC88FF'); /*tmp = [].concat(arrayNo); shape.strokeDraw(tmp, 'blue'); */ } if (arrayYes.length > 0) { arrayYes = transform.scale(transform.translate(arrayYes, 0, 0), scaleX/spaceX, scaleY/spaceY); //满足条件的点 --> 解集 tmp = [].concat(arrayYes); shape.pointDraw(tmp, '#CCFF88'); /*tmp = [].concat(arrayYes); shape.strokeDraw(tmp, 'green'); */ } //最优点绘制 shape.pointDraw(best, 'red'); //坐标轴标注 config.graphPaper2D(0, 0, r); config.axis2D(0, 0,190); config.axisSpacing(xS, xE, spaceX, scaleX, 'X'); config.axisSpacing(yS, yE, spaceY, scaleY, 'Y'); }</span>
这个Excel解决的是这个问题:
<span style="font-size:18px;"> if (1) { var r = 20; config.setSector(10,5,9.5,1); //坐标轴设定 var scaleX = 2*r, scaleY = 2*r; var spaceX = 0.1, spaceY = 0.1; var xS = 0, xE = 1; var yS = 0, yE = 0.8; // var arrayYes = [], arrayNo= []; //系数阵列 ax + by + c var coeffecient = []; var yes = 0, no = 0; for (var x = xS; x <= xE; x+=0.01) { for (var y = yS; y <= yE; y+=0.01) { //条件 yes = (0.105*x+0.105*y>=0.075) && (0.07*x+0.14*y>=0.06) && (0.14*x+0.07*y>=0.06) && (x >= 0) && (y >= 0); if (yes) { arrayYes.push([x, y]); } else { arrayNo.push([x, y]); } } } //可行域与最优解 var size = arrayYes.length; var feasible = []; for (var i = 0; i < size; i++) { feasible.push([arrayYes[i][0], arrayYes[i][1], arrayYes[i][0]*28+arrayYes[i][1]*21]); } feasible.sort(function(a, b) { return (a[2]-b[2]); }); var best = [feasible[0][0]*scaleX/spaceX, feasible[0][1]*scaleY/spaceY]; //区域图 var transform = new Transform(); var tmp = []; if (arrayNo.length > 0) { arrayNo = transform.scale(transform.translate(arrayNo, 0, 0), scaleX/spaceX, scaleY/spaceY); //不满足条件的点 tmp = [].concat(arrayNo); shape.pointDraw(tmp, '#CC88FF'); /*tmp = [].concat(arrayNo); shape.strokeDraw(tmp, 'blue'); */ } if (arrayYes.length > 0) { arrayYes = transform.scale(transform.translate(arrayYes, 0, 0), scaleX/spaceX, scaleY/spaceY); //满足条件的点 --> 解集 tmp = [].concat(arrayYes); shape.pointDraw(tmp, '#CCFF88'); /*tmp = [].concat(arrayYes); shape.strokeDraw(tmp, 'green'); */ } //最优点绘制 shape.pointDraw(best, 'red'); //坐标轴标注 config.graphPaper2D(0, 0, r); config.axis2D(0, 0,350); config.axisSpacing(xS, xE, spaceX, scaleX, 'X'); config.axisSpacing(yS, yE, spaceY, scaleY, 'Y'); var s = '最优解是:'+feasible[0][0].toFixed(3)+' , ' + feasible[0][1].toFixed(3)+' , ' +feasible[0][2].toFixed(3); plot.setFillStyle('blue') .fillText(s, 270, -340, 200); }</span>
本节到此结束,欲知后事如何,请看下回分解。