Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB Builder JA   

Matrix Math Example

Purpose

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

This example builds a Java 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.

MATLAB Functions to Be Encapsulated

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

 cholesky.m

 ludecomp.m

 qrdecomp.m

Understanding the getfactor Program

The getfactor program takes one or two arguments from standard input. 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 to standard output.

The main method has three parts:

Procedure

Step-by-Step 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 MATLAB to your work folder:

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

  2. If you have not already done so, set the environment variables that are required on a development machine. See Settings for Environment Variables (Development Machine).

  3. 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\MatrixMathDemoComp\.

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

    deploytool

  5. You create a Java application by using the Deployment Tool GUI to build a Java class that wraps around your M-code.

    To compile or build the Java application using the Deployment Tool, use the following information as you work through this example in Building the Java Component:

    Project Namefactormatrix
    Class Namefactor
    Files to compilecholesky.mludecomp.mqrdecomp.m

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

    The sample application for this example is in MatrixMathExample\MatrixMathDemoJavaApp\getfactor.java.

    The program listing is shown here.

     getfactor.java

    The statement:

    theFactor = new factor();
    

    creates an instance of the class factor.

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

    result = theFactor.cholesky(1, a);
    ...
    result = theFactor.ludecomp(2, a);
    ...
    result = theFactor.qrdecomp(2, a);
    ...
  7. Compile the getfactor application using javac. When entering this command, ensure there are no spaces between path names in the matlabroot argument. For example, there should be no space between javabuilder.jar; and .\distrib\factormatrix.jar in the following example.

    cd to the matlabroot\factormatrix folder. Ensure getfactor.java is in this folder.

    • On Windows, execute the following command:

      javac -classpath
      .;matlabroot\toolbox\javabuilder\jar\javabuilder.jar;
      .\distrib\factormatrix.jar getfactor.java
      
    • On UNIX, execute the following command:

      javac -classpath
      .:matlabroot/toolbox/javabuilder/jar/javabuilder.jar:
      ./distrib/factormatrix.jar getfactor.java
      

  8. Run the application.

    Run getfactor using a nonsparse matrix

    • On Windows, execute the getfactor class file as follows:

      java -classpath
      .;matlabroot\toolbox\javabuilder\jar\javabuilder.jar;
      .\distrib\factormatrix.jar 
      getfactor 4
      
    • On UNIX, execute the getfactor class file as follows:

      java -classpath
      .:matlabroot/toolbox/javabuilder/jar/javabuilder.jar:
      ./distrib/factormatrix.jar 
      getfactor 4
      

 Output for the Matrix Math Example

To run the same program for a sparse matrix, use the same command and add the string sparse to the command line:

java (... same arguments) getfactor 4 sparse

 Output for a Sparse Matrix

  


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