% Example in Doc sec 2.2.1 to 2.2.5  modified as in Sec. 2.2.6.2
fprintf(' Example in Doc, sec 2.2.1 to 2.2.5 modified as in Sec. 2.2.6.2 \n')
clear all
uqlab
rng(100)
PriorOpts.Name = 'Model parameters prior';
PriorOpts.Marginals(1).Name = 'b';
PriorOpts.Marginals(1).Type = 'Constant';
PriorOpts.Marginals(1).Parameters = 0.15; % (m)
PriorOpts.Marginals(2).Name = 'h';
PriorOpts.Marginals(2).Type = 'Constant';
PriorOpts.Marginals(2).Parameters = 0.3; % (m)
PriorOpts.Marginals(3).Name = 'L';
PriorOpts.Marginals(3).Type = 'Constant';
PriorOpts.Marginals(3).Parameters = 5; % (m)
PriorOpts.Marginals(4).Name = 'E';
% update for UQLab 1.4
PriorOpts.Marginals(4).Type = 'Lognormal';
PriorOpts.Marginals(4).Moments = [30 4.5] * 1e9 ; % (Pa)
PriorOpts.Marginals(5).Name = 'p';
PriorOpts.Marginals(5).Type = 'Constant';
PriorOpts.Marginals(5).Parameters = 12000; % (N/m)

myPriorDist = uq_createInput(PriorOpts);

ModelOpts.Name = 'Forward model';
ModelOpts.mFile = 'uq_SimplySupportedBeam';
myForwardModel = uq_createModel(ModelOpts);
V_mid = [12.84; 13.12; 12.13; 12.19; 12.67]/1000; % (m)
myData.Name = 'Beam mid-span deflection';
myData.y = V_mid;
%

DiscrepancyPriorOpts.Name = 'Prior of discrepancy parameter';
DiscrepancyPriorOpts.Marginals.Name = 'Sigma2';
DiscrepancyPriorOpts.Marginals.Type = 'Uniform';
DiscrepancyPriorOpts.Marginals.Parameters = [0, mean(V_mid)^2];
myDiscrepancyPrior = uq_createInput(DiscrepancyPriorOpts);

DiscrepancyOpts.Type = 'Gaussian';
DiscrepancyOpts.Prior = myDiscrepancyPrior;

% Since the last created INPUT object represents the prior density for 
% the Discrepancy, and not the Prior forthe considered parameters, 
% the Prior in BayesOpts should be defined explicitly
% BayesOpts.Prior = myPriorDist;
% If the line above is not uncommneted, one gets an error when
% uq_createAnalysis(BayesOpts) is executed, see below

BayesOpts.Discrepancy = DiscrepancyOpts;


BayesOpts.Type = 'Inversion';
BayesOpts.Data = myData;
% 
myBayesianAnalysis1a = uq_createAnalysis(BayesOpts);
uq_print(myBayesianAnalysis1a)
uq_display(myBayesianAnalysis1a)
% seems to work with UQ-Lab 2.0