本文主要是介绍matlab模拟积分,MATLAB学习笔记:随机模拟法计算数值积分,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
>> 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
这篇关于matlab模拟积分,MATLAB学习笔记:随机模拟法计算数值积分的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!