Transcription

Solving a Two Bar Truss Problem Using APM MATLABThis tutorial is a step-by-step procedure for solving optimization problems with theAPMonitor Toolbox for MATLAB. The tutorial assumes no prior experience with eitherAPMonitor or MATLAB so many of the steps can be skipped by experienced users. Thetutorial covers the solution to a two bar truss optimization problem with additional ain/TwoBarTruss1. Create a new folder or directory for this problem. This problem is demonstrated withWindows OS. Any other platform that can run MATLAB such as Linux or Mac OS isalso acceptable. Create a new folder TwoBar on the desktop.2. Open MATLAB and take note of the different workspace sections including theCommand Window, Current Folder, Workspace, Command History, etc. This tutorialuses MATLAB R2012b but other versions have also been tested and should also work.Some old versions of MATLAB (pre-2005) have some known issues with web-browseractions such as the apm web function but this is only for web-browser display of resultsand is not critical for this application.

3. Browse to the TwoBar folder by either using the commandcd C:\Users\{Your Username}\Desktop\Twobarin the Command Window or by using the dialog box to select the directory

4. Create a MATLAB script file (extension “.m”). A new script file can be created with the“New Script” button or by issuing the command at the Command Window:oredit MyNewScript.mFor this example the script is named MyNewScript.m but any script name is suitable.5. Include the following commands in your script file MyNewScript.m:% clear everythingclear all; close all; clc% load the APM MATLAB libraryaddpath('apm')% select the serverserver 'http://byu.apmonitor.com';% give the application a nameapp 'twobar';% clear any previous applications by that nameapm(server,app,'clear all');% load the model fileapm load(server,app,'twobar.apm');% solve the optimization problemapm(server,app,'solve');% retrieve the solutiony apm sol(server,app);z y.x;% display the solutiondisp(['Height:' num2str(z.height)])disp(['Diameter: ' num2str(z.diameter)])disp(['Weight:' num2str(z.weight)])The MATLAB script is a text file and can be edited with a text editor like Notepad(Windows) or Emacs (Linux) or within the MATLAB editor. Note that the % sign indicates acomment character and comments are indicated by green text in MATLAB. If MATLAB isthe default editor of m files, the MATLAB editor should open automatically with the scriptcontents displayed if the file is opened.

6. Create an APMonitor model file (File Extension .apm) named twobar.apm that will beused to configure the optimization problem. The model file is a collection of Constants,Parameters, Variables, and Equations that relate the Design Variables to the ObjectiveFunction. The APM model file can be modified by any text editor. Notepad (a free texteditor) is recommended instead of the Windows default (Notepad application) if youdon’t have another text editor of choice.

! Two bar truss engineering design problem! APMonitor Modeling Language! Solve model with the web-interface at:!http://apmonitor.com/online/view pass.phpModelConstants! declare fixed values that never changepi 3.14159End ConstantsParameters! declare model parameters! parameters can be changed by the user or with!input data but not by the optimizerwidth 60thickness 0.15density 0.3modulus 30000load 66End ParametersVariables! declare variables and initial guesses! variables can be changed by the optimizerheight 30.00, 10.0, 50.0diameter 3.000, 1.00, 4.00weightEnd VariablesIntermediates! intermediate variables are explicitly determined!with equality constraintsleng sqrt((width/2) 2 height 2)area pi * diameter * thicknessiovera (diameter 2 thickness 2) / 8stress load * leng / (2*area*height)buckling pi 2 * modulus * iovera / (leng 2)deflection load * leng 3 / (2 * modulus * area * height 2)End IntermediatesEquations! objective: minimize the weightminimize weight! equality constraintsweight 2 * density * area * leng! inequality constraintsweight 24stress 100stress bucklingdeflection 0.25End EquationsEnd Model

7. A final requirement is to obtain the APM toolbox libraries from the APMonitor.com website. These are a collection of .m file functions that allow a MATLAB user to use theAPM models to solve simulation and optimization problems. To obtain the APM librarybrowse Open the zipped archive and copy the apm folder into the TwoBar folder on your desktop.

8. The folder now contains all of the files necessary to solve the optimization problem.Once the apm folder is copied into the TwoBar folder, the following files should appear: apm – folder that allows users to work with the APM MATLAB toolboxMyNewScript.m – MATLAB driver script file for solving the Two Bar problemtwobar.apm – Two Bar problem in the APMonitor Modeling Language

9. Solve the optimization problem by running the MATLAB script MyNewScript.m. Thescript can be run by clicking the Run button, Pressing F5 with the MATLAB editor infocus, or by issuing the statement from the Command Window: MyNewScript(note, don’t include the or the .m file extension)10. The results will be accessible through a solution.csv file downloaded to your rundirectory or displayed in the Command Window as:Height:14.215Diameter: 1.6906Weight:15.86811. A web-interface can also be used to view the results by issuing the statement at theCommand Window. The semi-colon “;” at the end of the function call is used to suppressany output from the command and is optional. apm web pilengName-----3.1416E 003.3926E 01-----modulusload-----3.0000E 046.6000E --7.8520E-013.4986E-019.0000E 019.0000E 012.1795E-01-----------heightdiameterweightslk 3slk 41.0000E 011.0000E 00--0.0000E 000.0000E 00slk 5slk 60.0000E 00 0.0000E 000.0000E 00 0.0000E 00width--6.0000E 01--thickness 5.0000E-02 1.5000E-01 2.0000E-01density--3.0000E-01---1.4215E 01 5.0000E 011.6906E 00 4.0000E 001.5868E 01--8.1317E 00--3.2617E 00-------

12. Next is code for contour plots that can be added to the end of the MyNewScript.m file:% constantspi 3.14159;dens 0.3;modu 30000;load 66.0;% analysis variableswdth 60;thik 0.15;% design variables at mesh points[hght,diam] meshgrid(10:2:30,1:.3:3);% equationsleng ((wdth/2) 2 hght. 2). 0.5;area pi * diam .* thik;iovera (diam. 2 thik 2)/8;wght 2 * dens * leng .* area;strs load * leng ./ (2 * area .* hght);buck pi 2 * modu * iovera ./ (leng. 2);defl load * leng. 3 ./ (2*modu * area .* hght. tle('Two Bar Truss Contour Plot');xlabel('Height');ylabel('Diameter');hold on;% solid lines to show constraint .0,0.0],'b-','LineWidth',3);% show a legendlegend('Weight','Stress 100','Deflection 0.25','Stress-Buckling 0')figure(2)[C,h] abelspacing',250);title('Two Bar Truss Contour Plot');xlabel('Height');ylabel('Diameter');hold on;[C,h] l(C,h,'Labelspacing',250);[C,h] ;clabel(C,h,'Labelspacing',250);[C,h] .0],'b--');clabel(C,h,'Labelspacing',250);% show a legendlegend('Weight','Stress 100','Deflection 0.25','Stress-Buckling 0')% solid lines to show constraint am,wght);

13. The statement meshgrid produces a mesh and makes hght and diam arrays.% design variables at mesh points[hght,diam] meshgrid(10:2:30,1:.3:3);Thereafter, any function that depends on these two variables will also be an array. Note thatwe need to use array rather than matrix notation (“.*” instead of “*”, “./” instead of “/”, and“. ” instead of “ ” whenever these arrays are involved.) This notation tells MATLAB to docalculations element by element instead of regular matrix multiplication.In the statements which produce the contours, the code [12:3:34] indicates the levels we wishto have for the ');In this case, they will go from 12 pounds to 34 pounds in three pound increments.Alternatively we could just give one number (without brackets), which would indicate thenumber of contours, and MATLAB would decide on the levels.14. The resulting Figure 1 contour plot is given below. The plot file can be saved in a varietyof file formats including JPEG, PNG, EMF, EPS, etc. by using the Save As selectionunder the File toolbar menu or by using the saveas MATLAB command.Two Bar Truss Contour Plot2.8WeightStress 100Deflection 0.25Stress-Buckling 2224262830

15. Figure 2 is a more detailed contour plot that shows specific values and the region offeasible designs. Annotations and other mark-up items can be added within MATLAB orafterwards in an image editor.Two Bar Truss Contour Plot2.80.192.6212430.250.22270101.8-3 0-2210-1 0018-30-20-10010.2Diameter2.22485 90 592.41.608.5199095151.4180.251214161510 0121.211033WeightStress 10030Deflection 0.25Stress-Buckling 0271820Height0.232285900.19 51228300.21242616. The very last statement in the file, surfc results in Figure 3 for a 3D view of the surface.The 3D plot can be rotated after selecting the Rotate 3D button at the top. The figure canbe enhanced with the xlabel, ylabel, zlabel, and legend commands such 532.530252201.5151Ref: An Engineer’s Guide to Matlab, 2nd Edition, Pearson Prentice Hall.10

uses MATLAB R2012b but other versions have also been tested and should also work. Some old versions of MATLAB (pre-2005) have some known issues with web-browser actions such as the apm_web function but t