Mapping input variables to a new input space because relationship from x to y is so nonlinear that fitting a lookup table is difficult. Large nonlinear changes are not well captured by regularizeNd or regularizeNdMatrices.
Using a semilog space of the output, log10(y). When the output changes so rapidly that it jumps several orders of magnitude, you may want to consider fitting log10 of the output, y. The other reason is that the input to the output may be polynomial like. If it is polynomial like, a polynomial in log space is smoother and changes slower.
Inequality and bounded constraints. Handling constraints are easy using regularizeNdMatrices with lsqlin or lsqConstrainedAlternative. Monotonic constraints and bounded constraints are easily handled in this example. Equality constraints are possible as well but are not discussed here.
Load and Clean the Data
For what we are going to do, we need to discard .
clc; clear; close all;
load('a06_constraints_and_mapping.mat','x','y');
index = y<=0;
x(index)=[];
y(index)=[];
Examine the Data
x to y is highly nonlinear. Specifically note the sudden and large changes in y at x=6 and x=6.45.
The mapping is created manually and best explained via the video below.
The idea is to make all sections of the data the same slope, , or at least a similar slope. See the video on how I get a rough idea of the sections of the data. pchip interpolation is used because it is the smoothest interpolant that guarantees monotonic interpolation (aka no oscillations) in the mapping; a 1 to 1 mapping (1 x point maps to only 1 xNew point) is desired/required. The akima interpolation method does not always give a monotonic interpolation.
title('Monotonic, 1 to 1 Mapping of x to xMapped')
% plot new input to output
xMapped = mapping(x);
figure('Position',figurePositionAndSize)
scatter(xMapped, y)
xlabel('xMapped')
ylabel('y')
set(gca,'yscale','log')
grid on;
title('xMapped to y. Note how much smoother this is.')
Setup Fidelity and Regularization Matrices
Set up an xGrid and then map it to the new xMapped space. The fitting will be done in the mapped space.
y is converted to log10 space because log10(y) is smoother than y.
Call regularizeNdMatrices. Get the fidelity matrix, Cfidelity, and the regularization matrix, Lregularization (in other cases, Lregularization is "matrices" if there is more than one input).
Combine the fidelity matrix, Cfidelity, and the regularization matrix, Lregularization, into one composite matrix, C.
Create the corresponding vector to C, d.
Note that the matrices correspond to the objective function:
In this case, the desired constraint is that the lookup table, yGrid, is monotonically increasing. Also, the bound is that no value in the table is less than 1e-2.
The constraints are formed as inequalities like this:
Using the lsqlin solver has advantages. It has more flexibility but the Optimization Toolbox is required. lsqlin can handle inequality, equality, and bound constraints. In some cases lsqlin won't converge when lsqConstrainedAlternative does. lsqlin can handle large sparse problems while lsqConstrainedAlternative is limited to smaller problems.
% make sure lsqlin exists. If it doesn't warn the user they don't have the optimization toolbox