This example shows an application that calculates a bond price from a simple formula.
You run this example by entering the following known values into a simple graphical interface:
Coupon payment — C
Number of payments — N
Interest rate — i
Value of bond or option at maturity — M
The application calculates price (P
) based
on the following equation:
P = C * ( (1 - (1 + i)^-N) / i ) + M * (1 + i)^-N
The Bond Pricing Tool demonstrates the following features of MATLAB® Production Server™:
Deploying a simple MATLAB function with a fixed number of inputs and a single output
Deploying a MATLAB function with a simple GUI front-end for data input
Using dispose()
to free system
resources
Implement the Bond Pricing Tool in MATLAB, by writing the
following code. Name the code pricecalc.m
.
Sample code is available in
.MPS_INSTALL
\client\java\examples\BondPricingTool\MATLAB
function price = pricecalc(value_at_maturity, coupon_payment,... interest_rate, num_payments) C = coupon_payment; N = num_payments; i = interest_rate; M = value_at_maturity; price = C * ( (1 - (1 + i)^-N) / i ) + M * (1 + i)^-N; end
To create the deployable archive for this example:
From MATLAB, select the Production Server Compiler App.
In the Application Type list, select Deployable Archive.
In the Exported Functions field,
add pricecalc.m
.
pricecalc.m
is located in
.MPS_INSTALL
\client\java\examples\BondPricingTool\MATLAB
Under Application Information, change pricecalc
to BondTools
.
Click Package.
The generated deployable archive, BondTools.ctf
is
located in the for_redistribution_files_only
of
the project’s folder.
Download the MATLAB Runtime, if needed, at http://www.mathworks.com/products/compiler/mcr. See Download and Install the MATLAB Runtime for more information.
Create a server using mps-new
.
See Create a Server for
more information.
If you have not already done so, specify the location
of the MATLAB Runtime to the server by editing the server configuration
file, main_config
and specifying a path for --mcr-root
.
See Edit the Configuration File for details.
Start the server using mps-start
and verify
it is running with mps-status
.
Copy the BondTools.ctf
file to
the auto_deploy
folder on the server for hosting.
Create a compatible client interface and define methods in Java® to
match MATLAB function pricecalc.m
, hosted
by the server as BondTools.ctf
, using the guidelines
in this section.
Additional Java files are also included that are typical
of a standalone application. You can find the example files in
.MPS_INSTALL
\client\java\examples\BondPricingTool\Java
This Java code... | Provides this functionality... |
---|---|
BondPricingTool.java | Runs the calculator application. The variable values of the pricing function are declared in this class. |
BondTools.java | Defines pricecalc method interface, which
is later used to connect to a server to invoke pricecalc.m |
BondToolsFactory.java | Factory that creates new instances of BondTools |
BondToolsStub.java | Java class that implements a dummy |
BondToolsStubFactory.java | Factory that returns new instances of BondToolsStub |
RequestSpeedMeter.java | Displays a GUI interface and accepts inputs using Java Swing classes |
ServerBondToolsFactory.java | Factory that creates new instances of MWHttpClient and
creates a proxy that provides an implementation of the BondTools interface
and allows access to pricecalc.m , hosted by the
server |
When developing your Java code, note the following essential tasks, described in the sections that follow. For more information about clients coding basics and best practices, see Java Client Coding Best Practices.
This documentation references specific portions of the client
code. You can find the complete Java client code in
.MPS_INSTALL
\client\java\examples\BondPricingTool\Java
To use the MATLAB functions you defined in Step 1: Write MATLAB Code, declare the
corresponding Java method signature in the interface BondTools.java
:
interface BondTools { double pricecalc (double faceValue, double couponYield, double interestRate, double numPayments) throws IOException, MATLABException; }
This interface creates an array of primitive double
types,
corresponding to the MATLAB primitive types (Double
,
in MATLAB, unless explicitly declared) in pricecalc.m
.
A one to one mapping exists between the input arguments in both the MATLAB function
and the Java interface The interface specifies compatible type double
.
This compliance between the MATLAB and Java signatures demonstrates
the guidelines listed in Java Client Coding Best Practices.
In the ServerBondToolsFactory
class, perform
a typical MATLAB
Production Server client setup:
Instantiate MWClient
with an instance
of MWHttpClient
:
... private final MWClient client = new MWHttpClient();
Call createProxy
on the new client
instance. Specify port number (9910
) and the deployable
archive name (BondTools
) the server is hosting
in the auto_deploy
folder:
... public BondTools newInstance () throws Exception { mpsUrl = new URL("http://user1.dhcp.mathworks.com:9910/BondTools"); return client.createProxy(mpsUrl, BondTools.class); } ...
This application makes use of the Factory pattern to encapsulate creation of several types of objects.
Any time you create objects—and therefore allocate resources—ensure
you free those resources using dispose()
.
For example, note that in ServerBondToolsFactory.java
,
you dispose of the MWHttpClient
instance you created
in Instantiate MWClient, Create Proxy, and Specify Deployable Archive when
it is no longer needed.
Additionally, note the dispose()
calls to
clean up the factories in BondToolsStubFactory.java
and BondTools.java
.
Before you attempt to build and run your client code, ensure that you have done the following:
Added mps_client.jar
(
)
to your Java $MPS_INSTALL
\client\javaCLASSPATH
and Build
Path.
Copied your deployable archive to your server’s auto_deploy
folder.
Modified your server’s main_config
file
to point to where your MATLAB Runtime is installed.
When you run the calculator application, you should see the following output: