Spring Problem

Spring Problem

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:

Mathematical Formulation:

Minimize:

Subjected to:

The following ranges for the variables were used:

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 mprintf 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
if exitflag == 0 then
    mprintf('Optimal Solution Found');
    mprintf('\n\nThe optimal design parameters are
     d =  %f ,D = %f mprintf 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);
elseif exitflag == 1 then
    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 mprintf 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);
else
    disp("Error encountered")
end

Expected Output:

Optimal Solution Found

The optimal design parameters are d =  0.051692 ,D = 0.356786 mprintf N = 11.285142 m

The optimal objective is 0.012665 

Iterations completed 138
Total CPU time 1.015535
Total Functional Evaluations 183