Code covered by the BSD License  

Highlights from
New Approaches to Constrained Optimization in MATLAB

image thumbnail
from New Approaches to Constrained Optimization in MATLAB by Rakesh Kumar
M-files accompanying the webinar held on November 05, 2005

helicalSpringOptimization.m
%%  Design of a helical spring 
% In this demo we will design a helical spring subject to some nonlinear
% constraints such that weight of the spring is minimized. The spring is
% used in an automotive valve which operates at a certain frequency. While
% spring design problems are small in the number of constraints (<20 or
% so), the presence of high powers of wire and coil diameter, e.g. terms
% involving d^4, etc., makes it difficult for gradient based methods. Two
% design parameters 'mean diameter' and wire diameter are considered in
% this demo.

%%
% Operating load
Fo = 35;
% Preload on the spring
Fc = 5;
% Spring compression at maximum load (used to calculate spring constant)
delta = 0.25;
% Elastic modulus of the material of the spring
G = 11.5e6;
% Frequency of the valve where the spring will be used (used to calculate
% the fatige load on the spring)
Nf = 1e6;
% Mass density of the spring material
rho = 0.282;

%% Optimization data
% Initial guess of the design variables
D = .8; d = 0.462;
x0 = [D d];
% Lower bound on design variables
lb = [0.1 0.01];
% Objective function
objective   = @(x) springObjective(x,Fo,Fc,delta,G,rho);
% Nonlinear constraint function
constraints = @(x) springConstraints(x,Fo,Fc,delta,G,Nf,rho);

%% Run pattern search solver
% Set pattern search option to display iteration history
psopt = psoptimset('Display','iter');
[x1,f1] = patternsearch(objective,x0,[],[],[],[],lb,[],constraints,psopt)

%%
% The pattern search solver found an optimum solution and all the
% constraints are satisfied. 

%% Run the genetic algorithm solver
% Set genetic algorith option to use appropriate mutation function for
% linear and bound constraints and also provide the start point. We use the
% new mutation function 'mutationadaptfeasible' and also provide the start
% point 'x0' to GA via the 'InitialPopulation' option.
rand('state',0); randn('state',0);
gaopt = gaoptimset('MutationFcn',@mutationadaptfeasible, ...
    'InitialPopulation',x0,'Display','iter');
    
[x2,f2] = ga(objective,2,[],[],[],[],lb,[],constraints,gaopt)

%%
% The genetic algorithm solver found an optimum solution and all the
% constraints are satisfied. However, the genetic algorithm took much more
% function evaluation than the pattern search solver.

Contact us at files@mathworks.com