[38ba34]: / Application / Examples / BikeSpring / Model / anyoptgrad.m

Download this file

45 lines (37 with data), 1.7 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
% 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 side constraints, xmin and xmax, are handled by means of
% an exterior penalty function.
nOptimParam = 6; % Number of design variables
% 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 SpringY
xmin = [0.1 0.0 0.01 0.0 0.1]; % Lower bound
x = [0.20 150.0 0.11 56.0 0.3]; % Initial guess
xmax = [0.4 1000.0 0.40 180.0 0.5]; % Upper bound
% Compute initial objective function value
F = ObjectivePenal(xmin,x,xmax);
% 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);
d(j) = -(Fp-F)/pert;
end
[x F] = goldengrad(xmin,x,xmax,d,10); % Perform golden section optimization in that direction
evals = evals + 1;
end