- Do you need to use the symbolic calculations at all, rather than purely numerical calculations?
- Can you share your whole code?
Summation of matrices using symsum or other
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Johan Burger
il 9 Dic 2020
Commentato: Johan Burger
il 10 Dic 2020
I am trying to run a Monte Carlo simulation at about 1-10k iterations (N). Some of the inputs remain constant throughout the computation, while others (8 variables) are randomised at each iteration. My code currently works as follows:
- An array is created for each variable, with the length set to N.
- Each variable is passed through a function, which describes a mathematical equation.
- Data samples are analysed for trends.
In the past I have been using a for-loop and the symsum function but as N grows, the computation becomes very slow. Symsum requires a scalar for its bounds (but these are also arrays. One component of the final calculation is described by:
Where X, B and K are arrays. The symsum function I have been using is:
ans = double(symsum((X*Y)./((1+Z).^(A+k*B)),k,1,K));
I have played around with arrayfun, but the result was a single value instead of an array, size N, which I need.
My question is, how can I best calculate this value? I would need to run several of these simulations for my project and would like to trim down the time.
2 Commenti
Alan Stevens
il 9 Dic 2020
Risposta accettata
Alan Stevens
il 10 Dic 2020
It's difficult to tell from what you've listed, but have you tried getting rid of all symbolic stuff and using just numerics (which are much faster)? For example:
for i = 1:numberOfRuns
repSum(i) = sum(cREP(i)*capP./(1+r)^(tC+k*tR));
ic(i) = icPre(i) + repSum(i);
om_sum(i) = sum((cPom(i)*capP + cEom(i)*...
(cycPa*DoD*capE)*(1-cycDeg)^((k-1)*cycPa)*(1-tDeg)^(k-1))/(1+r)^(k+tC),k,1,N(i));
elec_sum = cycPa*DoD*capE*nRT*(1-nSELF)*sum(((1-cycDeg)...
^((k-1)*cycPa)*(1-tDeg)^(k-1))/(1+r)^(k+tC),k,1,N);
eol_sum(i) = (icPre(i))*fEOL/(1+r)^(N(i)+1);
end
Although I can't tell where you might need to replace * by .* or ^ by .^ etc because of the limited information here.
Also, do you need to keep every value of repSum (you have repSum(i)) even though it doesn't seem to be used outside of the loop?
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Cardiology in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!