![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
clc close clear format long syms x a p1=int(exp(2*x),-0.5,0); p2=int(exp(-2*x),0,0.5) double(p1+p2) total=1000000; c=0; for i=1:total rnd=rand(); if(rnd<=0.5) rnd1=0.5*rand(); v=0.5 *log(2*rnd1); else rnd2=0.5*rand(); v=-0.5 *log(1-2*rnd2); end if( v>=-0.5 && v<=0.5) c=c+1; end end (c/total)
说明
其中的rnd1可以用rnd代替,但是rnd2不能用rnd*0.5,这样的话结果对不上
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
clc close clear format long syms t a b y c x1 x2 u1 u2 uy E_YX1 E_YX2 E_X1X2 E_X12 E_X22 x total=100000; c=0; for i=1:total rnd=rand(); if(rnd<=1/4) v= sqrt(rand()*(1/4) * 4)+2; else u=rand()*(3/4); v=-sqrt(-12*u +9)+6; end if( v>2.5 && v<3.5) c=c+1; end end c/total p1=int(0.5*(x-2),2.5,3); p2=int(0.5*(2-(x/3)),3,3.5); double(p1+p2)
这个在开根号时需要考虑3<x<=6时, 0<u<3/4 ,所以开根号后加负号