summaryrefslogtreecommitdiff
path: root/code/fmincon
diff options
context:
space:
mode:
Diffstat (limited to 'code/fmincon')
-rw-r--r--code/fmincon/Tankprob.sci95
-rw-r--r--code/fmincon/spring.sci120
2 files changed, 215 insertions, 0 deletions
diff --git a/code/fmincon/Tankprob.sci b/code/fmincon/Tankprob.sci
new file mode 100644
index 0000000..500090e
--- /dev/null
+++ b/code/fmincon/Tankprob.sci
@@ -0,0 +1,95 @@
+//Design a circular tank, closed at both ends, with a volume of 200 m3.The cost is proportional to the surface area of material, which is priced
+//at $400/m2. The tank is contained within a shed with a sloping roof,thus the height of the tank h is limited by h ≤ 12 − d/2, where d is
+//the tank diameter. Formulate the minimum cost problem and solve the design problem.
+//Ref:Diwekar, Urmila,Introduction to Applied Optimization, Introduction to Applied Optimization, Editor:Ding-Zhu Du, Springer Optimization and Its Applications Springer Optimization and Its Applications, VOL 22, Chapter 3
+
+// Copyright (C) 2018 - IIT Bombay - FOSSEE
+// This file must be used under the terms of the CeCILL.
+// This source file is licensed as described in the file COPYING, which
+// you should have received as part of this distribution. The terms
+// are also available at
+// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+// Author:Debasis Maharana
+// Organization: FOSSEE, IIT Bombay
+// Email: toolbox@scilab.in
+//======================================================================
+
+clc;
+
+function cost = Tankprob(x)
+ r = x(1);h = x(2);
+ cost = (2*%pi*r*h+2*%pi*r^2)*400;
+endfunction
+
+function [ceq,c] = Nonlinearcon(x)
+ r = x(1);h = x(2);
+ c = [];
+ ceq = 200-%pi*r^2*h;
+endfunction
+
+function y=Gradobj(x)
+ y= [800*%pi*x(2) + 1600*%pi*x(1),800*%pi*x(1)];
+endfunction
+
+mprintf('\nDesign a circular tank, closed at both ends, with a volume of 200 m3 with minimum cost.\n The tank is contained within a shed with a sloping roof,thus the height of the tank h is limited')
+mprintf('\nCost of material is: %f',400);
+mprintf('The design variables are radius and height of the tank')
+
+A = [1 1];b = 12;
+
+x0 = input('Enter initial guess as vector:');
+if (sum(x0<=0) | (length(x0)~=2))
+ x0 = [1 2];
+ mprintf('Incorrect initial guess...\n changing initial guess to r = %d and h = %d',x0(1),x0(2));
+end
+
+
+lb = [0 0];
+input('press enter to continue')
+options=list("MaxIter",1000,"GradObj", Gradobj);
+
+[xopt,fopt,exitflag,output1] = fmincon(Tankprob,x0,A,b,[],[],lb,[],Nonlinearcon,options);
+
+[xopt1,fopt1,exitflag1,output2] = fmincon(Tankprob,x0,A,b,[],[],lb,[],Nonlinearcon);
+
+clc
+select exitflag
+case 0
+ mprintf('Optimal Solution Found');
+ mprintf('\nThe optimal radius and height of the tank are %f m and %f m',xopt(1),xopt(2));
+ mprintf('\nThe volume of the tank is %f m^3',%pi*xopt(1)^2*xopt(2));
+ mprintf('\nThe total surface area and cost of the tank are %f m^2 and %f $',fopt/400,fopt)
+ mprintf('\nTime taken to solve the problem with gradient information is %f s and without gradient information is %f s',output1.Cpu_Time,output2.Cpu_Time);
+case 1
+ mprintf('Maximum Number of Iterations Exceeded. Output may not be optimal');
+ input('press enter to view results');
+ printf('\nThe optimal radius and height of the tank are %f m and %f m',xopt(1),xopt(2));
+ mprintf('\nThe volume of the tank is %f m^3',%pi*xopt(1)^2*xopt(2));
+ mprintf('\nThe total surface area and cost of the tank are %f m^2 and %f $',fopt/400,fopt)
+case 2
+ mprintf('Maximum amount of CPU Time exceeded. Output may not be optimal.');
+ input('press enter to view results');
+ printf('\nThe optimal radius and height of the tank are %f m and %f m',xopt(1),xopt(2));
+ mprintf('\nThe volume of the tank is %f m^3',%pi*xopt(1)^2*xopt(2));
+ mprintf('\nThe total surface area and cost of the tank are %f m^2 and %f $',fopt/400,fopt)
+case 3
+ mprintf('Stop at Tiny Step');
+ input('press enter to view results');
+ printf('\nThe optimal radius and height of the tank are %f m and %f m',xopt(1),xopt(2));
+ mprintf('\nThe volume of the tank is %f m^3',%pi*xopt(1)^2*xopt(2));
+ mprintf('\nThe total surface area and cost of the tank are %f m^2 and %f $',fopt/400,fopt)
+case 4
+ mprintf('Solved To Acceptable Level');
+ input('press enter to view results');
+ printf('\nThe optimal radius and height of the tank are %f m and %f m',xopt(1),xopt(2));
+ mprintf('\nThe volume of the tank is %f m^3',%pi*xopt(1)^2*xopt(2));
+ mprintf('\nThe total surface area and cost of the tank are %f m^2 and %f $',fopt/400,fopt)
+case 5
+ mprintf('Converged to a point of local infeasibility.');
+ input('press enter to view results');
+ printf('\nThe optimal radius and height of the tank are %f m and %f m',xopt(1),xopt(2));
+ mprintf('\nThe volume of the tank is %f m^3',%pi*xopt(1)^2*xopt(2));
+ mprintf('\nThe total surface area and cost of the tank are %f m^2 and %f $',fopt/400,fopt)
+end
+
+
diff --git a/code/fmincon/spring.sci b/code/fmincon/spring.sci
new file mode 100644
index 0000000..0409dce
--- /dev/null
+++ b/code/fmincon/spring.sci
@@ -0,0 +1,120 @@
+//The problem consists of minimizing the weight of a tension/compression spring subject to constraints on
+//minimum deflection, shear stress, surge frequency, limits on outside diameter and on design variables. The design
+//variables are the mean coil diameter D, the wire diameter d and the number of active coils N. The problem can be
+//expressed as follows:
+//Min f(x) = ( N + 2)*D*d^2
+//ST
+//g1(x) = 1-D^3*N/(71785*d^4) <= 0;
+//g2(x) = 1-140.45*d/(D^2*N) <= 0;
+//g3(x) = (4*D^2-d*D)/(12566*(d^3*D-d^4)) + 1/(5108*d^2)-1 <= 0;
+//g4(x) = (D+d)/1.5 - 1 <=0;
+//The following ranges for the variables were used
+//0.05 <= d <= 2.0, 0.25<= D <=1.3, 2.0<=N<=15.0
+//
+//Ref:Arora, J. S.. Introduction IO optimum design New York McGraw-Hill, 1989
+//======================================================================
+// Copyright (C) 2018 - IIT Bombay - FOSSEE
+// This file must be used under the terms of the CeCILL.
+// This source file is licensed as described in the file COPYING, which
+// you should have received as part of this distribution. The terms
+// are also available at
+// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+// Author:Debasis Maharana
+// Organization: FOSSEE, IIT Bombay
+// Email: toolbox@scilab.in
+//======================================================================
+clc;
+
+function y = spring(x)
+ y = (x(3)+2)*x(1)^2*x(2);
+endfunction
+
+function [C,Ceq] = Nonlinconstraint(x)
+ C(1) = 1-x(2)^3*x(3)/(71785*x(1)^4);
+ C(2) = 1-140.45*x(1)/(x(2)^2*x(3));
+ C(3) = (4*x(2)^2-x(1)*x(2))/(12566*(x(1)^3*x(2)-x(1)^4)) + 1/(5108*x(1)^2)-1;
+ Ceq = [];C = C';
+endfunction
+
+function f = fGrad(x)
+ f = [2*x(1)*x(2)*(x(3)+2) x(1)^2*(x(3)+2) x(1)^2*x(2)];
+endfunction
+
+mprintf('minimizing the weight of a tension/compression spring ');
+mprintf('\n\n Design variables include mean coil diameter:D, the wire diameter:d and the number of active coils:N ');
+disp('The objective is :( N + 2)*D*d^2');
+mprintf('\nNon-linear constraints are on minimum deflection, shear stress, surge frequency,');
+mprintf('\nlinear constraint is on outside diameter');
+
+
+A = [1 1 0];
+b = 1.5;
+mprintf('Bounds on the variable are as follows');
+
+lb = [0.05 0.25 2];
+ub = [2 1.3 15];
+table = [['bounds', 'lb','ub'];[['d';'D';'N'],string(lb'),string(ub')]];
+disp(table);
+x0 = lb + rand(1)*(ub-lb);
+disp(x0,'Initial Guess is :');
+
+input('press enter to continue');
+
+mprintf('Scilab is solving your problem');
+options=list("MaxIter", [1500], "CpuTime", [500], "GradObj", fGrad);
+
+[xopt,fval,exitflag,output] =fmincon(spring, x0,A,b,[],[],lb,ub,Nonlinconstraint,options);
+
+clc
+select exitflag
+case 0
+ mprintf('Optimal Solution Found');
+ mprintf('\n\nThe optimal design parameters are d = %f ,D = %f and N = %f m',xopt(1),xopt(2),xopt(3));
+ mprintf('\n\nThe optimal objective is %f ',fval);
+ mprintf('\n\nIterations completed %d',output.Iterations);
+ mprintf('\nTotal CPU time %f',output.Cpu_Time);
+ mprintf('\nTotal Functional Evaluations %d',output.Objective_Evaluation);
+case 1
+ mprintf('Maximum Number of Iterations Exceeded. Output may not be optimal');
+ input('press enter to view results');
+ mprintf('\nThe optimal design parameters are d = %f ,D = %f and N = %f m',xopt(1),xopt(2),xopt(3));
+ mprintf('\nThe optimal objective is %f ',fval);
+ mprintf('\nIterations completed %d',output.Iterations);
+ mprintf('\nTotal CPU time %f',output.Cpu_Time);
+ mprintf('\nTotal Functional Evaluations %d',output.Objective_Evaluation);
+case 2
+ mprintf('Maximum amount of CPU Time exceeded. Output may not be optimal.');
+ input('press enter to view results');
+ mprintf('\nThe optimal design parameters are d = %f ,D = %f and N = %f m',xopt(1),xopt(2),xopt(3));
+ mprintf('\nThe optimal objective is %f ',fval);
+ mprintf('\nIterations completed %d',output.Iterations);
+ mprintf('\nTotal CPU time %f',output.Cpu_Time);
+ mprintf('\nTotal Functional Evaluations %d',output.Objective_Evaluation);
+case 3
+ mprintf('Stop at Tiny Step');
+ input('press enter to view results');
+ mprintf('\nThe optimal design parameters are d = %f ,D = %f and N = %f m',xopt(1),xopt(2),xopt(3));
+ mprintf('\nThe optimal objective is %f ',fval);
+ mprintf('\nIterations completed %d',output.Iterations);
+ mprintf('\nTotal CPU time %f',output.Cpu_Time);
+ mprintf('\nTotal Functional Evaluations %d',output.Objective_Evaluation);
+case 4
+ mprintf('Solved To Acceptable Level');
+ input('press enter to view results');
+ mprintf('\nThe optimal design parameters are d = %f ,D = %f and N = %f m',xopt(1),xopt(2),xopt(3));
+ mprintf('\nThe optimal objective is %f ',fval);
+ mprintf('\nIterations completed %d',output.Iterations);
+ mprintf('\nTotal CPU time %f',output.Cpu_Time);
+ mprintf('\nTotal Functional Evaluations %d',output.Objective_Evaluation);
+case 5
+ mprintf('Converged to a point of local infeasibility.');
+ input('press enter to view results');
+ mprintf('\nThe optimal design parameters are d = %f ,D = %f and N = %f m',xopt(1),xopt(2),xopt(3));
+ mprintf('\nThe optimal objective is %f ',fval);
+ mprintf('\nIterations completed %d',output.Iterations);
+ mprintf('\nTotal CPU time %f',output.Cpu_Time);
+ mprintf('\nTotal Functional Evaluations %d',output.Objective_Evaluation);
+end
+
+
+