%% Portfolio Optimization (Black Litterman Approach)
% This example shows how to generate a MEX function and C source code from MATLAB code that performs portfolio optimization using the Black Litterman approach.
%% Prerequisites
% There are no prerequisites for this example.
%% About the hlblacklitterman Function
% The hlblacklitterman.m function reads in financial information regarding a portfolio and performs portfolio optimization using the Black Litterman approach.

type hlblacklitterman

% The %#codegen directive indicates that the MATLAB code is intended for code generation.
%
%% Generate the MEX Function for Testing
% Generate a MEX function using the codegen command.

codegen hlblacklitterman -args {0, zeros(1, 7), zeros(7,7), 0, zeros(1, 7), 0, 0}

% Before generating C code, you should first test the MEX function in MATLAB to ensure that it is functionally equivalent to the original MATLAB code and that no run-time errors occur. By default, codegen generates a MEX function named hlblacklitterman_mex in the current folder. This allows you to test the MATLAB code and MEX function and compare the results.
%
%% Run the MEX Function
% Call the generated MEX function

testMex();

%
%% Generate C Code

cfg = coder.config('lib');
codegen -config cfg hlblacklitterman  -args {0, zeros(1, 7), zeros(7,7), 0, zeros(1, 7), 0, 0}

% Using codegen with the specified -config cfg option produces a standalone C library.
%
%% Inspect the Generated Code
% By default, the code generated for the library is in the folder codegen/lib/hbblacklitterman/.
% The files are:

dir codegen/lib/hlblacklitterman/

%
%% Inspect the C Code for the hlblacklitterman.c Function

type codegen/lib/hlblacklitterman/hlblacklitterman.c

% Copyright 2018 The MathWorks, Inc.