%% Problem 2 - A ReLU construction and learned exponential relaxation
% Generates all data analytically. Uses the supplied nn_train/nn_predict.
% No Deep Learning Toolbox or external data files are needed.
clear; clc; close all;

%% Part A: exactly reproduce the three-ReLU triangular function.
relu = @(x) max(0,x);
x = linspace(-2,2,401);
triangle = relu(x+1) - 2*relu(x) + relu(x-1);

figure;
plot(x,triangle,'LineWidth',1.7); grid on;
xlabel('x'); ylabel('f(x)'); title('Three ReLU neurons: triangular function');

%% Part B: learn du/dt = -0.7*u, u(0)=1 from example values.
% The network is fitted to samples; no differential-equation residual
% is included in training (PINNs belong to Lecture 9).
rng(8);                            % reproducible network weights
tTrain = linspace(0,5,55);
uTrain = exp(-0.7*tTrain);         % exact training samples

% Normalize time to roughly [-1,1] on the TRAINING interval.
% The same transformation must be used for all later predictions.
X = (tTrain-2.5)/2.5;
[net,loss] = nn_train(X,uTrain,16,3500,0.012,false);

% Unseen interior times and extrapolation beyond the training interval.
tInside = linspace(0.013,4.987,180);
tOutside = linspace(5.02,8,120);
uInside = exp(-0.7*tInside);
uOutside = exp(-0.7*tOutside);
pInside = nn_predict(net,(tInside-2.5)/2.5);
pOutside = nn_predict(net,(tOutside-2.5)/2.5);
rmse = @(p,y) sqrt(mean((p-y).^2));

fprintf('Interior RMSE:      %.6g\n',rmse(pInside,uInside));
fprintf('Extrapolation RMSE: %.6g\n',rmse(pOutside,uOutside));
Results = table(["Unseen interior";"Extrapolation"], ...
    [rmse(pInside,uInside);rmse(pOutside,uOutside)], ...
    'VariableNames',{'Region','RMSE'});
disp(Results);

% Plot the learned curve versus the known physical solution.
tPlot = linspace(0,8,350);
pPlot = nn_predict(net,(tPlot-2.5)/2.5);
figure;
plot(tPlot,exp(-0.7*tPlot),'k-','LineWidth',1.7); hold on;
plot(tPlot,pPlot,'r--','LineWidth',1.5);
plot(tTrain,uTrain,'bo','MarkerSize',3);
xline(5,':','training ends'); grid on;
xlabel('t'); ylabel('u(t)');
legend('Exact relaxation','Neural network','Training samples','Location','best');
title('Problem 2: fit and extrapolation');

figure;
semilogy(loss,'LineWidth',1.3); grid on;
xlabel('Epoch'); ylabel('Mean-square loss'); title('Training history');
