%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                                                     %%
%%  Solution of the Poisson equation in one dimension  %%
%%                      u_xx = f                       %%
%%               using Green's functions               %%
%%                                                     %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% compact printing
format compact

% set limits beyond which f can be ignored;
% these will also be used as plot limits:
x_hi=3;
x_lo=-x_hi;

% set number of spikes ONLY IF IT IS NOT DEFINED YET.
if exist('n_max') ~= 1
  n_max=2
end

% size of the segments into which f is divided
d_xi=(x_hi-x_lo)/n_max;

% create the locations of the delta functions; put one
% delta function in the center of each segment
xi_vals=[x_lo+.5*d_xi : d_xi : x_hi-.499999*d_xi];

% set the strengths of the delta functions
str_vals=gf1d_f(xi_vals)*d_xi;

% create the values of x at which to evaluate the
% approximate (Green's function) solution; (take them
% mostly the same as the xi values):
x_app_vals=[x_lo, xi_vals, x_hi];

% create the approximate solution values by summing the
% Green's functions
u_app_vals=gf1d_sumgf(x_app_vals,xi_vals,str_vals,n_max);

% evaluate the exact solution at enough plot points
x_exa_vals=[x_lo:.2:x_hi];
u_exa_vals=gf1d_u_exa(x_exa_vals);

% plot approximate with red lines, exact with blue points
plot(x_app_vals,u_app_vals,'-r',x_exa_vals,u_exa_vals,'ob')
title(['n_{max} = ' , num2str(n_max)],'fontsize',24)

% print it
print(['gf1d_',num2str(n_max),'.ps'], '-deps')

% To print, try say,
%print('badlap.gif', '-dgif')
% If that complains about fonts, try
%print('badlap.ps', '-deps')
% and then convert that to gif using say ps2gif
