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
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;
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.
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.
BondTools.ctf file to the
auto_deploy folder on the server for hosting.
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 Edit the Configuration File for details.
Save this code as an HTML file named
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 price different bonds. Varying the interest rate results in the most dramatic change in the price of the bond.