Matlab is capable of much more than 2+2. It can write powerful scripts that can compute large amount of data.

  1. Simple Script– Create a new matlab script (.m) file. Press CTRL
  2. for i=1:10 % doesn't not necessarily have to be i. we just assigning it that so that. This basically repeats whatever we write next (10) time
      disp ('it works') % displays it works for (10) times
    end % for loop always have an end so it can start again and assign i=2 and do the loop for the 2nd time
    cd % displays your working directory 

    now save it by clicking the play button in the “editor” window.
    assign is a name and see the results in the command window.
    OR save it using the save button and run is from the command window by pressing

    run ('x')  % x is your filename
  3. Dice Throw– Create a new matlab script (.m) file. Press CTRL. Simulates the throw of two dice using the rand() function and returns the individual and combined score
  4. %%"dice throw" jignesh Kerai. 29/01/09. Ver 1.0
    %displays sum of two dice throws.
    %numbers are random
    disp ('----------------------Throwing Two Dice-------------------')
    a=ceil(6*rand); % rand gives random decimal point values between 1 & 0
    b=ceil(6*rand); % ceil rounds of the 6*rand to give simulate 6 possible outputs (1-6)
    total= (a+b);
    disp ('First Dice'), disp(a); % displays First Dice and the value of (a)
    disp ('Second Dice'), disp (b);

    Now if you wanted to tell the script to double the score if both dice have the same number (i.e a=b)

    if(a==b) %placing and condition to do something if a=b. Note the double ==.
      disp('Double Score'),disp(total*2) % displays double score and 2*total value.
      else %otherwise if a is NOT = b
      disp ('Total Score'), disp(total)
      disp('##########################################################') %for fun
    end %END
  5. Real Draught Problem- Calculating the stability of a cuboid immersed in a fluid
  6. %%"Draught of a Floating" Jignesh Kerai 29/01/09
    %Determines whether a cuboid will float or not
    l=input ('what is the lenght of the cuboid (metres)?'); ';' suppresses output, i.e the output of the calculation will not be displayed in the command window
    w=input ('what is the width of the cuboid (metres)?');
    h=input ('what is the height of the cuboid (metres)?');
    pc=input ('what is the density of the cuboid (kg/m^3)?');
    pfluid=input('what is the density of the fluid (kg/m^3)?');
    if (pc>pfluid);
      disp('Cuboid will drown'); % if density of cuboid> fluid's density then it warns the user.
      elseif (h>l||h>w);  %elseif has to be used if you want another "if" statement within the original if loop
      disp ('Cuboid unstable') % if the height> length OR (|| height > width then disp()
      elseif (h<l||h<w);
      disp ('Cuboid is stable')
    V= (l*w*h); % calculate volume
    disp('draught='), disp(d)
  7. Play Rock, Paper Scissor with the Computer
  8. %%"dice throw" jignesh Kerai. 29/01/09. Ver 1.0
    %Play Rock, Paper Scissor with the Computer
    a= input ('choose ROCK(1), PAPER(2) or SCISSOR(3)'); % user input of what they choose
    b= ceil(3*rand); creates random numbers from 1 TO 3
    if (a==b) %Condition
        disp ('ITS A DRAW')
        elseif (a==1 && b==2) % elseif if used within an if statement to say IF again if the first if was not satisfied. && is the AND function. (i.e if a=1 AND b=2 the disp (...))
        disp ('The computer choose PAPER and you choose ROCK. PAPER covers ROCK. You LOOSE')
        elseif (a==2 && b==1)
        disp ('The computer choose ROCK and you choose PAPER. PAPER covers ROCK. You WIN')
        elseif (a==3 && b==1)
        disp ('The computer choose ROCK and you choose SCISSOR. ROCK breaks SCISSORS. You LOOSE')
        elseif (a==1 && b==3)
        disp ('The computer choose SCISSORS and you choose ROCK. ROCK breaks SCISSORS. You WIN')
    a=input ('do u want to play again. YES(1), NO(2)?'); % user input
    if a==1;
        run 'C:\%location of file\FILENAME.m%' % Allows user to play again with the computer. if unsure what directory you have saved the file. use the CD command.
    else disp ('GoodBYE')
  9. Simple counting loop with FOR
  10. %%Jignesh
    %simple Counting loop
    a=input('enter digit to start counting from'); %starting value you want to count from
    b=input ('enter digit to stop counting'); % end
    disp ('---')
    for i=a:b
      m(i)=i; % assigns first row, first element
  11. Calculate Factorial of a Number-FOR
  12. a=input ('Value of n! enter your value of n') % input of last number
    f=1 %have to set value of first f
    for i=1:a %number of time whatever you write under for has to be executed
        f=f*i % first time, i=1, f=1, therefore new f=1.
        %2nd time, i=2, f=1, therefore new f=2
        %3rd time, i=3, f=2, therefore new f=6...etc
    disp (f)
  13. Calculate Factorial of a Number-WHILE
  14. %%computing factorial using while loop.Jignesh%start
    n=input ('value of number to calculate factorial of') %last number
    i=1 % set value of i
    a=1% set value of a
    while i<=n % if i less than or equal to (user input) do the following
         a=a*i % first time a=1, i=1, new a =1.
         i=1+i % have to change value of i (same as for loop that automatically updates the value of i)
    end % so when it gets here, it will start the while statement again, i<=n.
        %2nd step i=2 and a=2.
        %3rd step i=3 and a=2*3= 6.
        %it will keep running till i=n.
    disp (a)
  15. Checking if input is a Prime number
  16. %%computing factorial using while loop.Jignesh
    N=input('put the number you would like to check')
     flag = 0 %add a variable called flag which assumes N is a prime
    for i=2:N-1 % for every number from 2 to n-1 divide N by each number
    x=mod(N,i);% check to see if you can divide N by i cleanly (to give an integer)
               % if it is possible to divide cleanly then x will = 0
               % if x=0 then you can divide N cleanly by another number. Hence it is not a prime
       if (x==0) % Make flag 1 to tell the rest of the code that this number is not a prime
          flag = 1
    end;%after trying divide n by every number if the flag = 0 then n is a prime
        %otherwise n is not a prime
    if (flag==1)
       disp('this number is not a prime')
       disp(' this number is a prime')
  17. Listing all prime numbers
  18. n=1
    while (n>0); %while n is greater than 0 run the commands below
      while (isprime(n)==1) %isprime is an predefined matlab script. type help isprime or doc isprime to see how it works. alternatively i you can also see the isprime script by typing edit isprime
      break; % stops the while loops. i.e it runs it once only to check if n is prime or not
    n=n+1;% increases the value of n by 1 and tests if that number is prime
  19. Creating multiplication tables
  20. %%create Multiplication tables for the products of all integers from
    %%1:10.and display in a 2-d array
    for i=1:10;
        for j=1:10
    disp (table)
  21. Calculating the value of pi
  22. %%Calculating pi
    N=input ('Value of N?'); %number of iterative processes
    count = 0;
    for i=1:N
         if ( (x^2 + y^2)<1 )
            count= count + 1;