Cody

# Problem 179. Monte-Carlo integration

Solution 23020

Submitted on 1 Feb 2012 by the cyclist
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
%% 1d integral: integrate x^2 from 0 to 1 fun = @(x) x^2; assert(abs((dquad(1,fun) - 1/3)*3)<0.01)

2   Pass
%% 2d integral from the example fun = @(x) x(1)*x(2); assert(abs((dquad(2,fun) - 0.25)*4)<0.01)

3   Pass
%% constant in most dimensions fun = @(x) 1+sin(x(1)); assert(abs((dquad(50,fun) - 1.45969769)/1.45969769)<0.01)

4   Pass
%% volume of d-dimensional 2^d box with a spherical hole, d between 5 and 10 d = randi([5 10],1) r = rand*0.8 fun = @(x) 2^d*(norm(x)>r); dball = exp(d/2*log(pi)+d*log(r)-gammaln(d/2+1)); assert(abs((dquad(d,fun) - 2^d+dball)/(2^d-dball))<0.01)

d = 10 r = 0.2575

### Community Treasure Hunt

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

Start Hunting!
{"ft_lang_label":"__label__en","ft_lang_prob":0.60746676,"math_prob":0.9974562,"size":883,"snap":"2021-04-2021-17","text_gpt3_token_len":341,"char_repetition_ratio":0.12400455,"word_repetition_ratio":0.0,"special_character_ratio":0.4382786,"punctuation_ratio":0.11386139,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9967306,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-01-18T11:53:12Z\",\"WARC-Record-ID\":\"<urn:uuid:0774c888-ca27-4859-ae5e-237be7f51dcb>\",\"Content-Length\":\"83804\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:4c663c49-aec7-4cac-a633-752cd7fd32c8>\",\"WARC-Concurrent-To\":\"<urn:uuid:a2e0986d-d4c7-4dcf-b35f-003a331d0528>\",\"WARC-IP-Address\":\"23.223.252.57\",\"WARC-Target-URI\":\"https://www.mathworks.com/matlabcentral/cody/problems/179-monte-carlo-integration/solutions/23020\",\"WARC-Payload-Digest\":\"sha1:PRBZ7LOPXLMFDYTV2I3GAGQCXAYOFHLB\",\"WARC-Block-Digest\":\"sha1:7OQD57E6FXJOVSW2E5UA4KEBK44GUCMS\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-04/CC-MAIN-2021-04_segments_1610703514495.52_warc_CC-MAIN-20210118092350-20210118122350-00160.warc.gz\"}"}