52 lines (43 with data), 1.9 kB
% Unconstrained optimization in the AnyBody Modeling System
% using a golden section method and steeoest descen search
% directions. Notice that this may not work if the problem
% is non-smooth.
% The
nOptimParam = 6; % Number of design variables
penal = 2; % exterior penalty factor
% Design variables as they appear in the AnyScript model
% AnyVar L0 = 0.24; //Spring slack length
% AnyVar F1 = 100; //Spring force at 50% strain
% AnyVar SpringRad = 0.15; //Radius of spring attachment on the crank
% AnyVar SpringAngle = 45; //Angle of spring attachment points on the crank
% AnyVar SpringX = -0.10; //X coordinate of spring attachment on frame
% AnyVar SpringY = 0.30
% Initialize
% L0 F1 SpringRad SpringAngle SpringX SpringY
xmin = [0.1 0.0 0.01 0.0 -0.3 0.1]; % Lower bound
x = [0.20 150.0 0.11 56.0 0.0 0.3]; % Initial guess
xmax = [0.4 1000.0 0.40 180.0 0.3 0.5]; % Upper bound
% Compute initial objective function value
F = ObjectivePenal(xmin,x,xmax,penal);
% do optimization
maxiter = 10;
evals = 0;
delta = 0.1; % Relative perturbation
while evals<maxiter
% Compute gradient
for j=1:length(x)
xp = x;
pert = (xmax(j)-xmin(j))*delta;
xp(j) = xp(j)+pert;
Fp = ObjectivePenal(xmin,xp,xmax,penal);
d(j) = -(Fp-F)/pert;
end
% d = randn(size(x)); % Generate a random search direction
[x F] = golden(xmin,x,xmax,penal,d,10); % Perform golden section optimization in that direction
evals = evals + 1;
end
clock-clock1
fprintf(fidsht,'Optimal parameters:\n');
for k=1:length(optpar)
fprintf(fidsht,'%20.14f %20.14f\n',optpar(k),dx(k));
end