%% Problem 3 - Learning a damped plasma-oscillation model
% Phenomenological ODE: E'' + 2*gamma*E' + omega0^2*E = 0.
% E(0)=1, E'(0)=0. This is NOT a derivation of Landau damping.
% Trains three one-hidden-layer tanh networks from analytic samples.
% Helpers: nn_train.m, nn_predict.m. No data files or toolboxes.
clear; clc; close all;

gamma = 0.2;
omega0 = 3;
wd = sqrt(omega0^2-gamma^2);
Eexact = @(t) exp(-gamma*t) .* ...
    (cos(wd*t) + gamma/wd*sin(wd*t));

% Training data in [0,8]. A different grid tests interpolation.
tTrain = linspace(0,8,161);
yTrain = Eexact(tTrain);
tInside = linspace(0.021,7.979,240);
tOutside = linspace(8.02,11,140);

sizes = [8 32 64];
epochs = 4500;
learningRate = 0.01;
trainLoss = zeros(epochs,numel(sizes));
trainRMSE = zeros(numel(sizes),1);
extraRMSE = zeros(numel(sizes),1);
frequencyError = NaN(numel(sizes),1);
phaseError = NaN(numel(sizes),1);
residualRMSE = zeros(numel(sizes),1);

% The physical oscillation has phase -atan(gamma/wd) in cosine notation.
truePhase = -atan(gamma/wd);

figure(1); hold on;
tAll = linspace(0,11,650);
plot(tAll,Eexact(tAll),'k-','LineWidth',2);
colors = lines(numel(sizes));

for k = 1:numel(sizes)
    rng(20+k);                  % document the initialization seed
    H = sizes(k);
    % t scaled to [-1,1] on the TRAINING domain.
    [net,loss] = nn_train((tTrain-4)/4,yTrain,H,epochs,learningRate,false);
    trainLoss(:,k) = loss;

    yInside = nn_predict(net,(tInside-4)/4);
    yOutside = nn_predict(net,(tOutside-4)/4);
    trainRMSE(k) = sqrt(mean((yInside-Eexact(tInside)).^2));
    extraRMSE(k) = sqrt(mean((yOutside-Eexact(tOutside)).^2));

    % Frequency and phase from the learned signal's zero crossings.
    % This is an approximate diagnostic, not a training constraint.
    tDense = linspace(0,8,2001);
    yDense = nn_predict(net,(tDense-4)/4);
    j = find(yDense(1:end-1).*yDense(2:end) < 0);
    crossings = tDense(j) - yDense(j).* ...
        (tDense(j+1)-tDense(j))./(yDense(j+1)-yDense(j));
    if numel(crossings) >= 2
        wEstimate = pi/mean(diff(crossings));
        frequencyError(k) = wEstimate-wd;
        % If first crossing is downward, cosine phase there is pi/2.
        if yDense(j(1)) > 0
            phi = pi/2-wEstimate*crossings(1);
            phaseError(k) = atan2(sin(phi-truePhase),cos(phi-truePhase));
        end
    end

    % Analytical time derivatives of the single-hidden-layer network:
    % Etheta(t) = W2*tanh(W1*(t-4)/4+b1)+b2.
    xx = (tInside-4)/4;
    A = tanh(net.W1*xx+net.b1);
    slope = net.W1/4;
    Et = net.W2 * ((1-A.^2).*slope);
    Ett = net.W2 * (-2*A.*(1-A.^2).*slope.^2);
    residual = Ett + 2*gamma*Et + omega0^2*yInside;
    residualRMSE(k) = sqrt(mean(residual.^2));

    yAll = nn_predict(net,(tAll-4)/4);
    plot(tAll,yAll,'--','LineWidth',1.4,'Color',colors(k,:));
end

xline(8,':','training ends');
xlabel('t'); ylabel('E(t)'); grid on;
legend('Analytical solution','8 neurons','32 neurons','64 neurons', ...
       'Location','best');
title('Problem 3: interpolation and extrapolation');

figure(2);
semilogy(trainLoss,'LineWidth',1.3); grid on;
xlabel('Epoch'); ylabel('Mean-square loss');
legend('8 neurons','32 neurons','64 neurons','Location','best');
title('Learning curves');

Results = table(sizes(:),trainRMSE,extraRMSE,frequencyError, ...
    phaseError,residualRMSE, ...
    'VariableNames',{'Hidden','Interior_RMSE','Extra_RMSE', ...
                     'Frequency_error','Phase_error_rad','ODE_residual_RMSE'});
disp(Results);
fprintf('Exact damped frequency omega_d = %.6f\n',wd);
% A small data-fit error and small equation residual are DIFFERENT tests.
