>> x=unifrnd(-2*pi,2*pi,10000,1);
>> f=(1-x.^2).*sin(5*x);
>> fmax=max(f)
fmax =
34.711289618412117
随机模拟法求定积分:
数值积分:
>> f=inline('exp(x.^2+1)','x');
>> quad(f,0,1)
ans =
3.975899680849105
xi=unifrnd(0,1,50000,1);
yi=9*rand(50000,1);
y=exp(xi.^2+1);
k=0;
for i=1:50000
if yi(i)<=y(i)
k=k+1;
end
end
S=k/50000*9
S =
3.985200000000000
数值积分:
>> f=@(x,y) exp(sqrt(1+x.^2+y.^2));
>> quad2d(f,0,1,1,4)
ans = 64.637437598359753
随机模拟法:
x=unifrnd(0,1,1000000,1);
y=unifrnd(1,4,1000000,1);
zi=unifrnd(0,70,1000000,1);
z=exp(sqrt(1+x.^2+y.^2));
k=0;
for i=1:1000000
if zi(i)<=z(i)
k=k+1;
end
end
P=k/1000000*210
P =
64.739639999999994