% Optimization example -- Neural Network

N=size(u,1);              % Number of data points
Nh=4;                     % Number of hidden neurons
Ni=2;                     % Number of input neurons
Phi=[y(2:N,1), u(2:N,1)]; % Regressors
Y=y(1:N-1,1);             % Output 

% Initialize weights and biases.
x0=nninit(Phi,Y,Nh);  % Initial weights and biases

% Set the options.

% Select the Levenberg-Marquardt and indicate that the Jacobian
% is provided by the user.
options=optimoptions('lsqnonlin','Algorithm','levenberg-marquardt',...
                 'SpecifyObjectiveGradient',true);

% Also set termination tolerance on parameters and objective 
% function.
options=optimoptions(options,'StepTolerance',0.001,'OptimalityTolerance',0.001);

% Train the neural network.
[x,~,~,exitflag]=lsqnonlin(@(x) nnfun(x,Phi,Y,Nh),x0,[],[],options)
