Posts

Showing posts with the label MATLAB

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. H...

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(...

A MATLAB Program To Generate A Unit Step Signal

Image
%unit step signal generation %n denote the range of input %y here is output n=[-10:10] y=ones(1,21) y=zeros(1,10) y=ones(1,11) y=[zeros(1,10) ones(1,11)] stem(n,y) IN the above created program in MATLAB editor ,to generate the unit step sequence i first specified the range of n.Then the output y is obtained by appending the 2 vectors which are zeros(1,10) and ones(1,11). By using the stem(n,y),the each of output value is plotted against each value of input range specified as a vector that is stored in n.

Solution for Practical Programs

Image
A = size(3x3) with range [7-11] B = identity matrix of same size as A. Perform operations i) Addition ii) Subtraction iii) Multiplication iv) Element Multiplication. Solution. Click Here. Divide screen as shown in fig given below.                 Plot in screen 3 => y = x3 + 5 Plot in screen 1 => y = cos(x) in range (0,4*pi) with 100 points in blue color with dotted line. Solution. Click Here. Create a semicircle of radius 8 centered at (3 4) with red lines. Solution. Click Here. P2(Given x=[1 2 3 4 5 ] y=[2 9 28 65 126] find equation of curve which fits best. compare graphically) Solution. Click Here.              

Solution:

Image
theta=linspace(0,pi,100);   //simple pi for semi circle r=8;                                    //radius = 8 x=r*cos(theta);                 x=x+3;                               //for center at x=3. y=r*sin(theta); y=y+4;                              //for center at y=4. plot(x,y,'r');                      //'r' is to change color into red. Run. Output :

Solution

Image
x=linspace(0,50,100); p=[1 0 0 5]; y=polyval(p,x); subplot(2,2,4); plot(x,y); x1=linspace(0,4*pi,100); z=cos(x1); subplot(1,2,1); plot(x1,z,'b.'); Run it. Output :

Solution

a=rand(3)            //to create random matrix of range 0 to 1 and of size 3x3 a=a*4                 //for range; a=a+7 a=round(a)         // to convert elements into int. b=eye(3)            // to create identity matrix addi=a+b           // addition sub=a-b             //subtraction mul1=a*b         //multiplication mul2=a.*b       //corresponding elements multiplication Output : //for a=rand(3) a =     0.7922    0.0357    0.6787     0.9595    0.8491    0.7577     0.6557    0.9340    0.7431 //for a=a*4; a =     3.1688    0.1428    2.7149     3.8380    3.3965    3.0310     2.6230    3.7360 ...

P2 : Plot Graph

Image
linspace Cmd : >> x=linspace(0,10,100); linspace(a,b,n); where a = start point            b = final point            n = number of point you want to divide range of a and b. Example : >> x=linspace(0,10,10)                        x =                               0    1.1111    2.2222    3.3333    4.4444    5.5556    6.6667    7.7778    8.8889                                         10.0000 Plot Circle Using MATLAB : >> theta=linspace(0,10,100); >>r=10; >> y=r*sin(theta); >> x=r*cos(theta); >> plot(x,y) Output will be :  Now...

P1 : MATLAB Basics

To declare a variable. a=10 a =     10 Note : If you will put ; (semi colons) the you will not output of that variable  or command. As seen blow : >> a=10; >> b=30; Addition of two variable. >> c=a+b c =     40 Subtraction of two variable. >> c=a-b c =    -20 Multiplication of two numbers. >> c=-1*c c =     20 Similarly you can do divide operation using / symbol between two variables. Command for writing 1 to 10 >> a=1:10 a =      1     2     3     4     5     6     7     8     9    10 Note : By putting : colons you can  start from a number and count to another number. Here I have use 1 as starting and 10 as ending.  Command for writing or storing vector as counting and givi...