You can definitely use MATLink with CVX or any other toolbox for that matter! The only constraint is that the quantities you transfer back to Mathematica should be ones that are supported by MATLink. (As a fun fact, my personal motivation for developing MATLink with Szabolcs was so that I can use Mathematica + CVX in my research.)
The following is an example of fitting an optimal
$L_∞$ norm polynomial using CVX and Mathematica via MATLink. First, we setup the problem in CVX and use MEvaluate
to run it on the MATLAB side (note: You will need to know how to use CVX).
MEvaluate["
n = 6;
m = 40;
randn('state',0);
% generate 50 ponts ui, vi
u = linspace(-1,1,m);
v = 1./(5+40*u.^2) + 0.1*u.^3 + 0.01*randn(1,m);
A = vander(u');
A = A(:,m-n+[1:n]);
% L-infty fit
cvx_begin quiet
variable x_inf(n)
minimize (norm(A*x_inf - v', inf))
cvx_end
"]
CVX is quite verbose and it is a bit more efficient to use quiet
when running it via MATLink. There are other CVX global variables (starting with cvx_
) that you can use to programmatically check which solver was used, whether the problem was solved or if it is infeasible, etc. rather than relying on the textual output. If you are doing active algorithm development using CVX, then it might be better to use MATLAB directly for this part and then use MATLink once you are satisfied and have frozen the routine.
Next, we transfer the optimal solution and the original quantities to Mathematica using MGet
:
{u, v, A, xinf} = MGet[{"u", "v", "A", "x_inf"}];
Finally, we compute the optimal
$L_2$ norm solution in Mathematica, construct the polynomial using Horner's method and plot them in Mathematica:
With[{hornerPoly = Function[{var, x}, Fold[# var + #2 &, x]]},
Show[
ListPlot[Transpose@{u, v}, PlotStyle -> {AbsolutePointSize[5], Gray}],
Plot[{hornerPoly[x, x2], hornerPoly[x, xinf]}, {x, -1, 1}, Evaluated -> True,
PlotLegends -> {"!(*SubscriptBox[(L), (2)])", "!(*SubscriptBox[(L), ([Infinity])])"}]
]
]
![](http://i.stack.imgur.com/QhzzL.png)
Hope that helped!