function Graph
% This is for only pde depends on z
addpath('/Users/yuhuazhu/Documents/MATLAB/VPFP_linear')
addpath('/Users/yuhuazhu/Documents/MATLAB/Reduced basis')
addpath('/Users/yuhuazhu/Documents/MATLAB/General')
addpath('/Users/yuhuazhu/Documents/MATLAB/Interpolation')

%% Setting up
Setup_mesh([0,2*pi,32],[-6,6,64],0.5,1,1); 
global a; global b;  a= 1; b= 5; global dim; dim =1;
%reference parameter and exact solution
%%%%%%%%%%%%only need to run once%%%%%%%%%%%%
approx_n = 50;
[z, f_ext] = Test_f(approx_n);
save('exact solu eq6', 'z', 'f_ext');
%%%%%%%%%%%%only need to run once%%%%%%%%%%%%
load('exact solu eq6.mat');



%% Get reduced basis
%%%%%%%%%%%%only need to run once%%%%%%%%%%%%
Nz = 101; Z = linspace(a,b,Nz)'; %criteria = min([2*pi/32,12/64]/10);
criteria = 10^(-6);
[Z_select, f_T1_select, f_T2_select] = RB_linear(Z, criteria);
save('reduced basis eq6', 'Z_select','f_T1_select','f_T2_select')
%%%%%%%%%%%%only need to run once%%%%%%%%%%%%
load('reduced basis eq6.mat')


%% Greedy
[n,~] = size(Z_select);
[Nx,Nv,approx_n] = size(f_ext); N = Nx*Nv;
error_avg = zeros(n,1);
for i = 1:n
    f_approx = Proj_linear(z,Z_select(1:i,:),f_T1_select(:,:,1:i),f_T2_select(:,:,1:i));
    error = reshape(L2_xv(f_ext - f_approx),approx_n,1);
    error_avg(i) = sum(error)/approx_n;
end

%plot dim-error
semilogy(1:n, error_avg, '-b*', 'MarkerSize',8);
title('Greedy v.s. Collocation')
xlabel('Dimension') % x-axis label
ylabel('Averaged Error') % y-axis label
hold on;

%% interpolation
if n>10
    n = 10;
end
error_avg = zeros(n,1);
for i = 1:n
    Z_select = Guadrature_pt(i);
    f_0 = ID_EQ(Z_select);
    [~,f_T_select] = VPFP_linear(f_0, Z_select);
    [f_approx,~] = Poly_intp(z,Z_select,reshape(f_T_select,N,i));
    f_approx = reshape(f_approx,Nx,Nv,approx_n);
    error = reshape(L2_xv(f_ext - f_approx),approx_n,1);
    error_avg(i) = sum(error)/approx_n;
end

%plot dim-error
semilogy(1:n, error_avg, '-k*', 'MarkerSize',8);
legend('Greedy','Quad pts interpolation')
1;