Advanced Mathematics and Mechanics Applications Using

to such problems is known as linear programming [41] and applications ..... able linear equations, the general situation yields nonlinear equations, and a min4 ...... An elementary way to analyze the proximity of two surfaces is to describe each.
409KB taille 22 téléchargements 374 vues
Chapter 13 Nonlinear Optimization Applications

13.1 Basic Concepts Optimization problems occur for a diverse range of topics. Perhaps the simplest type of optimization problem involves a scalar function of several variables. For example, the cost of a product having several ingredients may need to be minimized. This problem can be represented by a function F (x) which depends on the vector x = [x1 ; x2 ; . . . ; xn ] in n-dimensional space. Function F is called the objective function and cases where the independent variables x ı can vary arbitrarily are considered unconstrained. Most problems have constraints requiring x ı to remain within given bounds or satisfy other functional equations. Different analysis procedures exist for solving problems depending on whether they are linear or nonlinear, constrained or unconstrained. General solutions are available to handle linear objective functions with linear equality and inequality constraints. The discipline devoted to such problems is known as linear programming [41] and applications involving thousands of independent variables can be analyzed. 1 Although this class of linear problems is important, it does not offer the versatility of methods used to address nonlinear problems (which are more compute intensive for problems of similar dimensionality). 2 The material in this chapter addresses nonlinear problems with a few independent variables which are either constrained or restricted to lie within bounds of the form aı ≤ xı ≤ bı . This type of constraint can be satisÞed by taking xı = aı + (bı − aı ) sin2 (zı ) and letting zı vary arbitrarily. The MATLAB intrinsic functions fminbnd and fminsearch are employed for solving this class of problems. The following Þve examples are presented to illustrate the nature of nonlinear optimization methods: 1. Computing the inclination angle necessary to cause a projectile to strike a stationary distant object; 1 High

dimensionality linear problems should always be solved using the appropriate specialized software. MathWorks markets an “Optimization Toolbox” intended to satisfy a number of specialized optimization needs.

2 The

© 2003 by CRC Press LLC

2. Finding parameters of a nonlinear equation to closely Þt a set of data values; 3. Determining components of end force on a statically loaded cable necessary to make the endpoint assume a desired position; 4. Computing the shape of a curve between two points such that a smooth particle slides from one end to the other in the minimum time; 5. Determining the closest points on two surfaces. Before addressing speciÞc problems, some of the general concepts of optimization will be discussed. The minimum of an unconstrained differentiable function F (x1 , x2 , . . . , xn ) will occur at a point where the function has a zero gradient. Thus the condition ∂F =0, 1≤ı≤n ∂xı leads to n nonlinear simultaneous equations. Such systems often have multiple solutions, and a zero gradient indicates either a maximum, or a minimum, or a saddle point. No reliable general methods currently exist to obtain all solutions to a general system of nonlinear equations. However, practical situations do occur where one unique point providing a relative minimum is expected. In such cases F (x) is called unimodal and we seek x 0 which makes F (x0 ) < F (x0 + ∆) for |∆| > 0. Most unconstrained nonlinear programming software starts from an initial point and searches iteratively for a point where the gradient vanishes. Multimodal, or nonunimodal, functions can sometimes be solved by initiating searches from multiple starting points and using the best result obtained among all the searches. Since situations such as false convergence are fairly common with nonlinear optimization methods, results obtained warrant greater scrutiny than might be necessary for linear problems. The intrinsic MATLAB functions fminbnd and fminsearch are adequate to address many optimization problems. Readers should study the documentation available for fminbnd, which performs a one-dimensional search within speciÞed limits, and fminsearch, which performs an unconstrained multi-dimensional search starting from a user selected point. Both functions require objective functions of acceptable syntactical form. Various options controlling convergence tolerances and function evaluation counts should be studied to insure that the parameter choices are appropriately deÞned.

© 2003 by CRC Press LLC

Projectile Trajectory for Velocity Squared Drag 500

400

300

y axis

200

100

0

−100

−200 100

200

300

400

500 x axis

600

700

800

900

1000

Figure 13.1: Projectile Trajectory for v 2 Drag Condition

13.2 Initial Angle for a Projectile In Chapter 8, equations of motion for motion of a projectile with atmospheric drag were formulated and a function traject producing a solution y(x) passing through (x, y) = (0, 0) with arbitrary inclination was developed. The solution is generated for 0 ≤ x ≤ xf assuming the initial velocity is large enough for the projectile to reach xf . Therefore, program execution terminates if dx/dt goes to zero. In order to hit a target at position (x f , yf ), the starting angle of the trajectory must be selected iteratively because the equations of motion cannot be solved exactly (except for the undamped case). With the aid of an optimization method we calculate |y(x f ) − yf )| and minimize this quantity (described in function missdis which has the Þring angle as its argument). Function fminbnd seeks the angle to minimize the “miss” distance. Program runtraj illustrates the solution to the problem described and Figure 13.1 shows the trajectory required for the projectile to strike the object. Depending on the starting conditions, zero, one, or two solutions exist to cause the “miss” distance to approach zero. Function fminbnd terminates at either a local minimum or at one of the search limits. The reader will need to examine how the initial data correlate to the Þnal answers. For example, if the projectile misses the target by a signiÞcant amount, the initial projectile velocity was not large enough to reach the target.

© 2003 by CRC Press LLC

Program Output and Code Trajectory Analysis Program

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:

function runtraj % Example: runtraj % ~~~~~~~~~~~~~~~~ % % This program integrates the differential % equations governing two-dimensional motion % of a projectile subjected to gravity loading % and atmospheric drag proportional to the % velocity squared. The initial inclination % angle needed to hit a distant target is % computed repeatedly and function fmin is % employed to minimize the square of the % distance by which the target is missed. The % optimal value of the miss distance is zero % and the optimum angle will typically be found % unless the initial velocity is too small % and the horizontal velocity becomes zero % before the target is passed. The initial % velocity of the projectile must be large % enough to make the problem well posed. % Otherwise, the program will terminate with % an error message. % % User m functions called: missdis, traject, % projcteq

26: 27: 28:

clear all; global Vinit Gravty Cdrag Xfinl Yfinl

29: 30: 31:

vinit=600; gravty=32.2; cdrag=0.002; xfinl=1000; yfinl=100;

32: 33: 34: 35: 36: 37: 38: 39: 40:

disp(’ ’); disp(’SEARCH FOR INITIAL INCLINATION ANGLE ’); disp(’TO MAKE A PROJECTILE STRIKE A DISTANT’); disp(’OBJECT’); disp(’ ’); disp([’Initial velocity = ’,num2str(vinit)]); disp([’Gravity constant = ’,num2str(gravty)]); disp([’Drag coefficient = ’,num2str(cdrag)]); disp([’Coordinates of target = (’, ...

© 2003 by CRC Press LLC

41: 42:

num2str(xfinl),’,’,... num2str(yfinl),’)’]); disp(’ ’);

43: 44: 45: 46:

% Replicate input data as global variables Vinit=vinit; Gravty=gravty; Cdrag=cdrag; Xfinl=xfinl; Yfinl=yfinl;

47: 48: 49: 50: 51: 52:

% Perform the minimization search fstart=180/pi*atan(yfinl/xfinl); fend=75; disp(’Please wait for completion of the’) disp(’minimization search’); bestang=fminbnd(@missdis,fstart,fend);

53: 54: 55: 56: 57: 58: 59: 60: 61: 62:

% Display final results [y,x,t]=traject ... (bestang,vinit,gravty,cdrag,xfinl); dmiss=abs(yfinl-y(length(y))); disp(’ ’) disp([’Final miss distance is ’, ... num2str(dmiss),’ when the’]); disp([’initial inclination angle is ’, ... num2str(bestang),... ’ degrees’]);

63: 64:

%=============================================

65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85:

function [dsq,x,y]=missdis(angle) % % [dsq,x,y]=missdis(angle) % ~~~~~~~~~~~~~~~~~~~~~~~~ % % This function is used by fminbnd. It returns % an error measure indicating how much the % target is missed for a particular initial % inclination angle of the projectile. % % angle - the initial inclination angle of % the projectile in degrees % % dsq - the square of the difference between % Yfinal and the final value of y found % using function traject. % x,y - points on the trajectory. % % Several global parameters (Vinit, Gravty, % Cdrag, Xfinl) are passed to missdis by the

© 2003 by CRC Press LLC

86: 87: 88: 89:

% driver program runtraj. % % User m functions called: traject %----------------------------------------------

90: 91: 92: 93: 94:

global Vinit Gravty Cdrag Xfinl Yfinl [y,x,t]=traject ... (angle,Vinit,Gravty,Cdrag,Xfinl,1); dsq=(y(length(y))-Yfinl)^2;

95: 96:

%=============================================

97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130:

function [y,x,t]=traject ... (angle,vinit,gravty,cdrag,xfinl,noplot) % % [y,x,t]=traject ... % (angle,vinit,gravty,cdrag,xfinl,noplot) % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ % % This function integrates the dynamical % equations for a projectile subjected to % gravity loading and atmospheric drag % proportional to the square of the velocity. % % angle - initial inclination of the % projectile in degrees % vinit - initial velocity of the projectile % (muzzle velocity) % gravty - the gravitational constant % cdrag - drag coefficient specifying the % drag force per unit mass which % equals cdrag*velocity^2. % xfinl - the projectile is fired toward the % right from x=0. xfinl is the % largest x value for which the % solution is computed. The initial % velocity must be large enough that % atmospheric damping does not reduce % the horizontal velocity to zero % before xfinl is reached. Otherwise % an error termination will occur. % noplot - plotting of the trajectory is % omitted when this parameter is % given an input value %

© 2003 by CRC Press LLC

131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144:

% y,x,t - the y, x and time vectors produced % by integrating the equations of % motion % % Global variables: % % grav, - two constants replicating gravty and % dragc cdrag, for use in function projcteq % vtol - equal to vinit/1e6, used in projcteq % to check whether the horizontal % velocity has been reduced to zero % % User m functions called: projcteq %----------------------------------------------

145: 146:

global grav dragc vtol

147: 148: 149: 150: 151: 152:

% Default data case generated when input is null if nargin ==0 angle=45; vinit=600; gravty=32.2; cdrag=0.002; xfinl=1000; end;

153: 154: 155: 156: 157: 158:

% Assign global variables and evaluate % initial velocity grav=gravty; dragc=cdrag; ang=pi/180*angle; vtol=vinit/1e6; z0=[vinit*cos(ang); vinit*sin(ang); 0; 0];

159: 160: 161: 162: 163:

% Integrate the equations of motion defined % in function projcteq deoptn=odeset(’RelTol’,1e-6); [x,z]=ode45(’projcteq’,[0,xfinl],z0,deoptn);

164: 165: 166:

y=z(:,3); t=z(:,4); n=length(x); xf=x(n); yf=y(n);

167: 168: 169: 170: 171: 172: 173: 174: 175:

% Plot the trajectory curve if nargin < 6 plot(x,y,’k-’,xf,yf,’ko’); xlabel(’x axis’); ylabel(’y axis’); title([’Projectile Trajectory for ’, ... ’Velocity Squared Drag’]); axis(’equal’); grid on; figure(gcf); %print -deps trajplot

© 2003 by CRC Press LLC

176:

end

177: 178:

%=============================================

179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205:

function zp=projcteq(x,z) % % zp=projcteq(x,z) % ~~~~~~~~~~~~~~~~ % % This function defines the equation of motion % for a projectile loaded by gravity and % atmospheric drag proportional to the square % of the velocity. % % x - the horizontal spatial variable % z - a vector containing [vx; vy; y; t]; % % zp - the derivative dz/dx which equals % [vx’(x); vy’(x); y’(x); t’(x)]; % % Global variables: % % grav - the gravity constant % dragc - the drag coefficient divided by % gravity % vtol - a global variable used to check % whether vx is zero % % User m functions called: none %----------------------------------------------

206: 207: 208:

global grav dragc vtol vx=z(1); vy=z(2); v=sqrt(vx^2+vy^2);

209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220:

% Check to see whether drag reduced the % horizontal velocity to zero before the % xfinl was reached. if abs(vx) < vtol disp(’ ’); disp(’*************************************’); disp(’ERROR in function projcteq. The ’); disp(’ initial velocity of the projectile’); disp(’ was not large enough for xfinal to’); disp(’ be reached.’); disp(’EXECUTION IS TERMINATED.’);

© 2003 by CRC Press LLC

221: 222: 223: 224: 225:

disp(’*************************************’); disp(’ ’),error(’ ’); end zp=[-dragc*v; -(grav+dragc*v*vy)/vx; ... vy/vx; 1/vx];

13.3 Fitting Nonlinear Equations to Data Often an equation of known form is needed to approximately Þt some given data values. An equation y(t) to Þt m data values (t ı , yı ) might be sought from an equation expressible as y = f (a1 , a2 , . . . , an , t) where n parameters a 1 , a2 , . . . , an are needed to minimize the least squares error (a1 , a2 , . . . , an ) =

n 

2

[y − f (a1 , a2 , . . . , an , t )] .

=1

The smallest possible error would be zero when the equation passes exactly through all the data values. Function  can be minimized with an optimizer such as fminsearch, or conditions seeking a zero gradient of  which require   n  ∂f ∂ =2 [f (a1 , a2 , . . . , an , t ) − y ] ∂aı ∂aı =1 can be written. Note that the problem of minimizing a function and the problem of solving a set of nonlinear simultaneous equations are closely related. Solving large systems of nonlinear equations is difÞcult. Therefore, data Þtting by use of function minimization procedures is typically more effective. The formulation assuming y depends on a single independent variable could just as easily have involved several independent variables x 1 , x2 , . . . , xN , which would yield an equation of the form y = f (a1 , a2 , . . . , an , x1 , x2 , . . . , xN ). For instance, we might choose the simplest useful equation depending linearly on the independent variables N  y= xk ak k=0

where x0 = 1. The least squares error can be expressed as (a0 , a1 , . . . , an ) =

n  =1

© 2003 by CRC Press LLC

 y −

N  k=0

2 Xk ak

where Xk means the value of the k th independent variable at the j th data point. The condition that  have a zero gradient gives  n  N n    Xı Xk ak = Xı y , 1 ≤ ı ≤ N. k=0

=1

=1

This linear system can be solved using traditional methods. Since the multiple indices in the equation are slightly cryptic, expressing the relationship in matrix notation is helpful. We get Y ≈ XA where

  y1   y2     Y =  .  , X = [1, X1 , X2 , . . . , XN ] , A =  .   .  

yn

a0 a1 .. .

    

aN

with Xı being the column matrix [x ı1 , xı2 , . . . , xın ] and the Þrst column of X contains all ones. The requirement to minimize  is simply (X T X)A = X T Y and MATLAB produces the desired solution using A=X\Y; Although taking y as a linear function of parameters a 0 , a1 , . . . , aN produces solvable linear equations, the general situation yields nonlinear equations, and a minimization search procedure has greater appeal. We conclude this section with an example employing a minimization search. Consider an experiment where data values (t ı , yı ) are expected to conform to the transient response of a linear harmonic oscillator governed by the differential equation m0 y¨ + c0 y˙ + k0 y = 0. This equation has a solution representable as y = a1 e−|a2 |t cos(|a3 |t + a4 ) where |a2 | makes the response decay exponentially and |a 3 | assures that the damped natural frequency is positive. Minimizing the error function n  2  y − a1 e−1|a2 |t cos(|a3 |t + a4 ) (a1 , a2 , a3 , a4 ) = =1

requires a four-dimensional search.

© 2003 by CRC Press LLC

Data Approximating y = 1.5*exp(−.1*t)*cos(2.5*t+pi/4) 1.5

1

y axis

0.5

0

−0.5

−1 Approx. equation is y = a*exp(b*t)*cos(c*t+d) a = 1.4961 b = −0.098761 c = 2.4993 d = 0.62104 −1.5

0

2

4

6

8

10 time

12

14

16

18

20

Figure 13.2: Data Approximating y = 1.5 exp(−0.1t) cos(2.5t + π/4)

The program vibÞt tests data deviating slightly from an equation employing speciÞc values of a1 , a2 , a3 , a4 . Then function fminsearch is used to verify whether the coefÞcients can be recovered from the data points. Figure 13.2 shows the data values and the equation resulting from the nonlinear least square Þt. The results produced are quite acceptable.

Program Output and Code Program vibÞt

1: 2: 3: 4: 5: 6: 7: 8: 9: 10:

function vibfit % % Example: vibfit % ~~~~~~~~~~~~~~~ % % This program illustrates use of the Nelder % and Mead multi-dimensional function % minimization method to determine an equation % for y(t) which depends nonlinearly on several % parameters chosen to closely fit known data

© 2003 by CRC Press LLC

11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22:

% values. The program minimizes the sum of the % squares of error deviations between the data % values and results produced by the chosen % equation. The example pertains to the time % response curve characterizing free vibrations % of a damped linear harmonic oscillator. % % User m functions called: vibfun % % Make the data vectors global to allow % access from function vibfun global timdat ydat

23: 24: 25: 26: 27: 28: 29: 30: 31: 32:

echo off; disp(’ ’); disp(’ CHOOSING PARAMETERS’); disp(’ IN THE THE NONLINEAR EQUATION’); disp(’ Y = A*EXP(B*T)*COS(C*T+D)’); disp(’TO OBTAIN THE BEST FIT TO GIVEN DATA’); fprintf(’\nPress [Enter] to list function\n’); fprintf(’vibfun which is to be minimized\n’); pause;

33: 34: 35: 36: 37: 38:

% Generate a set of data to be fitted by a % chosen equation. a=1.5; b=-.1; c=2.5; d=pi/5; timdat=0:.2:20; ydat=a*exp(b*timdat).*cos(c*timdat+d);

39: 40: 41:

% Add some random noise to the data ydat=ydat+.1*(-.5+rand(size(ydat)));

42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53:

% Function vibfun defines the quantity to be % minimized by a search using function fmins. disp(’ ’); disp(’The function to be minimized is:’); type vibfun.m; disp(’ ’); disp(’The input data will be plotted next.’); disp(’Press [Enter] to continue’); pause; plot(timdat,ydat,’k.’); title(’Input Data’); xlabel(’time’); ylabel(’y axis’); grid off; figure(gcf); input(’’,’s’);

54: 55:

% Initiate the four-dimensional search

© 2003 by CRC Press LLC

56:

x=fminsearch(@vibfun,[1 1 1 1]);

57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78:

% Check how well the computed parameters % fit the data. aa=x(1); bb=-abs(x(2)); cc=abs(x(3)); dd=x(4); as=num2str(aa); bs=num2str(bb); cs=num2str(cc); ds=num2str(dd); ttrp=0:.05:20; ytrp=aa*exp(bb*ttrp).*cos(cc*ttrp+dd); disp(’ ’); disp(’Press [Enter] to see how well’); disp(’the equation fits the data’); pause; plot(ttrp,ytrp,’k-’,timdat,ydat,’k.’); str1=[’Approx. equation is y = ’, ... ’a*exp(b*t)*cos(c*t+d)’]; str2=[’a = ’,as,’ b = ’,bs,’ c = ’, ... cs,’ d = ’,ds]; text(6,-1.1,str1); text(6,-1.25,str2); xlabel(’time’); ylabel(’y axis’); title([’Data Approximating ’, ... ’y = 1.5*exp(-.1*t)*cos(2.5*t+pi/4)’]); grid off; figure(gcf); print -deps apprxdat

79: 80:

%=============================================

81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100:

function z=vibfun(x) % % z=vibfun(x) % ~~~~~~~~~~~ % % This function evalautes the least square % error for a set of vibration data. The data % vectors timdat and ydat are passed as global % variables. The function to be fitted is: % % y=a*exp(b*t)*cos(c*t+d) % % x - a vector defining a,b,c and d % % z - the square of the norm for the vector % of error deviations between the data and % results the equation gives for current % parameter values %

© 2003 by CRC Press LLC

101: 102:

% User m functions called: none %----------------------------------------------

103: 104: 105: 106: 107:

global timdat ydat a=x(1); b=-abs(x(2)); c=abs(x(3)); d=x(4); z=a*exp(b*timdat).*cos(c*timdat+d); z=norm(z-ydat)^2;

13.4 Nonlinear Deßections of a Cable We will now present an optimization procedure to determine the static equilibrium position of a perfectly ßexible inextensible cable having given end positions and a known distributed load per unit length. If R(s) is the position of any point on the cable as a function of arc length 0 ≤ s ≤ L, then the internal tension at position s is  L q(s) ds T (s) = F e + s

with q(s) being the applied force per unit length and F e being the support force at s = L. The end force to produce a desired end deßection has to be determined in the analysis. However, the end deßection resulting from any particular choice of end force can be computed by observing that the tangent to the deßection curve will point along the direction of the cable tension. This means T (s) dR = ds |T (s)| and

 R(s) = 0

s

T (s)ds = |T (s)|

 0

s

Fe +

L

|F e +

s

q ds ds

L s

q ds|

where R(0) = 0 is taken as the position at the starting end. The deßection at s = L will have some speciÞed position R e so that requiring R(L) = R e gives a vector equation depending parametrically on F e . Thus, we need to solve three nonlinear simultaneous equations in the Cartesian components of force F e . A reasonable analytical approach is to employ an optimization search to minimize |R(L) − R e | in terms of the components of F e . The procedure described for a cable with continuous loading extends easily to a cable having several rigid links connected at frictionless joints where arbitrary concentrated forces are applied. The function cabldeß evaluates the position of each joint when the joint forces and outer end force are given. With the end force on the last link treated as a parameter, function endß computes an error measure |F (L) − RE |2 to be minimized using function fminsearch. The optimization search seeks the components of F e needed to reduce the error measure to zero. Specifying a sensible problem obviously requires that |R e | must not exceed the total length of all members

© 2003 by CRC Press LLC

Deflection Shape for a Loaded Cable

z axis

0 −1 9 −2

8 7

0

6 5

−1

4

−2

3

−3

2 −4

yaxis

1 0

x axis

Figure 13.3: Deßected Shape for a Loaded Cable

in the chain. Initiating the search with a randomly generated starting force leads to a Þnal force produced by fminsearch, which is then employed in another call to cabldeß to determine and plot the Þnal deßection position as shown in Figure 13.3. Using a random initial guess for the end force was done to show that choosing bad starting data, insufÞciently stringent convergence tolerances, or too few allowable function iterations can sometimes produce erroneous results. This emphasizes the need to always examine the results from nonlinear search procedures to assure that satisfactory answers are obtained.

Program Output and Code Program cablsolv

1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11:

function [r,t,pends]=cablsolv(Len,P,Rend) % % [r,t,pends]=cablsolv(Len,P,Rend) % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ % % This function computes the equilibrium % position for a cable composed of rigid % weightless links with loads applied at the % frictionless joints. The ends of the cable % are assumed to have a known position. %

© 2003 by CRC Press LLC

12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33:

% % % % % % % % % % % % % % % % % % % % % %

Len P

Rend

- a vector containing the lengths Len(1), ..., Len(n) - matrix of force components applied at the interior joints. P(:,i) contains the Cartesian components of the force at joint i. - the coordinate vector giving the position of the outer end of the last link, assuming the outer end of the first link is at [0,0,0].

r

- a matrix with rows giving the computed equilibrium positions of all ends t - a vector of tension values in the links pends - a matrix having two rows which contain the force components acting at both ends of the chain to maintain equilibrium User m functions called: endfl, cabldefl

34: 35: 36: 37: 38: 39: 40:

if nargin < 3 % Example for a ten link cable with vertical % and lateral loads Len=1.5*ones(10,1); Rend=[10,0,0]; P=ones(9,1)*[0,-2,-1]; end

41: 42: 43:

global len p rend len=Len; rend=Rend; p=P; tol=sum(Len)/1e8;

44: 45: 46:

% Start the search with a random force applied % at the far end

47: 48: 49: 50: 51: 52: 53: 54:

% % % % % % %

Perform several searches to minimize the length of the vector from the outer end of the last link to the desired position Rend where the link is to be connected to a support. The final end force should reduce the deflection error to zero if the search is successful.

55: 56:

opts=optimset(’tolx’,tol,’tolfun’,tol,...

© 2003 by CRC Press LLC

57: 58:

’maxfunevals’,2000); endval=realmax;

59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69:

% Iterate several times to avoid false % convergence for k=1:5 p0=10*max(abs(p(:)))*rand(size(p,2),1); [pendk,endvalk,exitf]=... fminsearch(@endfl,p0,opts); if endvalk < endval pend=pendk(:); endval=endvalk; end end

70: 71: 72: 73: 74: 75: 76:

% Use the computed end force to obtain the % final deflection. Also return the % support forces. [r,t,pstart]=cabldefl(len,[p;pend’]); x=r(:,1); y=r(:,2); z=r(:,3); pends=[pstart(:)’;pend(:)’];

77: 78: 79: 80: 81: 82: 83:

% Plot the deflection curve of the cable plot3(x,y,z,’k-’,x,y,z,’ko’); xlabel(’x axis’); ylabel(’yaxis’); zlabel(’z axis’); title(’Deflection Shape for a Loaded Cable’); axis(’equal’); grid on; figure(gcf); print -deps defcable

84: 85:

%=============================================

86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101:

function enderr=endfl(pend) % % enderr=endfl(pend) % ~~~~~~~~~~~~~~~~~~ % % This function computes how much the % position of the outer end of the last link % deviates from the desired position when an % arbitrary force pend acts at the cable end. % % pend - vector of force components applied % at the outer end of the last link % % enderr - the deflection error defined by the % square of the norm of the vector

© 2003 by CRC Press LLC

102: 103: 104: 105: 106: 107: 108:

% from the computed end position and % the desired end position. This error % should be zero for the final % equilibrium position % % User m functions called: cabldefl %----------------------------------------------

109: 110: 111: 112: 113:

% Pass the lengths, the interior forces and the % desired position of the outer end of the last % link as global variables. global len p rend

114: 115: 116: 117: 118: 119:

% use function cabldefl to compute the % desired error r=cabldefl(len,[p;pend(:)’]); rlast=r(size(r,1),:); d=rlast(:)-rend(:); enderr=d’*d;

120: 121:

%=============================================

122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146:

function [r,t,pbegin]=cabldefl(len,p) % % [r,t,pbegin]=cabldefl(len,p) % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ % % This function computes the static equilibrium % position for a cable of rigid weightless % links having concentrated loads applied at % the joints and the outside of the last link. % The outside of the first link is positioned % at the origin. % % len - a vector of link lengths % len(1), ..., len(n) % p - a matrix with rows giving the % force components acting at the % interior joints and at the outer % end of the last link % % r - matrix having rows which give the % final positions of each node % t - vector of member tensions % pbegin - force acting at the outer end of % the first link to achieve

© 2003 by CRC Press LLC

147: 148: 149: 150:

% equilibrium % % User m functions called: none %----------------------------------------------

151: 152:

n=length(len); len=len(:); nd=size(p,2);

153: 154: 155: 156:

% Compute the forces in the links T=flipud(cumsum(flipud(p))); t=sqrt(sum((T.^2)’)’);

157: 158: 159: 160: 161:

% Obtain the deflections of the outer ends % and the interior joints r=cumsum(T./t(:,ones(1,nd)).*len(:,ones(1,nd))); r=[zeros(1,nd);r]; pbegin=-t(1)*r(2,:)/len(1);

13.5 Quickest Time Descent Curve (the Brachistochrone) The subject of variational calculus addresses methods to Þnd a function producing the minimum value for an integral depending parametrically on the function. Typically, we have a relationship of the form 

x2

I(y) =

G(x, y, y  (x)) dx

x1

where values of y at x = x 1 and x = x2 are known, and y(x) for x 1 < x < x2 is sought to minimize I. A classical example in this subject is determining a curve starting at (0, 0) and ending at (a, b) so that a smooth particle will slide from one end to the other in the shortest possible time. Let X and Y be measured positive to the right and downward. Then the descent time for frictionless movement along the curve will be 1 t= √ 2g



a



0

1 + Y  (X)2 dX , Y (0) = 0 , Y (a) = b. Y (X)

This problem is solved in various advanced calculus books. 3 The curve is a cycloid expressed in parametric form as X = k[θ − sin(θ)] , Y = k[1 − cos(θ)]

3 Weinstock

[105] provides an excellent discussion of the brachistochrone problem using calculus of variation methods.

© 2003 by CRC Press LLC

where 0 < θ < θf . Values of θf and k are found to make x(θ f ) = a and Y (θf ) = b. The exact descent time is  k tbest = θf g which is signiÞcantly smaller than the descent time for a straight line, which is  2(a2 + b2 ) . tline = gb Two functions, brfaltim and bracifun, are used to compute points on the brachistochrone curve and evaluate the descent time. The main purpose of this section is to illustrate how optimization search can be used to minimize an integral depending parametrically on a function. The method used chooses a set of base points through which an interpolation curve is constructed to specify the function. Using numerical integration gives a value for the integral. Holding the x values for the interpolation points constant and allowing the y values to vary expresses the integral as a function of the y values at a Þnite number of points. Then a multi-dimensional search function such as fminsearch can optimize the choice of Y values. Before carrying out this process for the brachistochrone problem it is convenient to change variables so that x = X/a and Y (X) = b[x + y(x)] , 0 ≤ x ≤ 1, with y(0) = y(1) = 0. Then the descent integral becomes  1 1 + (b/a)2 [1 + y  (x)]2 a dx. t= √ x+y 2gb 0 For any selected set of interpolation points, functions spline and splined can evaluate y(x) and y  (x) needed in the integrand, and function gcquad can be used to perform Gaussian integration. An optimization search employing fminsearch produces the curve heights yielding an approximation to the brachistochrone as shown in Figure 13.4.

© 2003 by CRC Press LLC

Brachistochrone Curve for a/b = 3 0 Descent time

(secs)

Approximate: 1.76

−0.2

Exact:

1.7774

Error tolerance:

0.0001

−0.4

y/b

−0.6

−0.8

−1

−1.2 Approximate Curve Computed Points Exact Curve −1.4

0

0.1

0.2

0.3

0.4

0.5 x/a

0.6

0.7

Figure 13.4: Brachistochrone Curve for

© 2003 by CRC Press LLC

0.8

a b

=3

0.9

1

Program Output and Code Program brachist

1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:

function brachist % Example: brachist % ~~~~~~~~~~~~~~~~~ % This program determines the shape of a % smooth curve down which a particle can slide % in minimum possible time. The analysis % employs a piecewise cubic spline to define % the curve by interpolation using a fixed set % of base point positions. The curve shape % becomes a function of the heights chosen at % the base points. These heights are determined % by computing the descent time as a function % of the heights and minimizing the descent % time by use of an optimization program. The % Nelder and Mead unconstrained search % procedure is used to compute the minimum. % % User m functions called: % chbpts, brfaltim, fltim, gcquad, % bracifun, splined

21: 22: 23:

global cbp cwf cofs n xc yc a b b_over_a ... grav nparts nquad nfcls

24: 25: 26: 27: 28: 29:

fprintf(... ’\nBRACHISTOCHRONE DETERMINATION BY NONLINEAR’); fprintf(’\n OPTIMIZATION SEARCH \n’); fprintf([’\nPlease wait. The ’,... ’calculation takes a while.\n’]);

30: 31: 32: 33:

% Initialize a=30; b=10; grav=32.2; nparts=1; nquad=50; tol=1e-4; n=6; b_over_a = b/a;

34: 35: 36: 37:

[dummy,cbp,cwf]=gcquad(’’,0,1,nquad,nparts); xc=chbpts(0,1,n); xc=xc(:); y0=5*sin(pi*xc); xc=[0;xc;1];

38: 39: 40:

% Calculate results from the exact solution [texact,xexact,yexact]=brfaltim(a,b,grav,100);

© 2003 by CRC Press LLC

41: 42: 43: 44: 45: 46:

% Perform minimization search for % approximate solution opts=optimset(’tolx’,tol,’tolfun’,tol); [yfmin,fmin,flag,outp] =... fminsearch(@fltim,y0,opts);

47: 48: 49: 50: 51: 52: 53:

% Evaluate final position and approximate % descent time Xfmin=xc; Yfmin=Xfmin+[0;yfmin(:);0]; % tfmin=a/sqrt(2*grav*b)*fltim(yfmin(:)); tfmin=a/sqrt(2*grav*b)*fmin; nfcls=1+outp.funcCount;

54: 55: 56: 57: 58: 59: 60: 61:

% Summary of calculations fprintf(’\nBrachistochrone Summary’); fprintf(’\n-----------------------’); fprintf(’\nNumber of function calls: fprintf(’%g’,nfcls); fprintf(’\nDescent time: ’); fprintf(’%g’,tfmin), fprintf(’\n’)

’);

62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80:

% Plot results comparing the approximate % and exact solutions xplot=linspace(0,1,100); yplot=spline(Xfmin,Yfmin,xplot); plot(xplot,-yplot,’-’,Xfmin,-Yfmin,’o’, ... xexact/a,-yexact/b,’--’); xlabel(’x/a’); ylabel(’y/b’); % grid title([’Brachistochrone Curve for ’, ... ’a/b = ’,num2str(a/b)]); text(.5,-.1, ’Descent time (secs)’) text(.5,-.175,[’Approximate: ’,num2str(tfmin)]) text(.5,-.25, [’Exact: ’,num2str(texact)]); text(.5,-.325, ... sprintf(’Error tolerance: %g’,tol)); legend(’Approximate Curve’, ... ’Computed Points’,’Exact Curve’,3); figure(gcf); print -deps brachist

81: 82:

%=============================================

83: 84: 85:

function [tfall,xbrac,ybrac]=brfaltim ... (a,b,grav,npts)

© 2003 by CRC Press LLC

86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127:

% % % [tfall,xbrac,ybrac]=brfaltim(a,b,grav,npts) % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ % % This function determines the descent time % and a set of points on the brachistochrone % curve passing through (0,0) and (a,b). % The curve is a cycloid expressible in % parametric form as % % x=k*(th-sin(th)), % y=k*(1-cos(th)) for 0