Pre-Engineering 220 Introduction to MatLab ® & Scientific Programming j kiefer



Download 8.69 Mb.
Page106/128
Date02.05.2018
Size8.69 Mb.
#47255
1   ...   102   103   104   105   106   107   108   109   ...   128
d. Example

E



valuate , where is the region .


[If you want to try it, for r = 0.5, I = 0.57.]
This is equivalent to averaging the integrand over a circular area, thusly

.

Of course, often the shape of the region of integration isn’t a simple rectangle or circle.


e. Example script
% Script to carry out 2-dim integration via random sampling

f=inline('sin(sqrt(log(x+y+1)))')

n=100;

r=0.5;


r2=r*r;

sum=0;


ax=0.5-r;

ay=0.5-r;

bx=r+0.5;

by=r+0.5;

m=0

for i=1:n



ex=rand*(bx-ax)+ax;

why=rand*(by-ay)+ay;

are=(ex-0.5)^2+(why-0.5)^2;

if are<=r2

sum=sum+f(ex,why);

m=m+1;


end

end


sum=sum*(by-ay)*(bx-ax)/m;

fprintf('Integral of f(x,y) over the circle = %g',sum)




Download 8.69 Mb.

Share with your friends:
1   ...   102   103   104   105   106   107   108   109   ...   128




The database is protected by copyright ©ininet.org 2024
send message

    Main page