Thursday 1 June 2017

How to Use MATLAB A Brief Introduction

                                   Some Useful Commands:

  • help                         % list all the topics
  • clear                        % remove all the data in current session
  • ; (semicolon)          % prevent commands from outputing results
  • % (percent sign)     % comments line
  • clc                           % clears the screen

Vectors:

A row vector in MATLAB can be created by an explicit list, starting with a left bracket,
entering the values separated by spaces (or commas) and closing the vector with a right
bracket.
  • A column vector can be created the same way, and the rows are separated by semicolons.
  • Example:
>> x = [ 0 0.25*pi 0.5*pi 0.75*pi pi ]
x = x is a row vector.
0 0.7854 1.5708 2.3562 3.1416
>> y = [ 0; 0.25*pi; 0.5*pi; 0.75*pi; pi ]
y = y is a column vector.
0
0.7854
1.5708
2.3562
3.1416
  • Vector Addressing – A vector element is addressed in MATLAB with an integer
index enclosed in parentheses.
  • Example:
>> x(3)
ans =
1.5708 ( 3rd element of vector x)
  • The colon notation may be used to address a block of elements.
(start : increment : end)
start is the starting index, increment is the amount to add to each successive index, and end
is the ending index. A shortened format (start : end) may be used if increment is 1.
  • Example:
  • >> x(1:3)
  • ans =
  • 0 0.7854 1.5708  (1st to 3rd elements of vector x)
  • NOTE: MATLAB index starts at 1. 

                                                    Some useful commands:

  • x = start:end                                 create row vector x starting with start, counting by one,                                                                ending at end
  • x = start:increment:end                create row vector x starting with start, counting by increment,                                                      ending at or before end
  • linspace(start,end,number)           create row vector x starting with start, ending at end, having                                                         number elements
  • length(x)                                       returns the length of vector x
  • y = x’                                            transpose of vector x
  • dot (x, y)                                       returns the scalar dot product of the vector x and y.

                                               Array Operations

  • Scalar-Array Mathematics
For addition, subtraction, multiplication, and division of an array by a
scalar simply apply the operations to all elements of the array.
  • Example:
>> f = [ 1 2; 3 4]
f =
1 2
3 4
>> g = 2*f – 1
g =
1 3
5 7
Each element in the array f is
multiplied by 2, then subtracted
by 1
  • Element-by-Element Array-Array Mathematics.

Operation                              Algebraic Form                                MATLAB

Addition                                      a + b                                                  a + b
Subtraction                                  a – b                                                  a – b
Multiplication                              a x b                                                  a .* b
Division                                       a ÷ b                                                  a ./ b
Exponentiation                            a^b                                                     a .^ b








0 comments:

Post a Comment