Main Content

Integrating MATLAB Optimization Routines with Objective Functions in .NET


This example shows how to:

  • Use the MATLAB® Compiler SDK™ product to create an assembly (OptimizeComp). This assembly applies MATLAB optimization routines to objective functions implemented as .NET objects.

  • Access the component in a .NET application (OptimizeApp.cs). Then use the MWObjectArray class to create a reference to a .NET object (BananaFunction.cs), and pass that object to the component.


    For information about these data conversion classes, see the MATLAB MWArray Class Library Reference, available in the matlabroot\help\dotnetbuilder\MWArrayAPI folder, where matlabroot represents your MATLAB installation folder

  • Build and run the application.

OptimizeComp Component

The component (OptimizeComp) finds a local minimum of an objective function and returns the minimal location and value. The component uses the MATLAB optimization function fminsearch. This example optimizes the Rosenbrock banana function used in the fminsearch documentation.

The class OptimizeComp.OptimizeClass performs an unconstrained nonlinear optimization on an objective function implemented as a .NET object. A method of this class, doOptim, accepts an initial value (NET object) that implements the objective function, and returns the location and value of a local minimum.

The second method, displayObj, is a debugging tool that lists the characteristics of a .NET object. These two methods, doOptim and displayObj, encapsulate MATLAB functions. The MATLAB code for these two methods resides in doOptim.m and displayObj.m. You can find this code in matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\OptimizeExample\OptimizeComp.


  1. If you have not already done so, copy the files for this example as follows:

    1. Copy the following folder that ships with MATLAB to your work folder:

    2. At the MATLAB command prompt, cd to the new OptimizeExample subfolder in your work folder.

  2. If you have not already done so, set the environment variables that are required on a development machine.

  3. Write the MATLAB code that you want to access. This example uses doOptim.m and displayObj.m, which already reside in your work folder. The path is matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\OptimizeExample\OptimizeComp.

    For reference, the code of doOptim.m is displayed here:

    function [x,fval] = doOptim(h, x0)
    mWrapper = @(x) h.evaluateFunction(x);
    directEval = h.evaluateFunction(x0)
    wrapperEval = mWrapper(x0)
    [x,fval] = fminsearch(mWrapper,x0)
    For reference, the code of displayObj.m is displayed here:
    function className = displayObj(h)
    className = class(h)

  4. From the MATLAB apps gallery, open the Library Compiler app.

  5. As you compile the .NET application using the Library Compiler, use the following information:

    Project NameOptimizeComp
    Class NameOptimizeComp.OptimizeClass
    File to compiledoOptim.m

  6. Write source code for a class (BananaFunction) that implements an object function to optimize. The sample application for this example is in matlabroot\toolbox\dotnetbuilder\VSVersion\NET\Examples\OptimizeExample\OptimizeCSApp. The program listing for BananaFunction.cs displays the following code:

    using System;
    namespace MathWorks.Examples.Optimize
      public class BananaFunction 
    	    public BananaFunction() {}
    	    public double evaluateFunction(double[] x)
    		      double term1= 100*Math.Pow((x[1]-Math.Pow(x[0],2.0)),2.0); 
    		      double term2=  Math.Pow((1-x[0]),2.0);
    		      return term1+term2;
    The class implements the Rosenbrock banana function described in the fminsearch documentation.

  7. Customize the application using Visual Studio® .NET using the OptimizeCSApp folder, which contains a Visual Studio .NET project file for this example.

    1. . Open the project in Visual Studio .NET by double-clicking OptimizeCSApp.csproj in Windows® Explorer. You can also open it from the desktop by right-clicking OptimizeCSApp.csproj > Open Outside MATLAB.

    2. Add a reference to the MWArray component, which is matlabroot\toolbox\dotnetbuilder\bin\architecture\framework_version

    3. If necessary, add (or fix the location of) a reference to the OptimizeComp component which you built in a previous step. (The component, OptimizeComp.dll, is in the \OptimizeExample\OptimizeComp\x86\V2.0\Debug\distrib subfolder of your work area.)

When run successfully, the program displays the following output:

Using initial points= -1.2000 1

**            Properties of .NET Object            **

h =

  MathWorks.Examples.Optimize.BananaFunction handle 
             with no properties.
  Package: MathWorks.Examples.Optimize

className =


  Name  Size   Bytes  Class  Attributes

  h      1x1    60  MathWorks.Examples.Optimize.BananaFunction

Methods for class MathWorks.Examples.Optimize.BananaFunction:

BananaFunction    addlistener       findprop          lt
Equals            delete            ge                ne
GetHashCode       eq                gt                notify
GetType           evaluateFunction  isvalid
ToString          findobj           le

**************** Finished displayObj ****************

** Performing unconstrained nonlinear optimization **

directEval =


wrapperEval =


x =

    1.0000    1.0000

fval =


***************** Finished doOptim ******************

Location of minimum: 1.0000    1.0000
Function value at minimum: 8.1777e-010