實驗目的:
學會運用Mathematica、Lingo或Matlab軟件求解非線性規划模型。
實驗步驟:
實驗步驟要有模型建立,模型求解、結果分析。
實驗內容:
(1)某廠向用戶提供發動機,合同規定,第一、二、三季度末分別交貨40台、60台、80台.每季度的生產費用為f(x)=ax+bx2(元),其中x是該季生產的台數.若交貨后有剩余,可用於下季度交貨,但需支付存儲費,每台每季度c元.已知工廠每季度最大生產能力為100台,第一季度開始時無存貨,設a=50、b=0.2、c=4,問工廠應如何安排生產計划,才能既滿足合同又使總費用最低.討論a、b、c變化對計划的影響,並作出合理的解釋。
(2)一基金管理人的工作是,每天將現有的美元、英鎊、馬克、日元四種貨幣按當天匯率相互兌換,使在滿足需要的條件下,按美元計算的價值最高.設某天的匯率、現有貨幣和當天需求如下:
|
美元 |
英鎊 |
馬克 |
日元 |
現有量 |
需求量 |
美元 |
1 |
.58928 |
1.743 |
138.3 |
8 |
6 |
英鎊 |
1.697 |
1 |
2.9579 |
234.7 |
1 |
3 |
馬克 |
.57372 |
.33808 |
1 |
79.346 |
8 |
1 |
日元 |
.007233 |
.00426 |
.0126 |
1 |
0 |
10 |
問該天基金管理人應如何操作(“按美元計算的價值”指兌入、兌出匯率的平均值,如1英鎊相當於(1.697+(1/0.58928))/2=1.696993美元).
實驗步驟:
使用Lingo求解:

1 model: 2 min=50*x1+0.2*(x1^2)+4*(x1-40)+50*x2+0.2*(x2^2)+4*(x1+x2-100)+50*x3+0.2*(x3^2); 3 x1>=40; 4 (x1-40)+x2>=60; 5 (x1-40)+x2-60+x3=80; 6 0<=x1;x1<=100; 7 0<=x2;x2<=100; 8 0<=x3;x3<=100; 9 @gin(x1); 10 @gin(x2); 11 @gin(x3); 12 end
運行截圖:
使用Mathematica求解,

1 (*非線性一,1*) 2 Minimize[{50*Subscript[x, 1]+0.2*\!\(\*SubsuperscriptBox[\(x\), \(1\), \(2\)]\)+4*(Subscript[x, 1]-40)+50*Subscript[x, 2]+0.2*\!\(\*SubsuperscriptBox[\(x\), \(2\), \(2\)]\)+4*(Subscript[x, 1]+Subscript[x, 2]-100)+50*Subscript[x, 3]+0.2*\!\(\*SubsuperscriptBox[\(x\), \(3\), \(2\)]\), 3 Subscript[x, 1]>=40,Subscript[x, 1]-40+Subscript[x, 2]>=60,Subscript[x, 1]-40+Subscript[x, 2]-60+Subscript[x, 3]==80, 4 0<=Subscript[x, 1] && Subscript[x, 1]<=100,0<=Subscript[x, 2] && Subscript[x, 2]<=100,0<=Subscript[x, 3] && Subscript[x, 3]<=100, 5 Subscript[x, 1]\[Element]Integers,Subscript[x, 2]\[Element]Integers,Subscript[x, 3]\[Element]Integers 6 },{Subscript[x, 1],Subscript[x, 2],Subscript[x, 3]}]
使用MATLAB求解,

1 %電動機生產的非線性規划問題 2 %目標函數 3 H=[0.4 0 0;0 0.4 0;0 0 0.4];%二次項 4 c=[58;54;50];%一次項 5 %約束條件 6 A=[1 0 0;1 1 0;];%不等式項 7 b=[40;100]; 8 Aeq=[1 1 1];%使用時取負,使得不等式符號改變 9 beq=[180]; 10 [x,z]=quadprog(H,c,-A,-b,Aeq,beq,[0;0;0],[100;100;100]); 11 x 12 z-560%把常數項減掉
解得第一季度生產50台發動機,第二季度生產60台發動機,第三季度生產70台發動機,所花總成本最小,為11280元。
使用Lingo求解,

1 model: 2 sets: 3 row/1..4/:v,n; 4 Links(row,row):r; 5 endsets 6 data: 7 v=8,1,8,0; 8 n=6,3,1,10; 9 r=1,0.58928,1.743,138.3, 10 1.697,1,2.9579,234.7, 11 0.57372,0.33808,1,79.346, 12 0.007233,0.00426,0.0126,1; 13 enddata 14 y1=(r(2,1)+1/r(1,2))/2; 15 y2=(r(3,1)+(1/r(1,3)))/2; 16 y3=(r(4,1)+(1/r(1,4)))/2; 17 max=x1+x2*r(1,2)*y1+x3*r(1,3)*y2+x4*r(1,4)*y3+x5*r(2,1)+x6*y1+x7*r(2,3)*y2+x8*r(2,4)*y3+x9*r(3,1)+x10*r(3,2)*y1+x11*y2+x12*r(3,4)*y3; 18 x1+x2+x3+x4=8; 19 x5+x6+x7+x8=1; 20 x9+x10+x11+x12=8; 21 x1+r(2,1)*x5+r(3,1)*x9>=6; 22 r(1,2)*x2+x6+r(3,2)*x10>=3; 23 r(1,3)*x3+r(2,3)*x7+x11>=1; 24 r(1,4)*x4+r(2,4)*x8+r(3,4)*x12>=10; 25 end
使用Mathematica求解

1 (*非線性一,2*) 2 Maximize[{Subscript[x, 1]+Subscript[x, 2]*0.58928*1.69699+Subscript[x, 3]*1.743*0.573722+Subscript[x, 4]*138.3*0.00723183+ 3 Subscript[x, 5]*1.697+Subscript[x, 6]*1.69699+Subscript[x, 7]*2.9579*0.573722+Subscript[x, 8]*234.7*0.00723183+Subscript[x, 9]*0.57372+Subscript[x, 10]*0.33808*1.69699+ 4 Subscript[x, 11]*0.573722+Subscript[x, 12]*79.346*0.00723183, 5 Subscript[x, 1]+Subscript[x, 2]+Subscript[x, 3]+Subscript[x, 4]==8, 6 Subscript[x, 5]+Subscript[x, 6]+Subscript[x, 7]+Subscript[x, 8]==1, 7 Subscript[x, 9]+Subscript[x, 10]+Subscript[x, 11]+Subscript[x, 12]==8, 8 Subscript[x, 1]+1.697*Subscript[x, 5]+0.57372*Subscript[x, 9]>=6, 9 0.58928*Subscript[x, 2]+Subscript[x, 6]+0.33808*Subscript[x, 10]>=3, 10 1.743*Subscript[x, 3]+2.9579*Subscript[x, 7]+Subscript[x, 11]>=1, 11 138.3*Subscript[x, 4]+234.7*Subscript[x, 8]+79.346*Subscript[x, 12]>=10, 12 Subscript[x, 1]>=0,Subscript[x, 2]>=0,Subscript[x, 3]>=0,Subscript[x, 4]>=0,Subscript[x, 5]>=0,Subscript[x, 6]>=0,Subscript[x, 7]>=0,Subscript[x, 8]>=0,Subscript[x, 9]>=0,Subscript[x, 10]>=0,Subscript[x, 11]>=0,Subscript[x, 12]>=0 13 },{Subscript[x, 1],Subscript[x, 2],Subscript[x, 3],Subscript[x, 4],Subscript[x, 5],Subscript[x, 6],Subscript[x, 7],Subscript[x, 8],Subscript[x, 9],Subscript[x, 10],Subscript[x, 11],Subscript[x, 12]}]
結果為,
小結:
未知量的設置應該盡量減小目標函數的復雜程度,能使用一元的就不使用多元,能使用一次方程的就使用一次方程,能使用多項式的就不使用帶對數或三角函數的方程。在編程解決問題時,必須考慮式子的靈敏度,誤差限。