# Error using Sobol sensitivity analysis on PCE

**URL:** <https://uqworld.org/t/error-using-sobol-sensitivity-analysis-on-pce/1626>\
**Category:** Community Q&A and How To\
**Tags:** sensitivity, pce, sobol\
**Created:** [June 9, 2022, 8:57am UTC](https://uqworld.org/t/error-using-sobol-sensitivity-analysis-on-pce/1626 "2022-06-09T08:57:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Magnus](https://uqworld.org/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@Magnus](https://uqworld.org/u/Magnus)\
**Post date:** [June 9, 2022, 8:57am UTC](https://uqworld.org/t/error-using-sobol-sensitivity-analysis-on-pce/1626/1 "2022-06-09T08:57:09Z")

</div>

Hello everyone,

I am very new to UQ and sensitivity analysis, and I am getting the following error trying to use similar method as the borehole example.

```matlab
--- Calculating the PCE coefficients by regression. ---
Error using uq_lar (line 403)
Unable to perform assignment because the left and right sides have a different number of elements.

Error in uq_PCE_lars (line 64)
lar_results = uq_lar(Psi, Y, lar_options);

Error in uq_PCE_calculate_coefficients_regression (line 196)
                lars_results = uq_PCE_lars(univ_p_val, current_model);

Error in uq_PCE_calculate_coefficients (line 47)
            uq_PCE_calculate_coefficients_regression(current_model);

Error in uq_calculateMetamodel (line 18)
        success = uq_PCE_calculate_coefficients(current_model);

Error in uq_initialize_uq_metamodel (line 458)
success = uq_calculateMetamodel(current_model);

Error in uq_core_module/run_initialization_script (line 208)
                            initHandle(obj);

Error in uq_core_model/add_module (line 100)
                success = this.run_initialization_script(obj);

Error in uq_createModel (line 116)
eval(str);

Error in Sens_analysis (line 77)
myPCE = uq_createModel(PCEOpts);

```

My model looks like following, where y is the predicted impact load of a flat head on a beam in a pendulum test. rho and T are measured experimentally, where T is really not normal distributed but is varying depending on the dimensions (b,h,l).

It is possible to simplify the model by making E a gaussian distribution [12.4 1.7], however I get the same error…

```matlab
function [y] = ForcePredict(xx)

v = xx(1);
b = xx(2);
h = xx(3);
l = xx(4); 
rho = xx(5);
T = xx(6);

E = 48*(l - 180)^3*l*rho*10^(-9)/(pi^2*T^2*h^2);

y = (3*3475*v^2*E*b*240/h/(4*(3475/(rho*b*l*h + 1))))^(1/2);

```

And my input data as follows. Using this exact code but changing the input to borehole works just fine.

```matlab

% 2 - COMPUTATIONAL MODEL
ModelOpts.mFile = 'ForcePredict';
myModel = uq_createModel(ModelOpts);

% 3 - PROBABILISTIC INPUT MODEL

InputOpts.Marginals(1).Name = 'v'; % impact velocity
InputOpts.Marginals(1).Type = 'Uniform';
InputOpts.Marginals(1).Parameters = [9.51 9.57]; % (m/s)

InputOpts.Marginals(2).Name = 'b'; % width (impact 'height')
InputOpts.Marginals(2).Type = 'Uniform';
InputOpts.Marginals(2).Parameters = [160 200]; % (mm)

InputOpts.Marginals(3).Name = 'h'; % height
InputOpts.Marginals(3).Type = 'Uniform';
InputOpts.Marginals(3).Parameters = [160 320]; % (mm)

InputOpts.Marginals(4).Name = 'l'; % length 
InputOpts.Marginals(4).Type = 'Uniform';
InputOpts.Marginals(4).Parameters = [2900 4300]; % (mm)

InputOpts.Marginals(5).Name = 'rho'; % density
InputOpts.Marginals(5).Type = 'Gaussian';
InputOpts.Marginals(5).Parameters = [480 40]; % (kg/m^3)

InputOpts.Marginals(6).Name = 'T'; % Period from vibration test
InputOpts.Marginals(6).Type = 'Uniform';
InputOpts.Marginals(6).Parameters = [15 35]; % (ms)

% Create an INPUT object based on the specified marginals:
myInput = uq_createInput(InputOpts);

% Select the sensitivity analysis module in UQLab and the Sobol' analysis method:
SobolOpts.Type = 'Sensitivity';
SobolOpts.Method = 'Sobol';

% Specify the maximum order of the Sobol' indices to be calculated:
SobolOpts.Sobol.Order = 1;

% Specify the sample size for the MC simulation:
SobolOpts.Sobol.SampleSize = 1e5;

% 4.2 PCE-based Sobol' indices
% Select the metamodeling tool in UQLab and the polynomial chaos expansion (PCE) type:
PCEOpts.Type = 'Metamodel';
PCEOpts.MetaType = 'PCE';

% Assign the full computational model:

PCEOpts.FullModel = myModel;

% The full model is used to generate an experimental design for the PCE.
% Specify the maximum polynomial degree (default: sparse PCE):
PCEOpts.Degree = 5;

% Specify the size of the experimental design (i.e., the total computational cost of constructing the metamodel):
PCEOpts.ExpDesign.NSamples = 200;

% Calculate the PCE:
myPCE = uq_createModel(PCEOpts);

% The same options structure SobolOpts can be re-used to create a new analysis on the PCE model:
mySobolAnalysisPCE = uq_createAnalysis(SobolOpts);

% Retrieve the results for comparison:

mySobolResultsPCE = mySobolAnalysisPCE.Results;

uq_print(mySobolAnalysisPCE)

```

Any feedback is greatly appreciated

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![styfen.schaer](https://uqworld.org/user_avatar/uqworld.org/styfen.schaer/32/849_2.png) [@styfen.schaer](https://uqworld.org/u/styfen.schaer)\
**Post date:** [June 10, 2022, 8:37am UTC](https://uqworld.org/t/error-using-sobol-sensitivity-analysis-on-pce/1626/2 "2022-06-10T08:37:08Z")

</div>

Dear @Magnus

The problem is that your “ForcePredict” function is not vectorized.  
You can find the detailed explanation here:

> **[UQLab - the Model module user manual](https://www.uqlab.com/model-user-manual)**
>
> User manual for the modeling module of UQLab.

Best regards  
Styfen

---

<div class="post-metadata">

**Author:** ![Magnus](https://uqworld.org/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@Magnus](https://uqworld.org/u/Magnus)\
**Post date:** [June 10, 2022, 10:53am UTC](https://uqworld.org/t/error-using-sobol-sensitivity-analysis-on-pce/1626/3 "2022-06-10T10:53:26Z")

</div>

Thank you very much for your reply Styfen,

I used the example of borehole from [http://www.sfu.ca/~ssurjano/Code/boreholem.html](http://www.sfu.ca/~ssurjano/Code/boreholem.html) and not the vectorized from [https://uqworld.org/t/borehole-function/60](https://uqworld.org/t/borehole-function/60)

---

<div class="post-metadata">

**Author:** ![styfen.schaer](https://uqworld.org/user_avatar/uqworld.org/styfen.schaer/32/849_2.png) [@styfen.schaer](https://uqworld.org/u/styfen.schaer)\
**Post date:** [June 10, 2022, 1:24pm UTC](https://uqworld.org/t/error-using-sobol-sensitivity-analysis-on-pce/1626/4 "2022-06-10T13:24:28Z")

</div>

Dear @Magnus

Then you have to specify it explicitly with the flag:

```matlab
ModelOpts.isVectorized = false;

```

Note, however, that this can slow down your code significantly.

Best regards  
Styfen
