# Ho to perform Monte Carlo Simulation with a factor and a vector.

2 views (last 30 days)
Jonas Reibenspies on 12 May 2022
Commented: Jonas Reibenspies on 17 May 2022
Hey everybody, I'm trying to perform a Monte Carlo Simulation by randomly choosing 10 numbers of the factor and its standard deviaton 0.5859+-0.2321. Then these factors has to be multiplied with the vector TSS which has 353665 values. I found two possible ways of doing it, but non of them works.
The first:
clc
clear all
data_TSS=readtable('turb_danuube_15min.csv'); % data TSS & time
TSS=data_TSS{1:1:353665,2};
time=data_TSS{1:1:353665,1};
%% TSS-PAH-Factor
data_Corr2=readtable('Factor_York.xlsx'); % data corr. factor TSS - PAH
factor=data_Corr2{:,3};
fac=mean(factor); %mean of the different factors of York River = 0.5859
S=std(factor); %standard deviation = 0.2321
%% Calculate PAH with Monte Carlo
TSS=data_TSS{1:1:353665,2};
nsamples = 10;
MCfactor= 0.3538+0.4642*rand(nsamples,1); %(Mean-Std)+2*Std, always (10 times) choose one random Number in between 0.3538 and 0.8180
PAH=TSS.*MCfactor;
this is my error:
Arrays have incompatible sizes for this operation.
Error in Monte_Carlo_Var2 (line 21)
PAH=TSS.*MCfactor;
The second:
data_TSS=readtable('turb_danuube_15min.csv'); % data TSS & time
TSS=data_TSS{1:1:353665,2};
time=data_TSS{1:1:353665,1};
%% TSS-PAH-Factor
data_Corr2=readtable('Factor_York.xlsx'); % data corr. factor TSS - PAH
factor=data_Corr2{:,3};
fac=mean(factor); %mean of the different factors of York River = 0.5859
S=std(factor); %standard deviation = 0.2321
%% Calculate PAH with Monte Carlo
TSS=data_TSS{1:1:353665,2};
nsamples = 10;
for i=1:nsamples
MCfactor= 0.3538+0.4642*rand(1); %(Mean-Std)+2*Std, always choose one random Number in between 0.3538 and 0.8180
PAH(i)=MCfactor.*TSS;
end
this is my error:
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Error in Monte_Carlo_Var1 (line 20)
PAH(i)=MCfactor.*TSS;
If anybody has an idea how to solve this problem I would be very happy. Thanks in advance.
Greetings Jonas
##### 2 CommentsShowHide 1 older comment
Jonas Reibenspies on 16 May 2022
Hello Torsten, thanks for your answer. I want to get a matrix of the size 353665x10 with these 10 random picked factors as columns and every factor multiplied with every single value of TSS in the lines.
In the secon version in the end the factor is multiplied with TSS but it doesnt work properly.
I also tried adding something like:
TSS=data_TSS{1:1:353665,2};
nsamples = 10;
A=nan(length(TSS),nsamples);
%%
for i=1:nsamples
MCfactor= 0.3538+0.4642*rand(1); %(Mean-Std)+2*Std, always choose one random Number in between 0.3538 and 0.8180
MCPAH=MCfactor.*TSS;
end
A(:,i)=MCPAH(i);
but it still doesnt work.

Torsten on 16 May 2022
Edited: Torsten on 16 May 2022
data_TSS=readtable('turb_danuube_15min.csv'); % data TSS & time
TSS=data_TSS(1:1:353665,2);
time=data_TSS(1:1:353665,1);
%% TSS-PAH-Factor
data_Corr2=readtable('Factor_York.xlsx'); % data corr. factor TSS - PAH
factor=data_Corr2(:,3);
fac=mean(factor); %mean of the different factors of York River = 0.5859
S=std(factor); %standard deviation = 0.2321
%% Calculate PAH with Monte Carlo
nsamples = 10;
MCfactor= 0.3538+0.4642*rand(nsamples,1); %(Mean-Std)+2*Std, always (10 times) choose one random Number in between 0.3538 and 0.8180
TSS_rand = [TSS*MCfactor(1),TSS*MCfactor(2),TSS*MCfactor(3),TSS*MCfactor(4),...
TSS*MCfactor(5),TSS*MCfactor(6),TSS*MCfactor(7),TSS*MCfactor(8),...
TSS*MCfactor(9),TSS*MCfactor(10)];
Jonas Reibenspies on 17 May 2022
Thanks a lot!!

R2022a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by