Friday, March 10, 2017

                                       Chapter-2

CREATING ARRAYS AND MATHEMATICAL OPERATIONS WITH ARRAY
ONE DIMENSIONAL ARRAY:
Variable_name = [type vector elements or array elements]
ROW VECTOR :- row vectors are created by enclosing the set of elements in square bracket and putting space or comma between the elements.
For example :
COLUMN VECTOR :– These are created by enclosing the set of elements in square bracket and putting semicolon between the elements.
For example :


REFERENCING THE ELEMENTS OF VECTOR :-
a(i)refer to the  ith element of a vector.
a(:) – refer to the all elements of a vector.
a(i:j)- refer to the elements from i to j of vector a.
For example :



CREATING A VECTOR WITH CONSTANT SPACING BY SPECIFYING THE FIRST TERM, LAST TERM AND SPACING:
Syntax for creating a vector with constant spacing by specifying the first term, last term and spacing:


CREATING A VECTOR WITH CONSTANT SPACING BY SPECIFYING THE FIRST TERM, LAST TERM AND SPACING:-
Vector in which first element is x, last element is y and number of elements is n is created by using linspace command. Syntax for using linspace command:-
                                         Variable_name = linspace(x,y,n)


CREATING A TWO DIMENSIONAL ARRAY(MATRIX):-
Syntax for entering matrix
Variable_name = [first row elements; second row elements;--------------------; last row elements]
·         All the rows must have same number of elements.

                                                                                              
THE ZEROS, ONES AND EYE COMMAND:-
zeros(m,n)It creates a matrix with m rows and n columns in which all elements are zero.
ones(m,n)-It creates a matrix with m rows and n columns in which all elements are 1.
eye(n) -It creates a square matrix with n rows and n columns in which diagonal elements are 1 and non diagonal elements are 0.
REFERENCING THE ELEMENTS OF MATRIX:-
A(m,n)-refers to the element in row m and column n of matrix A.
A(:,n)-refers to the elements in all the rows of column n of matrix A.
A(:,m:n)-refers to the elements in all the rows between column m and n of matrix A.
A(m:n,:)-refers to the elements in all the columns between row m and n of matrix A.
A(m:n,p:q)-refers to the elements in rows m through n and column p through q of matrix A.


DELETING ELEMENTS- An element, or a range of elements, of an existing variable can be deleted by reassigning nothing to these elements. For example :-
For deleting  6th   element of a vector, command is
A(6) = []


BUILT – IN FUNCTIONS FOR HANDLING ARRAYS:-
   1 length (A)
·         Returns the number of elements in the vector A.
For example:- 



    2 size (A)
·         Returns a row vector containing two elements one indicating number of rows and another indicating number of column in matrix A.
For example:-


     3 reshape(A,m,n)
·         Rearrange a matrix A that has r rows and s columns to have m rows and n columns. r times s must be equal to m times n.
For example:-


     4 diag(A)
·         when A is a vector or one dimensional array. Above function creates a square matrix with the elements of A in the diagonal.
·         When A is a matrix, above function creates a vector from diagonal elements of A.
F    For example:


MATHEMATICAL OPERATIONS WITH ARRAY:-
ADDITION AND SUBTRACTION-
The operations +(addition) and –(subtraction) can be used with arrays of identical size(the same number of rows and column). For example:


MULTIPLICATION-

  • Multiplication operation * is executed by MATLAB according to the rules of linear algebra.
  •  If A and B are two matrices, the operation A*B can be carried out if the number of column in matrix A is equal to number of rows in matrix B.
  •  Two vectors can multiply each other only if both have the same number of elements and one is a row vector and other is column vector.

IN- BUILT FUNCTIONS IN MATLAB
METHOD FOR SOLVING PAIR OF LINEAR EQUATIONS IN MATLAB:-
Lets take example of following linear equations
                                                    x + 2y + 3z = 6
                                                   6x - 7y + 8z =  7
                                                     x + 3y + 5z = 6


AX = B (multiply both sides by A^-1)
X = (A^-1)*B
In matlab, last equation can be written as
X = A\B ------------------------------------------(First method or Gauss elimination method)
 or   X = inv(A)*B------------------------------(Second method)
There are two methods for solving system of linear equations. First (left division method is recommended, because in second method calculation of inverse may be less accurate, when large matrices are involved)
MATLAB COMMANDS FOR SOLVING SYSTEM OF LINEAR EQUATION
ELEMENT-BY- ELEMENT OPERATIONS
Element-by-element multiplication, division and exponentiation of two vectors or matrices can be done in MATLAB by using following symbols.
For example:














































































































No comments:

Post a Comment