Posts

Showing posts from February, 2015

Eval 2

Image
x=[1 2 3 4 5]; y=[2 9 28 65 126]; polyfit(x,y,3) hold all a=linspace(1,5,100); p=[1 0 0 1 ]; b=polyval(p,a); plot(x,y,'r') plot(a,b,'b')

Monte Carlo Approch In MATLAB

Image
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 = x 2   . ans give us 0x 3 + 1x 2 + 0x 1  + 0x 0   you chan

Gauss Elimination

Gauss Elimination : means elimination of variable. Let there are three equation and we have to find values of x, y, z x+y+z=0 x-2y+2z=4 x+2y-z=2 How we can find values of x,y,z by using pen and paper? x+y+z=0 -(I) x-2y+2z=4 -(II) x+2y-z=2 -(III) substract II-I and III-I we get : -  x+y+z=0 -(IV) -3y+2z=4 -(V)     2y-z=2 -(VI) Now we have to elimnate y multiply 3 with III and 2 with II and subsutract III-II we get : - x+y+z=0 -(VII)  -3y+z=4 -(VIII)      -5z=10   -(IX) from IX  we get : - z = -2 Put z=-2 in VII and VIII we get : - x+y-2=0 -(X) -3y-2=4 from this we get : - y=-2 and put y in X We get : - x=4 Now how can done this process using MATLAB:  Here is code, paste it in Editor Window of MATLAB function x = gauss(A,b) [n,n]=size(A); [n,k]=size(b); x=zeros(n,k); for i=1:n-1     m=-A(i+1:n,i)/A(i,i);     A(i+1:n,:)=A(i+1:n,:) + m*A(i,:);     b(i+1:n,:)=b(i+1:n,:) + m*b(i,:); end x(n,:)=b(n,:)/A(n,