clc
clear
close all

np = 50;
Var = 0.5;
x = linspace(-2,2,np);
x2 = x;
y2 = x2.^2.*1+(x2-1).^2.*wgn(1,np,-40);
ySOC1 = x2.^2.+Var;
ySOC2 = x2.^2.-Var;
y = linspace(-1,4,np);
[X,Y] = meshgrid(x,y);

f = @(x,y) (1-x).^2 + 100*(y-x.^2).^2;
fy = @(y) f(x(1),y);
fSOC = @(x,c) (1-x).^2 + 100*c.^2;

% Definition of integer values
itg.fig = 1;
itg.lab = 1;
itg.fda = 1;
itg.fda = 1;
itg.axe = 1;
itg.ann = 1;

% Definition of figure and values
fig.dimw = 8.6;
fig.dimh = 6;
fig.Font = [10 8];
fig.label = [];
fig.ann   = [];

fig.in(itg.fig) = figure(itg.fig);
set(fig.in(itg.fig),'units','centimeters','position', [5+itg.fig,5+itg.fig, fig.dimw, fig.dimh]);
fig.axes(itg.axe) = gca;
[fig.mat,fig.cont] = contour(X,Y,f(X,Y),'k','LineWidth',.75);
fig.cont.LevelList = [100 250 500 1000 1500 2000];
clabel(fig.mat,fig.cont,[100 250 500 1000])
fig.cont.TextList 
hold on
fig.dat(itg.fda) = plot3(x,x.^2,f(x,x.^2),'Color',[0.93 0.69 0.13]); itg.fda = itg.fda+1;
% fig.dat(itg.fda) = line(x,y2(1:9:end),f(x(1:9:end),y2(1:9:end)),'Color','r','LineStyle','-.'); itg.fda = itg.fda+1;
fig.dat(itg.fda) = plot3(x,ySOC1,fSOC(x, Var),'r--'); itg.fda = itg.fda+1;
fig.dat(itg.fda) = plot3(x,ySOC2,fSOC(x,-Var),'r--'); itg.fda = itg.fda+1;
ylim([-1 4])
fig.label(itg.lab) = xlabel('Disturbance $d$'); itg.lab = itg.lab+1;
fig.label(itg.lab) = ylabel('Parameter $u$'); itg.lab = itg.lab+1;
legend('$J(d,u)$','$u_{opt}(d)$, $c = 0$','$c = \pm 0.5$')
set(fig.axes(itg.axe).Legend,'Location','North','FontSize',fig.Font(2),'Interpreter','latex','Box','on')
%fig.axes(itg.axe).XTickLabel = {};
%fig.axes(itg.axe).XTick = [];
%fig.axes(itg.axe).YTickLabel = {};
%fig.axes(itg.axe).YTick = [];

itg.fig = itg.fig+1;
itg.axe = itg.axe+1;

set(fig.label, 'Fontsize',      fig.Font(1));
set(fig.label, 'Interpreter',   'Latex');
set(fig.axes,  'FontSize',      fig.Font(2));
set(fig.axes,  'Box',           'on');
set(fig.axes,  'TickLabelInterpreter', 'Latex');
set(fig.axes,  'LineWidth',     .75);
set(fig.axes,  'XMinorTick',    'off');
set(fig.axes,  'YMinorTick',    'off');
set(fig.dat,   'LineWidth',     .75)
% figure(itg.fig)
% plot(x,f(x,y2))