Monte Carlo Approch In MATLAB

x=rand(1000,1);
f=(x<0.6);
p=sum(f);
q=sum(1-f);
[p, q];
bar([0,1],[p,q]/1000);

x=rand(1000,1)
 create a random matrix of size 1000x1 where 1000 is number of rows and 1 is number of cols.
Note : rand(1000,1) generates 1000 numbers in the range of 0 to 1. By removing semicolons you see in command window when running this program.

f=(x<0.6)
Here is a matrix which store values form x matrix whose value is less then 0.6.

p=sum(f)
p store sum of all elements of matrix f.

q=sum(1-f);
q store sum of complement of values of matrix f.

[p,q]
it generate array of values of p and q.

bar([0,1] , [p,q]/1000)
it generate this bar graph shown above in this post.

 

Interpolation of Curve

>> a=[1 2 3 4 5];
>> b=[1 4 9 16 25];

>> polyfit(a,b,3) 
ans =

   -0.0000    1.0000   -0.0000    0.0000 


This program is use to find equation from given values of x and y points.
Here b = x2  .
ans give us 0x3 + 1x2 + 0x1 + 0x0 
you change highest  



 clc;
n=input('No of Points: ')
X=rand(n,1);
Y=rand(n,1);
figure('color','white');
hold all
axis square
x1=X-0.5;
y1=Y-0.5;
r=x1.^2+y1.^2;
m=0;
for i=1:n
    if(r(i)<0.25)
        m=m+1;
        plot(X(i),Y(i),'b.');
    else
        plot(X(i),Y(i),'r.');
    end
end
(y*m)/n


Output on giving 10000 as value of n.
 
 

Popular posts from this blog

Shutdown Pc

Ellipse using OpenGl

String Comparisons