Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB Builder NE   

Matrix Math Example

Purpose

The purpose of the example is to show you the following:

This example builds a .NET component to perform matrix math. The example creates a program that performs Cholesky, LU, and QR factorizations on a simple tridiagonal matrix (finite difference matrix) with the following form:

A = [ 2 -1  0  0  0
     -1  2 -1  0  0
      0 -1  2 -1  0
      0  0 -1  2 -1
      0  0  0 -1  2 ]

You supply the size of the matrix on the command line, and the program constructs the matrix and performs the three factorizations. The original matrix and the results are printed to standard output. You may optionally perform the calculations using a sparse matrix by specifying the string "sparse" as the second parameter on the command line.

Procedure

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

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

      matlabroot\toolbox\dotnetbuilder\Examples\VS8\NET\MatrixMathExample
      
    2. At the MATLAB command prompt, cd to the new MatrixMathExample subfolder in your work folder.

  2. Write the MATLAB functions as you would any MATLAB function.

    The code for the cholesky, ludecomp, and qrdecomp functions is already in your work folder in MatrixMathExample\MatrixMathComp\.

  3. While in MATLAB, issue the following command to open the Deployment Tool:

    deploytool

  4. Build the .NET component. See the instructions in Building Your Component for more details. Use the following information:

    Project NameMatrixMathComp
    Class NameFactor
    Files to compilecholesky.mludecomp.mqrdecomp.m

  5. Write source code for an application that accesses the component.

    The sample application for this example is in MatrixMathExample\MatrixMathCSApp\MatrixMathApp.cs.

    The program listing is shown here.

     MatrixMathApp.cs

    The statement

     Factor factor= new Factor(); 

    creates an instance of the class Factor.

    The following statements call the methods that encapsulate the MATLAB functions:

    argOut= factor.cholesky((MWArray)matrix);
    ...
    argsOut= factor.ludecomp(2, matrix);
    ...
    argsOut= factor.qrdecomp(2, matrix);
    ...

  6. Build the MatrixMathApp application using Visual Studio .NET.

    1. The MatrixMathCSApp folder contains a Visual Studio .NET project file for this example. Open the project in Visual Studio .NET by double-clicking MatrixMathCSApp.csproj in Windows Explorer. You can also open it from the MATLAB desktop by right-clicking MatrixMathCSApp.csproj > Open Outside MATLAB.

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

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

  7. Build and run the application in Visual Studio .NET.

MATLAB Functions to Be Encapsulated

The following code defines the MATLAB functions used in the example.

 cholesky.m

 ludecomp.m

 qrdecomp.m

Understanding the MatrixMath Program

The MatrixMath program takes one or two arguments from the command line. The first argument is converted to the integer order of the test matrix. If the string sparse is passed as the second argument, a sparse matrix is created to contain the test array. The Cholesky, LU, and QR factorizations are then computed and the results are displayed.

The main method has three parts:

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS