% Numerical Analysis
% Department of Applied Mathematics
% National Dong Hwa University
%
function demo_fsolve_CNN2()
syms c % adaptable parameters in an MLP network 
M=12;filter_no_1 = 5; filter_no_2 = 5;
% initialization
c0=rand(M,M,30)*2-1;
size(c0)
% preparation of training data
% uniform sampling
% Substitute to the target function g 
A=rand(28,28,500);
BB = rand(M*M*25 + 17*17*5,1);
% B=rand(M,M,30)*2-1;
y = cnn_my(A,BB,25);
size(y)
% Levenberg-Marquardt method
options = optimoptions('fsolve','Algorithm', 'levenberg-marquardt') 
% specification of a nonlinear system for MLP_learning
f = @(c)learning_cnn(c,A,y);
% apply fsolve
% Verification of c_zero
c_zero = fsolve(f,c0,options) 
mean(mean(mean((learning_cnn(c_zero,A,y)).^2)))
end

% a nonlinear system for CNN_learning 
function F = learning_cnn(C,A,y) 
filter_no = size(C,3);
F=0;
for i=1:filter_no
    F=F+tanh(convn(A,C(:,:,i))); 
end
F =F-y; 
end

function ans = cnn_my(A,BB,N)
B1=BB(1:N*12*12);
B2=BB(end-17*17*5+1:end);
% filter_no_1 equlas filter_no_2

% form B from B1
filter_no = size(B,3); 
filter_no_1 = sqrt(N);
filter_no_2 = sqrt(N);
for j=1:filter_no_1
    h(:,:,:,j)=zeros(17,17,size(A,3));
    for i=1:filter_no_2
        cc = tanh(convn(A,B(:,:,(j-1)*filter_no_2+i),'valid'));
        h(:,:,:,j)=h(:,:,:,j)+cc;
    end
end
for j = 1:filter_no_1
    deg = zeros(500,5);
    for n=1:500
       deg(n,j) = sum(sum(h(:,:,n,j).*B(:,:,N+j)));
    end
end
ans = sum(deg,2);
end
function h = cnn_hat(C,A)
filter_no = size(C,3); h=0;
for i=1:filter_no
h=h+tanh(convn(A,C(:,:,i))); 
end
end