This example shows how to create a web application that calculates the price of a bond from a simple formula. It uses the MATLAB® Production Server™ RESTful API and JSON Representation of MATLAB Data Types to depict an end-to-end workflow of using MATLAB Production Server. You run this example by entering the following known values into a web interface:
Face value (or value of bond at maturity) —
Coupon payment —
Number of payments —
Interest rate —
The application calculates price (
P) based on the following
P = C * ( (1 - (1 + i)^-N) / i ) + M * (1 + i)^-N
Step 1: Write MATLAB Code
Write the following code in MATLAB to price bonds. Save the code using the filename
function price = pricecalc(face_value, coupon_payment,... interest_rate, num_payments) M = face_value; C = coupon_payment; N = num_payments; i = interest_rate; price = C * ( (1 - (1 + i)^-N) / i ) + M * (1 + i)^-N;
Step 2: Create a Deployable Archive with the Production Server Compiler App
To create the deployable archive for this example:
On the Apps tab, select the Production Server Compiler App.
In the Application Type list, select Deployable Archive.
In the Exported Functions field, add
Under Archive information, change
The generated deployable archive,
BondTools.ctf is located in
for_redistribution folder of the project.
Step 3: Place the Deployable Archive on a Server
Download the MATLAB Runtime, if needed, at https://www.mathworks.com/products/compiler/mcr. See Supported MATLAB Runtime Versions for MATLAB Production 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_configand specifying a path for
--mcr-root. See Server Configuration Properties for details.
BondTools.ctffile to the
auto_deployfolder on the server for hosting.
Step 4: Enable Cross-Origin Resource Sharing (CORS) on the Server
Enable Cross-Origin Resource Sharing (CORS) by editing the server configuration
main_config and specifying the list of domain origins from
which requests can be made to the server. For example, setting the
cors-allowed-origins option to
--cors-allowed-origins * allows requests from any domain to
access the server. See cors-allowed-origins and Server Configuration Properties for details.
Save this code as an HTML file named
Step 7: Run Example
Confirm that the server with the deployed MATLAB function is running. Open the HTML file
bptool.html in a web browser. The default bond price is
NaN because no values have been entered as yet. Try the
following values to price a bond:
Face Value = $1000
Coupon Payment = $100
Number of payments = 5
Interest rate = 0.08 (Corresponds to 8%)
The resulting bond price is $1079.85.
Use the sliders in the tool to price different bonds. Varying the interest rate results in the most dramatic change in the price of the bond.