Main Content

Generate MATLAB Code for Design Optimization Problems (GUI)

This example shows how to automatically generate a MATLAB® function to solve a Design Optimization problem. You use theResponse Optimizerto define an optimization problem for a hydraulic cylinder design and generate MATLAB code to solve this optimization problem.

Hydraulic Cylinder Design Problem

TheDesign Optimization to Meet a Custom Objective (GUI)example shows how to use theResponse Optimizerto optimize a cylinder design. In this example we load a pre-configuredResponse Optimizersession based on that example.

loadsdoHydraulicCylinder_sdosession

Use the following command to open theResponse Optimizer

sdotool(SDOSessionData)

Generate MATLAB Code

From theOptimizelist, selectGenerate MATLAB Code.

The generated code is added to the MATLAB editor as an unsaved MATLAB function.

Examine the generated code. Significant code portions are:

  • Specify Design Variables- Definition of the model parameters being optimized.

  • 指定设计Requirements- Definition of the design requirements.

  • Create Optimization Objective Function- Creation of an anonymous function that calls the subfunctionsdoHydraulicCylinder_optFcn, which evaluates the cylinder design.sdo.optimizecalls the anonymous function at each iteration.

  • Evaluate custom parameter requirement functions- Evaluates the custom requirement,MinimizeAC, that uses thesdoHydraulicCylinder_customObjectivefunction.

  • Optimize the Design——优化使用sdo.optimizecommand.

SelectSavefrom the MATLAB editor to save the generated function.

Run Generated Code

Run the generated function.

The first output argument,pOpt, contains the optimized parameter values and the second output argument,optInfo, contains optimization information.

Modify the Generated Code

You can:

  • Modify the generatedsdo_sdoHydraulicCylinderfunction to include or exclude new design requirements or change the optimization options.

  • Call the generatedsdo_sdoHydraulicCylinderfunction with a different set of parameters to optimize.

For details on how to write an objective/constraint function to use with thesdo.optimizecommand, typehelp sdoExampleCostFunctionat the MATLAB command prompt.

Close the model.