MATLAB tutorial for Begineers Part 3

click here for Part 2                     click here for Part 4

Working With MATLAB

Type the following following commands in the Command Window, and observe(verify) their corresponding results

 12

ans =

    12

% commenting operator in matlab
%12

a=23

a =

    23

a=23; %; is a output suppressor operator

who

Your variables are:

a    ans

whos
  Name      Size            Bytes  Class     Attributes

  a         1x1                 8  double            
  ans       1x1                 8  double            


who---- This command lists the variables in workspace.

whos----This command details the variables in workspace.


12+23

ans =

    35

12-23

ans =

   -11

mul=12*23

mul =

   276

div=12/23;
; is the output suppressing operator

who

Your variables are:

a    ans  div  mul

div

div =

    0.5217

1/2

ans =

    0.5000

2/1

ans =

     2

1/2

ans =

    0.5000

1\2

ans =

     2

%   \ is the inverted division operator a\b=b/a
2/3

ans =

    0.6667

2\3

ans =

    1.5000

3/2

ans =

    1.5000

% exponential representation

1e3

ans =

        1000
% e and E holds fine

1E3

ans =

        1000
% But, matlab is case-sensitive

a=exp(2)

a =

    7.3891

A=exp(1)

A =

    2.7183
who
a   A

% lookfor ----This command is used for command search
% Syntax: lookfor keywork_to_search

% we know the relation between exponential and logarithm

q=exp(12)

q =

   1.6275e+05

log(q)

ans =

    12

% = assignment operator
% == equivalence operator, tests equality between variables on both sides
% == results 1, if both are equal; 0, if they are not equal
1==2

ans =

     0

1==1

ans =

     1

a=exp(12),b=log(a)

a =

   1.6275e+05


b =

    12

log(exp(12))

ans =

    12
exp(log(12))

ans =

    12

% log ---command to compute natural logarithm
% log10---command to compute base 10 logarithm
exp(log10(12))

ans =

    2.9423

log10(exp(12))==log(exp(12))

ans =

     0

% both are not equal

log10(10^12)==10^(log10(12))
ans=
   
     1
%both are equal

%trigonometric computations
sin(90)

ans =

    0.8940

pi

ans =

    3.1416

22/7

ans =

    3.1429

sin(pi/2)

ans =

     1

sin(pi/2)==sin(90)

ans =

     0

sin(0)

ans =

     0

asin(sin(pi/2))

ans =

    1.5708

asin(sin(90))

ans =

    1.1062

sin(5)

ans =

   -0.9589

% Matlab’s trig functions are permanently set to radians mode

% The up-arrow key will display previous commands.
% And when you back up to a previous command that you like, hit Enter and it will execute.
% Or you can edit it when you get to it (use , , and Del), then execute it. Try doing this
% Now to re-execute the commands you have already typed.
diary off

clear all %to clear the workspace. Execute and observe the workspace window

close all % closes safely all the windows opened, except main window and the editor window

clc%clears the command window. Execute it and observe the command window

who
% As "clear all" command clears all the variables; so "who" can't display any variable.

% who lists active variables

% whos----lists active variables and their sizes

% what-------lists .m files available in the current directory


% Numerical Accuracy in MATLAB
225/331

ans =

    0.6798

format long e
225/331

ans =

     6.797583081570997e-01

format short% (the default format)
225/331

ans =

    0.6798

format long
225/331

ans =

   0.679758308157100

format short e
%e stands for exponential notation
225/331

ans =

   6.7976e-01

format bank
225/331

ans =

    0.67

format short% (the default format)

225/331

ans =

    0.6798


pi

ans =

    3.1416

who

Your variables are:

ans

pi=2%can be done

pi =

     2

who

Your variables are:

ans  pi 

pi

pi =

     2

% please don't do so. "pi" was assigned to 3.1416, by default
pi=3.1416

pi =

    3.1416

clear all
pi

ans =

    3.1416

power(2,5)
ans =

    32

power(10,2)

ans =

   100

pow2(2)

ans =

     4

%pow2(2) means 2^2
%X = pow2(Y) for each element of Y is 2 raised to the power Y
%  X = pow2(F,E) for each element of the real array F and a integer
%    array E computes X = F .* (2 .^ E).

X = pow2(Y) for each element of Y is 2 raised to the power Y

sqrt(25)

ans =

     5

% sqrt---to calculate the squarate


% working with complex numbers
a=2+2j

a =

   2.0000 + 2.0000i

b=3+5i

b =

   3.0000 + 5.0000i

% either "i" or "j" can be used to represent the imaginary number
diary off
2j

ans =

        0 + 2.0000i

2i

ans =

        0 + 2.0000i

%but, the notation "i2" or "j2" is forbidden
i2
{ Undefined function or variable 'i2'.
}
j2
{ Undefined function or variable 'j2'.
}
% abs----to calculate the absolute value of a complex number
who

Your variables are:

a    ans  b  

w=3;
abs(w)

ans =

     3
% abs(real_number)=real_number

% abs(imaginary_number)=real_number
abs(3j)

ans =

     3

abs(3.33j)

ans =

    3.3300

a

a =

   2.0000 + 2.0000i

abs(a)

ans =

    2.8284

%abs(m+nj)=sqrt(power(m,2)+power(n,2))
sqrt(power(2,2)+power(2,2))

ans =

    2.8284

z1=1+2i

z1 =

   1.0000 + 2.0000i

%% or you can multiply by i, like this
z1=1+2*i

z1 =

   1.0000 + 2.0000i

z2=2-3i

z2 =

   2.0000 - 3.0000i

% add and subtract
addition=z1+z2

addition =

   3.0000 - 1.0000i

subtraction=z1-z2

subtraction =

  -1.0000 + 5.0000i

% multiply and divide
multiply=z1*z2

multiply =

   8.0000 + 1.0000i

division=z1/z2

division =

  -0.3077 + 0.5385i

d1=z1\z2

d1 =

  -0.8000 - 1.4000i

z=3+4i

z =

   3.0000 + 4.0000i

real(z)

ans =

     3

imag(z)

ans =

     4

conj(z)

ans =

   3.0000 - 4.0000i

abs(z)

ans =

     5

angle(z)

ans =

    0.9273

diary off
%angle(m+nj)=atan(b/a)
z

z =

   3.0000 + 4.0000i

atan(4/3)==angle(3+4j)

ans =

     1

%Here, 1 means both are equal
%Euler’s famous formula e
%exp(xi)= cos x + i sin x
exp(i*pi/4)

ans =

   0.7071 + 0.7071i

%% Housekeeping Functions
% ceil(x)---the nearest integer to x looking toward +
% close 3--- closes figure window 3
% fix(x)---- the nearest integer to x looking toward zero
% fliplr(A)------ flip a matrix A, left for right
% flipud(A)-----flip a matrix A, up for down
% floor(x)------- the nearest integer to x looking toward -
% length(a)------the number of elements in a vector
% mod(x,y)-----the integer remainder of x/y; see online help if x or y are negative
% rem(x,y)------the integer remainder of x/y; see online help if x or y are negative
% rot90(A)------rotate a matrix A by 90
% round(x)------the nearest integer to x
% sign(x)--------the sign of x and returns 0 if x=0
% size(c)---------the dimensions of a matrix

Popular posts from this blog

List of Websites to get free e-books

Online IDEs/Interpreters for python programming