# I am trying to apply different sampling methods for a function but I am only able to use the Monte Carlo method, the others do not work

**URL:** <https://uqworld.org/t/i-am-trying-to-apply-different-sampling-methods-for-a-function-but-i-am-only-able-to-use-the-monte-carlo-method-the-others-do-not-work/1989>\
**Category:** Community Q&A and How To\
**Tags:** pce\
**Created:** [September 17, 2023, 8:35pm UTC](https://uqworld.org/t/i-am-trying-to-apply-different-sampling-methods-for-a-function-but-i-am-only-able-to-use-the-monte-carlo-method-the-others-do-not-work/1989 "2023-09-17T20:35:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![SimoneRomani12](https://uqworld.org/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@SimoneRomani12](https://uqworld.org/u/SimoneRomani12)\
**Post date:** [September 17, 2023, 8:35pm UTC](https://uqworld.org/t/i-am-trying-to-apply-different-sampling-methods-for-a-function-but-i-am-only-able-to-use-the-monte-carlo-method-the-others-do-not-work/1989/1 "2023-09-17T20:35:54Z")

</div>

I am trying to use the Monte Carlo, Sobol, Halton and Latin hipercube sampling strategies for a test function written by me.  
Monte Carlo method works but the others sampling methods not and I do not understand what is the error. for completness I write the code:

clearvars  
rng(100,‘twister’)  
uqlab  
ModelOpts.mString = ‘X-\*sin(X).\*cos(X)’;  
ModelOpts.isVectorized = true;  
myModel = uq\_createModel(ModelOpts);  
IOpts.Marginals.Type = ‘uniform’;  
IOpts.Marginals.Parameters = [0 , 6];  
myInput = uq\_createInput(IOpts);  
X\_MC = uq\_getSample(20,‘MC’);  
Y\_MC = uq\_evalModel(myModel,X\_MC);  
X\_LHS = uq\_getSample(20,‘LHS’);  
Y\_LHS = uq\_evalModel(myModel,X\_LHS);  
X\_H = uq\_getSample(20,‘Halton’);  
Y\_H = uq\_evalModel(myModel,X\_H);  
X\_S = uq\_getSample(20,‘Sobol’);  
Y\_S = uq\_evalModel(myModel,X\_S);

---

<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:** [September 18, 2023, 7:20am UTC](https://uqworld.org/t/i-am-trying-to-apply-different-sampling-methods-for-a-function-but-i-am-only-able-to-use-the-monte-carlo-method-the-others-do-not-work/1989/2 "2023-09-18T07:20:26Z")

</div>

Dear @SimoneRomani12

Can you describe in more detail what did not work and what error you get?

Regardless, I notice that you have an invalid `mString`.  
`X-*sin(X).*cos(X)'` is not a valid Matlab syntax. Change it to `'X-sin(X).*cos(X)'`.

Best regards  
Styfen

---

<div class="post-metadata">

**Author:** ![SimoneRomani12](https://uqworld.org/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@SimoneRomani12](https://uqworld.org/u/SimoneRomani12)\
**Post date:** [September 18, 2023, 7:59am UTC](https://uqworld.org/t/i-am-trying-to-apply-different-sampling-methods-for-a-function-but-i-am-only-able-to-use-the-monte-carlo-method-the-others-do-not-work/1989/3 "2023-09-18T07:59:01Z")

</div>

Dear Styfen,  
thank you very much for your answer, there is one mistake in the formula that I sent, in fact the right equation is X\*sin(X)\*cos(X).  
When I compute the code I have the following error message:

Error using uq\_lhs  
Undefined function ‘lhsdesign’ for input arguments of type ‘double’.

---

<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:** [September 18, 2023, 12:17pm UTC](https://uqworld.org/t/i-am-trying-to-apply-different-sampling-methods-for-a-function-but-i-am-only-able-to-use-the-monte-carlo-method-the-others-do-not-work/1989/4 "2023-09-18T12:17:02Z")

</div>

Dear @SimoneRomani12

With the corrected m-string it works fine for me. Can you run the exact code below and post the full error message if it still doesn’t work?

```matlab
clearvars

rng(100,'twister')

uqlab

ModelOpts.mString = 'X .* sin(X) .* cos(X)';
ModelOpts.isVectorized = true;
myModel = uq_createModel(ModelOpts);

IOpts.Marginals.Type = 'uniform';
IOpts.Marginals.Parameters = [0 , 6];
myInput = uq_createInput(IOpts);

N = 1e3;

X_MC = uq_getSample(N,'MC');
Y_MC = uq_evalModel(myModel,X_MC);

X_LHS = uq_getSample(N,'LHS');
Y_LHS = uq_evalModel(myModel,X_LHS);

X_H = uq_getSample(N,'Halton');
Y_H = uq_evalModel(myModel,X_H);

X_S = uq_getSample(N,'Sobol');
Y_S = uq_evalModel(myModel,X_S);

```

---

<div class="post-metadata">

**Author:** ![SimoneRomani12](https://uqworld.org/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@SimoneRomani12](https://uqworld.org/u/SimoneRomani12)\
**Post date:** [September 19, 2023, 6:52am UTC](https://uqworld.org/t/i-am-trying-to-apply-different-sampling-methods-for-a-function-but-i-am-only-able-to-use-the-monte-carlo-method-the-others-do-not-work/1989/5 "2023-09-19T06:52:05Z")

</div>

Dear Styfen,  
thank you very much for your support, unfortunatelly the code does not work yet.  
I send you the complete matlab error message and for completeness I say you that I downloaded UQLab 2.0.0 folder and I created this ‘‘matlab exercize’’ inside the ‘‘core’’ folder, maybe the position of my matlab file is wrong.  
The error messages are:

Error using uq\_lhs  
Undefined function ‘lhsdesign’ for input arguments of type ‘double’.

Error in uq\_sampleU (line 54)  
u = uq\_lhs(N, M, 0, options.LHSiterations);

Error in uq\_default\_input (line 104)  
u(:,indNonConst) = uq\_sampleU(N, M,samplingOptions);

Error in uq\_getSample (line 42)  
[varargout{1:nargout}] = current\_input.getSample(current\_input,varargin{:});

Error in Experiment\_1\_PCE (line 32)  
X\_LHS = uq\_getSample(N,‘LHS’);

---

<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:** [September 19, 2023, 9:11am UTC](https://uqworld.org/t/i-am-trying-to-apply-different-sampling-methods-for-a-function-but-i-am-only-able-to-use-the-monte-carlo-method-the-others-do-not-work/1989/6 "2023-09-19T09:11:45Z")

</div>

- Can you first check if the statistics toolbox is installed?
- Can you then uninstall your current UQLab installation, including any pre-existing versions, from your MATLAB search path? Then proceed to reinstall the most recent UQLab version and run the example outside the UQLab directory. Are you still encountering the issue under these conditions?

---

<div class="post-metadata">

**Author:** ![SimoneRomani12](https://uqworld.org/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@SimoneRomani12](https://uqworld.org/u/SimoneRomani12)\
**Post date:** [September 19, 2023, 1:24pm UTC](https://uqworld.org/t/i-am-trying-to-apply-different-sampling-methods-for-a-function-but-i-am-only-able-to-use-the-monte-carlo-method-the-others-do-not-work/1989/7 "2023-09-19T13:24:50Z")

</div>

Dear Styfen,  
Using Matlab R2020b the code runs instead using Matlab R2022b the code does not work.  
In conclusion I think the problem is associated to the Matlab version (maybe some package is not installed) , I will use the first version to avoid potential problems.  
Thank you so much for your help.
