function model = exercise0_model
% quadratic poisson equation on unit interval
%
% u on [0,1] such that
%
% - laplace (k u) = 1
%    u(0) = u(1) = 0
%
% parameters k and m
%
% discretization with Finite differences
%

% B. Haasdonk 6.12.2011

model.xrange = [0,1];
model.xnumintervals = 100;
model.dx = (model.xrange(2)-model.xrange(1))/model.xnumintervals;

model.k = 1;
model.m = 1;

% for detailed simulation
model.gen_model_data  = @my_gen_model_data;
model.detailed_simulation  = @my_detailed_simulation;
model.plot_sim_data = @my_plot_sim_data;

%%% for real rb simulation:
model.gridtype = 'onedgrid';
model.no_lines = 0;
model.axis_equal = 0;

%%%%%%%%% auxiliary rb functions

function model_data = my_gen_model_data(model)

params.xrange = model.xrange;
params.xnumintervals = model.xnumintervals;
model_data.grid = onedgrid(params);
model_data.nelements = model.xnumintervals-1;

function sim_data = my_detailed_simulation(model, model_data)

n = model_data.nelements;

% Assembliere die linke Seite (Masse-Matrix)
invdx2 = (model.dx)^(-2);
d = 2 * invdx2 * ones(n,1);
d1 = ones(n-1,1)*(-invdx2);
D = model.k * spdiags([ [0;d1], d, [d1;0] ], [-1, 0, 1], n, n);

% Assembliere die rechte Seite
F = ones(n,1);

% Löse das Gleichungssystem
uk = D \ F;

% Schreibe das Ergebnis in die Ausgabevariable
sim_data.u = uk;

function p = my_plot_sim_data(model, detailed_data, sim_data, ...
			      plot_params)
uplot = [zeros(1,size(sim_data.u,2));sim_data.u;zeros(1,size(sim_data.u,2))];
p = plot(detailed_data.grid.X,uplot','Linewidth',2);
set(gca,'xlim',model.xrange);
set(gca,'ylim',[min(sim_data.u), max(sim_data.u)]);
if isfield(plot_params,'title')
  title(plot_params.title);
end;
if isfield(plot_params,'legend')
  legend(plot_params.legend);
end;





