0% found this document useful (0 votes)
35 views

Matlab Basics

MATLAB allows users to define variables, perform arithmetic operations, and define matrices and vectors. It supports common operators, predefined functions, and treats even scalars as matrices. Matrices can be generated using commands like zeros, ones, and eye and operations can be performed element-by-element using a dot before the operator.

Uploaded by

Vivek Vilvaraj
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Matlab Basics

MATLAB allows users to define variables, perform arithmetic operations, and define matrices and vectors. It supports common operators, predefined functions, and treats even scalars as matrices. Matrices can be generated using commands like zeros, ones, and eye and operations can be performed element-by-element using a dot before the operator.

Uploaded by

Vivek Vilvaraj
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

MATLAB

basics

How to start and finish


MATLAB is started by clicking the mouse on the appropriate icon and is ended by typing exit or by using the menu option. After each MATLAB command, the "return" or "enter" key must be depressed.

Definition of Variables
Variables are assigned numerical values by typing the expression directly Typing a = 1+2 yields: a = 3 The answer will not be displayed when a semicolon is put at the end of an expression. Type a = 1+2;

operators
MATLAB utilizes the following arithmetic operators: + addition - subtraction * multiplication / division ^ power operator ' transpose

Assigning values
A variable can be assigned using a formula that utilizes these operators and either numbers or previously defined variables. For example, since `a was defined previously, the following expression is valid b = 2*a;

To determine the value of a previously defined quantity, type the quantity by itself: b yields: b = 6 If your expression does not fit on one line, use an ellipsis (three or more periods at the end of the line) and continue on the next line. c = 1+2+3+... 5+6+7;

Pre-defined variables
There are several predefined variables which can be used at any time, in the same manner as user defined variables: i sqrt(-1) j sqrt(-1) pi 3.1416... For example, y= 2*(1+4*j); yields: y= 2.0000 + 8.0000i

There are also a number of predefined functions that can be used when defining a variable. Some common functions that are used are: abs magnitude of a number (absolute value for real numbers) angle angle of a complex number, in radians cos cosine function, assumes argument is in radians sin sine function, assumes argument is in radians exp exponential function

For example, with y defined as above, c = abs(y) yields: c = 8.2462 c = angle(y) yields: c = 1.3258 With a=3 as defined previously, c = cos(a) yields: c = -0.9900 c = exp(a) yields: c = 20.0855

Definition of Matrices
MATLAB is based on matrix and vector algebra; even scalars are treated as 1x1 matrices. Therefore,vector and matrix operations are as simple as common calculator operations. Vectors can be defined in two ways. The first method is used for arbitrary elements: v = [1 3 5 7]; creates a 1x4 vector with elements 1, 3, 5 and 7.

Additional elements can be added to the vector: v(5) = 8; yields the vector v = [1 3 5 7 8]. Previously defined vectors can be used to define a new vector. For example, with v defined above: a = [9 10]; b = [v a]; creates the vector b = [1 3 5 7 8 9 10].

The second method is used for creating vectors with equally spaced elements: t = 0:.1:10; creates a 1x101 vector with the elements 0, .1, .2, .3,...,10. the middle number defines the increment. If only two numbers are given, then the increment is set to a default of 1: k = 0:10; creates a 1x11 vector with the elements 0, 1, 2, ..., 10.

Defining matrices
Matrices are defined by entering the elements row by row: M = [1 2 4; 3 6 8]; creates the matrix A particular element of a matrix can be assigned: M(1,2) = 5; places the number 5 in the first row, second column

Types of matrices
There are a number of special matrices that can be defined: null matrix: M = []; nxm matrix of zeros: M = zeros(n,m); nxm matrix of ones: M = ones(n,m); nxn identity matrix: M = eye(n);

operations
Operations and functions that were defined for scalars in the previous section can also be used on vectors and matrices. a = [1 2 3]; b = [4 5 6]; c = a + b yields: c = 5 7 9 Functions are applied element by element. t = 0:10; x = cos(2*t); creates a vector x with elements equal to cos(2t) for t = 0, 1, 2, ..., 10.

Operations that need to be performed element-by-element can be accomplished by preceding the operation by a ". For example, to obtain a vector x that contains the elements of x(t) = tcos(t) at specific points in time, you cannot simply multiply the vector t with the vector cos(t). Instead you multiply their elements together t = 0:10; x = t.*cos(t);

Some aditiona commands to generate matrices

You might also like