function unknownCoefficientsN4 = WRM1D(unknownCoefficientsN4)

%  unknownCoefficientsN4 is a 6 X 4 matrix
%  unknownCoefficientsN4(1,:)  coefficients for Subdomain method
%  unknownCoefficientsN4(2,:)  coefficients for Collocation method
%  unknownCoefficientsN4(3,:)  coefficients for Finite Difference method
%  unknownCoefficientsN4(4,:)  coefficients for Least Square method
%  unknownCoefficientsN4(5,:)  coefficients for Galerkin method = phi = (x, x^2, x^3, x^4) 
%  unknownCoefficientsN4(6,:)  coefficients for Galerkin method = Finite
%       Element hat functions


root = 'figures';
[status,message,messageid] = mkdir(root);
%root = '';
if (length(root) > 0)
    root = [root, '/'];
end

Sind = 1;
Cind = 2;
FDind = 3;
Lind = 4;
Gind = 5;
Ghatind = 6;
numSol = 6;

numN = 4;

if (nargin < 1)
    unknownCoefficientsN4 = [];
end

if (length(unknownCoefficientsN4) == 0)
    unknownCoefficientsN4(Sind, 1) = 2;     unknownCoefficientsN4(Sind, 2) = -13/12; unknownCoefficientsN4(Sind, 3) = 1/2;  unknownCoefficientsN4(Sind, 4) = -1/12;
    unknownCoefficientsN4(Cind, 1) = 7/3;     unknownCoefficientsN4(Cind, 2) = -3/2; unknownCoefficientsN4(Cind, 3) = 5/6;  unknownCoefficientsN4(Cind, 4) = -1/6;
    unknownCoefficientsN4(FDind, 1) = 7/4;     unknownCoefficientsN4(FDind, 2) = 9/4; unknownCoefficientsN4(FDind, 3) = 11/4;  unknownCoefficientsN4(FDind, 4) = 13/4;
    unknownCoefficientsN4(Lind, 1) = 2;     unknownCoefficientsN4(Lind, 2) = -17/16; unknownCoefficientsN4(Lind, 3) = 23/48;  unknownCoefficientsN4(Lind, 4) = -5/64;
    unknownCoefficientsN4(Gind, 1) = 97/48;     unknownCoefficientsN4(Gind, 2) = -9/8; unknownCoefficientsN4(Gind, 3) = 17/32;  unknownCoefficientsN4(Gind, 4) = -35/384;
    unknownCoefficientsN4(Ghatind, 1) = 19/24;     unknownCoefficientsN4(Ghatind, 2) = 4/3; unknownCoefficientsN4(Ghatind, 3) = 11/6;  unknownCoefficientsN4(Ghatind, 4) = 7/3;
else
    [m, n] = size(unknownCoefficientsN4);
    if (m ~= 6)
        frpintf(1, '6 rows needed for unknownCoefficientsN4\n');
        pause;
    end
    if (n ~= 4)
        frpintf(1, '4 columns needed for unknownCoefficientsN4\n');
        pause;
    end
end

    
    
L = 2;
n = 400;

sInd = [Sind Cind Lind Gind];
nsInd = 4;

vec = {'S', 'C', 'FD', 'L', 'G', 'FE'};
lenvec = length(vec);
clr{Sind} = 'g';
clr{Cind} = 'c';
clr{Lind} = 'b';
clr{Gind} = 'r';
clr{Ghatind} = [0.6 0.6 0.6];
linwid = 2 * ones(numN, numSol);
linwid(1, Cind) = 6;
linwid(1, Gind) = 6;
linStyle{1} = '-';
linStyle{2} = '-.';
linStyle{3} = '--';
linStyle{4} = ':';


delx = 2/n;
for i = 1:(n + 1)
    x(i) = delx * (i - 1);
end


for i = 1:numN
    hs(i) = L / i;
    for ns = 1:numSol
        xs{ns}{i} = x;
    end
end

for i = 1:(n + 1)
    if (x(i) <= 1)
        y{Ghatind}{2}(i) = 4 / 3 * x(i) + 1;
        yExact(i) = x(i) * x(i) * (-1.0 + x(i) / 3.0) + 2.0 * x(i) + 1;
    else
        y{Ghatind}{2}(i) = 4 / 3 + (x(i) - 1) * (7/3 - 4/3) + 1;
        yExact(i) = x(i) + 4.0 / 3.0;
    end
    y{Sind}{1}(i) = 2 * x(i) + 1;
%    y{Sind}{1}(i) = 1 + 24478 / 13728 * x(i) - 2088/4576 * x(i) * x(i) - 17/4576 * x(i)^3 + 178 / 4576 * x(i)^4;
    y{Cind}{1}(i) = x(i) + 1;
    y{Lind}{1}(i) = x(i) + 1;
    y{Gind}{1}(i) = 7/6 * x(i) + 1;
    y{Ghatind}{1}(i) = y{Gind}{1}(i);
    
    y{Sind}{2}(i) = 2.0 * x(i) - x(i) * x(i) / 2.0 + 1;
    y{Cind}{2}(i) = x(i) + 1;
    y{Lind}{2}(i) = 2 * x(i) - 1/4 * x(i) * x(i) + 1;
    y{Gind}{2}(i) = 37/24 * x(i) - 3/16 * x(i) * x(i) + 1;

%    y3C(i) = 1 + 4/3 * x(i) - 2/3 * x(i)^2 + 1/6 * x(i)^3;
    y{Sind}{3}(i) = 1 + 2 * x(i) - 23/24 * x(i)^2 + 7/24 * x(i)^3;
    y{Cind}{3}(i) = 1 + 5/3 * x(i) - 2/3 * x(i)^2 + 1/6 * x(i)^3;
    y{Lind}{3}(i) = 1 + 2 * x(i) - 3/4 * x(i)^2 + 1/6 * x(i)^3;
    y{Gind}{3}(i) = 1 + 15/8 * x(i) - 11/16 * x(i)^2 + 1/6 * x(i)^3;
%   y3L(i) = 1 + 30/23 * x(i) + 1/92 * x(i)^2 - 2/69 * x(i)^3;
    a1GFEM3 = 80/81;
    a2GFEM3 = 5/3;
    a3GFEM3 = 7/3;
    
    if (x(i) < 2/3)
        frac = x(i)/(2/3);
        val = 0 * (1 - frac) + a1GFEM3 * frac;
    elseif (x(i) < 4/3)
        frac = (x(i) - 2/3)/(2/3);
        val = a1GFEM3 * (1 - frac) + a2GFEM3 * frac;
    else
        frac = (x(i) - 4/3)/(2/3);
        val = a2GFEM3 * (1 - frac) + a3GFEM3 * frac;
    end
    y{Ghatind}{3}(i) = val + 1;
    

    if (numN == 4)
        for l = 1:nsInd
            k = sInd(l);
            y{k}{4}(i) = 1 + x(i) * (unknownCoefficientsN4(k, 1) + x(i) * (unknownCoefficientsN4(k, 2) + x(i) * (unknownCoefficientsN4(k, 3) + x(i) * unknownCoefficientsN4(k, 4))));
        end
        a1GFEM4 = unknownCoefficientsN4(Ghatind, 1);
        a2GFEM4 = unknownCoefficientsN4(Ghatind, 2);
        a3GFEM4 = unknownCoefficientsN4(Ghatind, 3);
        a4GFEM4 = unknownCoefficientsN4(Ghatind, 4);
        
        if (x(i) < 1/2)
            frac = 2 * x(i);
            val = 0 * (1 - frac) + a1GFEM4 * frac;
        elseif (x(i) < 1)
            frac = (x(i) - 1/2)/(1/2);
            val = a1GFEM4 * (1 - frac) + a2GFEM4 * frac;
        elseif (x(i) < 3/2)
            frac = (x(i) - 1)/(1/2);
            val = a2GFEM4 * (1 - frac) + a3GFEM4 * frac;
        else
            frac = (x(i) - 3/2)/(1/2);
            val = a3GFEM4 * (1 - frac) + a4GFEM4 * frac;
        end
        y{Ghatind}{4}(i) = val + 1;
        
    end
        
end

xs{FDind}{1} = [0 2];
y{FDind}{1} = [1 3];
yD{FDind}{1} = [0 -1/3];
%
%
xs{FDind}{2} = [0 1 2];
y{FDind}{2} = [1 2 3];
yD{FDind}{2} = [0 -1/3 -1/3];

xs{FDind}{3} = [0 2/3 4/3 2];
y{FDind}{3} = [1  53/27 71/27 89/27];
yD{FDind}{3} = [0  -2/81 -1/27 -1/27];



if (numN == 4)
    y{FDind}{4} = [1 unknownCoefficientsN4(FDind, 1) unknownCoefficientsN4(FDind, 2) unknownCoefficientsN4(FDind, 3) unknownCoefficientsN4(FDind, 4)];
    xs{FDind}{4} = [0 1/2 1 3/2 2];
    yCrs4 = [1 43/24 7/3 17/6 10/3];
    yD{FDind}{4} = y{FDind}{4} - yCrs4;
end

for n = 1:numN
    plot(x, yExact, 'lineWidth', 4, 'Color', 'k');
    hold on;

    for j = 1:numSol
        if (j ~= FDind)
            plot(xs{j}{n}, y{j}{n}, 'lineWidth', linwid(n, j) , 'Color', clr{j});
        else
            plot(xs{FDind}{n}, y{FDind}{n}, 'lineStyle', 'none', 'Marker', 'o', 'MarkerSize', 10, 'MarkerFaceColor', 'm', 'MarkerEdgeColor', 'm');
        end
        hold on;
    end
       
    leg{1} = 'u';
    for i = 1:lenvec
        tmp = ['{', vec{i}, num2str(n), '}'];
        tmp = ['u^h_',tmp];
        leg{i + 1} = tmp;
    end
    legend(leg, 'location', 'NorthWest', 'FontSize', 18);
    %legend({'Exact', 'Subdomain', 'Collocation', 'Finite Difference', 'Least Square', 'Galerkin, Galerkin Hat'}, 'location', 'NorthWest', 'FontSize', 18);
    legend('boxoff');
    set(gca, 'FontSize', 18, 'XMinorTick', 'on', 'YMinorTick', 'on');
    print('-dpng', [root, 'WRM-solutions', num2str(n), 'Unknown.png']);
    close gcf;
 %
 %
 %
 %

    plot(x, 0 .* yExact, 'lineWidth', 2, 'Color', 'k');
    hold on;

    for j = 1:numSol
        if (j ~= FDind)
            yD{j}{n} = y{j}{n}- yExact;
            plot(xs{j}{n}, yD{j}{n}, 'lineWidth', linwid(n, j) , 'Color', clr{j});
        else
            plot(xs{FDind}{n}, yD{FDind}{n}, 'lineStyle', 'none', 'Marker', 'o', 'MarkerSize', 10, 'MarkerFaceColor', 'm', 'MarkerEdgeColor', 'm');
        end
        hold on;
     end
       
    leg{1} = '0';
    for i = 1:lenvec
        tmp = ['{', vec{i}, num2str(n), '}'];
        tmp = ['\Delta u^h_',tmp];
        leg{i + 1} = tmp;
    end
    legend(leg, 'location', 'NorthWest', 'FontSize', 18);
    %legend({'Exact', 'Subdomain', 'Collocation', 'Finite Difference', 'Least Square', 'Galerkin, Galerkin Hat'}, 'location', 'NorthWest', 'FontSize', 18);
    legend('boxoff');
    set(gca, 'FontSize', 18, 'XMinorTick', 'on', 'YMinorTick', 'on');
    print('-dpng', [root, 'WRM-errors', num2str(n), 'Unknown.png']);
    close gcf;
end


for j = 1:numSol
    leg = {};
    if (j ~= FDind)
        plot(x, yExact, 'lineWidth', 4, 'Color', 'k');
        hold on;
        leg{1} = 'u';
        for n = 1:numN
            plot(xs{j}{n}, y{j}{n}, 'lineWidth', linwid(3, j) , 'lineStyle', linStyle{n}, 'Color', clr{j});
            hold on;
            tmp = ['{', vec{j}, num2str(n), '}'];
            tmp = ['u^h_',tmp];
            leg{n + 1} = tmp;
        end
    else
        continue;
%        plot(xs{FDind}{n}, yD{FDind}{n}, 'lineStyle', 'none', 'Marker', 'o', 'MarkerSize', 10, 'MarkerFaceColor', 'm', 'MarkerEdgeColor', 'm');
    end
    legend(leg, 'location', 'NorthWest', 'FontSize', 18);
    %legend({'Exact', 'Subdomain', 'Collocation', 'Finite Difference', 'Least Square', 'Galerkin, Galerkin Hat'}, 'location', 'NorthWest', 'FontSize', 18);
    legend('boxoff');
    set(gca, 'FontSize', 18, 'XMinorTick', 'on', 'YMinorTick', 'on');
    print('-dpng', [root, 'WRM-method', num2str(j), '-', vec{j}, '.png']);
    close gcf;
%
%
%
%
    leg = {};
    if (j ~= FDind)
        plot(x, 0 * yExact, 'lineWidth', 4, 'Color', 'k');
        hold on;
        leg{1} = '0';
        for n = 1:numN
            plot(xs{j}{n}, yD{j}{n}, 'lineWidth', linwid(3, j) , 'lineStyle', linStyle{n}, 'Color', clr{j});
            tmpv = 2 * sum(yD{j}{n} .* yD{j}{n}) - yD{j}{n}(1) * yD{j}{n}(1) - yD{j}{n}(n + 1) * yD{j}{n}(n + 1);
            rmpv = tmpv * L / n / 2;
            yDv(j, n) = sqrt(rmpv);
            hold on;
            tmp = ['{', vec{j}, num2str(n), '}'];
            tmp = ['\Delta u^h_',tmp];
            leg{n + 1} = tmp;
        end
    else
        continue;
%        plot(xs{FDind}{n}, yD{FDind}{n}, 'lineStyle', 'none', 'Marker', 'o', 'MarkerSize', 10, 'MarkerFaceColor', 'm', 'MarkerEdgeColor', 'm');
    end
    legend(leg, 'location', 'NorthWest', 'FontSize', 18);
    %legend({'Exact', 'Subdomain', 'Collocation', 'Finite Difference', 'Least Square', 'Galerkin, Galerkin Hat'}, 'location', 'NorthWest', 'FontSize', 18);
    legend('boxoff');
    set(gca, 'FontSize', 18, 'XMinorTick', 'on', 'YMinorTick', 'on');
    print('-dpng', [root, 'WRM-error-method', num2str(j), '-', vec{j}, '.png']);
    close gcf;

end




cntr = 1;
leg = {};
for j = 1:numSol
    if (j ~= FDind)
        plot(1:numN, yDv(j, :), 'lineWidth', linwid(3, j), 'Color', clr{j}, 'Marker', 'o', 'MarkerSize', 10, 'MarkerFaceColor', clr{j}, 'MarkerEdgeColor', clr{j});
        hold on;
        tmp = ['{', vec{j},'}'];
        tmp = ['\Delta u^h_',tmp];
        leg{cntr} = ['L_2(', tmp, ')'];
        cntr = cntr + 1;
    else
        continue;
%        plot(xs{FDind}{n}, yD{FDind}{n}, 'lineStyle', 'none', 'Marker', 'o', 'MarkerSize', 10, 'MarkerFaceColor', 'm', 'MarkerEdgeColor', 'm');
    end
end
xlabel('n', 'FontSize', 25);
ylabel('error', 'FontSize', 25);
set(gca, 'XMinorGrid', 'on', 'YMinorGrid', 'on');
legend(leg, 'location', 'NorthEast', 'FontSize', 18);
%legend({'Exact', 'Subdomain', 'Collocation', 'Finite Difference', 'Least Square', 'Galerkin, Galerkin Hat'}, 'location', 'NorthWest', 'FontSize', 18);
legend('boxoff');
set(gca, 'FontSize', 18, 'XMinorTick', 'on', 'YMinorTick', 'on');
print('-dpng', [root, 'WRM-all-errors-n']);
close gcf;



cntr = 1;
leg = {};
for j = 1:numSol
    if (j ~= FDind)
        plot(hs, yDv(j, :), 'lineWidth', linwid(3, j), 'Color', clr{j}, 'Marker', 'o', 'MarkerSize', 10, 'MarkerFaceColor', clr{j}, 'MarkerEdgeColor', clr{j});
        hold on;
        tmp = ['{', vec{j},'}'];
        tmp = ['\Delta u^h_',tmp];
        leg{cntr} = ['L_2(', tmp, ')'];
        cntr = cntr + 1;
    else
        continue;
%        plot(xs{FDind}{n}, yD{FDind}{n}, 'lineStyle', 'none', 'Marker', 'o', 'MarkerSize', 10, 'MarkerFaceColor', 'm', 'MarkerEdgeColor', 'm');
    end
end
xlabel('h', 'FontSize', 25);
ylabel('error', 'FontSize', 25);
set(gca, 'XMinorGrid', 'on', 'YMinorGrid', 'on');
legend(leg, 'location', 'NorthWest', 'FontSize', 18);
%legend({'Exact', 'Subdomain', 'Collocation', 'Finite Difference', 'Least Square', 'Galerkin, Galerkin Hat'}, 'location', 'NorthWest', 'FontSize', 18);
legend('boxoff');
set(gca, 'FontSize', 18, 'XMinorTick', 'on', 'YMinorTick', 'on');
print('-dpng', [root, 'WRM-all-errors-h']);
close gcf;



cntr = 1;
leg = {};
for j = 1:numSol
    if (j ~= FDind)
        plot(log10(hs), log10(yDv(j, :)), 'lineWidth', linwid(3, j), 'Color', clr{j}, 'Marker', 'o', 'MarkerSize', 10, 'MarkerFaceColor', clr{j}, 'MarkerEdgeColor', clr{j});
        hold on;
        tmp = ['{', vec{j},'}'];
        tmp = ['\Delta u^h_',tmp];
        leg{cntr} = ['L_2(', tmp, ')'];
        cntr = cntr + 1;
    else
        continue;
%        plot(xs{FDind}{n}, yD{FDind}{n}, 'lineStyle', 'none', 'Marker', 'o', 'MarkerSize', 10, 'MarkerFaceColor', 'm', 'MarkerEdgeColor', 'm');
    end
end
xlabel('log_{10}(h)', 'FontSize', 25);
ylabel('log_{10}(error)', 'FontSize', 25);
set(gca, 'XMinorGrid', 'on', 'YMinorGrid', 'on');
if (numN ~= 4)
    legend(leg, 'location', 'NorthEast', 'FontSize', 18);
else
    legend(leg, 'location', 'SouthEast', 'FontSize', 18);
end
%legend({'Exact', 'Subdomain', 'Collocation', 'Finite Difference', 'Least Square', 'Galerkin, Galerkin Hat'}, 'location', 'NorthWest', 'FontSize', 18);
legend('boxoff');
set(gca, 'FontSize', 18, 'XMinorTick', 'on', 'YMinorTick', 'on');
print('-dpng', [root, 'WRM-all-errors-log-h']);
close gcf;










cntr = 1;
leg = {};
for j = 1:numSol
    if (j ~= FDind)
        plot(log10(hs), log10(yDv(j, :)), 'lineWidth', linwid(3, j), 'Color', clr{j}, 'Marker', 'o', 'MarkerSize', 10, 'MarkerFaceColor', clr{j}, 'MarkerEdgeColor', clr{j});
        tmp = ['{', vec{j},'}'];
        tmp = ['\Delta u^h_',tmp];
        leg{1} = ['L_2(', tmp, ')'];

    
        xlabel('log_{10}(h)', 'FontSize', 25);
        ylabel('log_{10}(error)', 'FontSize', 25);
        set(gca, 'XMinorGrid', 'on', 'YMinorGrid', 'on');
        if (numN ~= 4)
            legend(leg, 'location', 'NorthEast', 'FontSize', 18);
        else
            legend(leg, 'location', 'SouthEast', 'FontSize', 18);
        end
        %legend({'Exact', 'Subdomain', 'Collocation', 'Finite Difference', 'Least Square', 'Galerkin, Galerkin Hat'}, 'location', 'NorthWest', 'FontSize', 18);
        legend('boxoff');
        set(gca, 'FontSize', 18, 'XMinorTick', 'on', 'YMinorTick', 'on');
        print('-dpng', [root, 'WRM-error-log-h-method-', num2str(j), '-', vec{j}]);
        close gcf;    
    
    else
        continue;
%        plot(xs{FDind}{n}, yD{FDind}{n}, 'lineStyle', 'none', 'Marker', 'o', 'MarkerSize', 10, 'MarkerFaceColor', 'm', 'MarkerEdgeColor', 'm');
    end
end
