[go: up one dir, main page]

0% found this document useful (0 votes)
38 views12 pages

Comando S Dem at Lab

This document summarizes various commands and functions available in MATLAB. It describes: 1) Basic arithmetic operations like addition, subtraction, multiplication, and division. 2) Formatting commands that control the display of output values, such as fixing the number of decimal places or using scientific notation. 3) Common math functions for tasks like calculating exponents, logarithms, trigonometric functions, and more. 4) How to perform operations on complex numbers and predefined variables in MATLAB.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views12 pages

Comando S Dem at Lab

This document summarizes various commands and functions available in MATLAB. It describes: 1) Basic arithmetic operations like addition, subtraction, multiplication, and division. 2) Formatting commands that control the display of output values, such as fixing the number of decimal places or using scientific notation. 3) Common math functions for tasks like calculating exponents, logarithms, trigonometric functions, and more. 4) How to perform operations on complex numbers and predefined variables in MATLAB.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Comandos de MATLAB

ARITHMETIC OPERATIONS
Arithmetic operation Symbol Example
Addition + 6+3=9
Subtraction 63=3
Multiplication * 6 * 3 = 18
Right division / 6/3 = 2
Left division \ 6\3 = 3/6 = 1/2
Exponentiation ^ 6 ^ 3 = 63 = 216

DISPLAY FORMATS

Command Description Example


Fixed-point with 4 >> 351/7
format short
decimal digits ans = 50.1429
Fixed-point with 14 >> 351/7
format long decimal digits ans = 50.14285714285715
Scientific notation with 4 >> 351/7
format short e
decimal digits ans = 5.0143e + 001
Scientific notation with 15 >> 351/7
format long e
decimal digits ans = 5.014285714285715e001
Best of 5 digit fixed or >> 351/7
format short g
floating point ans = 50.143
Best of 15 digit fixed >> 351/7
format long g
or floating point ans = 50.1428571428571
>> 351/7
format bank Two decimal digits
ans = 50.14
Eliminates empty lines to allow more lines with information
format compact
displayed on the screen
format loose Adds empty lines (opposite of compact)

ELEMENTARY MATH BUILT-IN FUNCTIONS


Common math functions Exponential functions

Function Description Function Description


abs(x) Computes the absolute value of x. exp(x) Exponential (e x)
sqrt(x) Computes the square root of x. log(x) Natural logarithm
round(x) Rounds x to the nearest integer. log10(x) Base 10 logarithm
fix(x) Rounds (or truncates) x to the nearest integer toward 0. sqrt(x) Square root
floor(x) Rounds x to the nearest integer toward .
ceil(x) Rounds x to the nearest integer toward .
sign(x) Returns a value of 1 if x is less than 0, a value of 0 if x equals 0,
and a value of 1 otherwise.
rem(x,y) Returns the remainder of x/y. for example, rem(25, 4) is 1, and
rem(100, 21) is 16. This function is also called a modulus function.
exp(x) Computes e x, where e is the base for natural logarithms, or
approximately 2.718282.
log(x) Computes ln x, the natural logarithm of x to the base e.
log10(x) Computes log10 x, the common logarithm of x to the base 10.
Round-off functions
Function Description Example
>> round(20/6)
round(x) Round to the nearest integer
ans = 3
>> fix(13/6)
fix(x) Round towards zero
ans = 2
>> ceil(13/5)
ceil(x) Round towards infinity
ans = 3
>> floor(10/4)
floor(x) Round towards minus infinity
ans = 3
>> rem(14,3)
rem(x,y) Returns the remainder after x is divided by y
ans = 2
Signum function. Returns 1 if x > 0, 1 if x < 0, >> sign(7)
sign(x,y)
and 0 if x = 0. ans = 1

Trigonometric and hyperbolic functions

Function Description
sin(x) Computes the sine of x, where x is in radians.
cos(x) Computes the cosine of x, where x is in radians.
tan(x) Computes the tangent of x, where x is in radians.
asin(x) Computes the arcsine or inverse sine of x, where x must be between 1 and 1. The
function returns an angle in radians between /2 and /2.

acos(x) Computes the arccosine or inverse cosine of x, where x must be between


1 and 1. The function returns an angle in radians between 0 and . Computes
the arctangent or inverse tangent of x. The function returns an angle in
radians between /2 and /2.
atan(x)
Computes the arctangent or inverse tangent of the value y/x. The function
returns an angle in radians that will be between and , depending on the
signs of x and y.
atan2(y,x) ex ex
Computes the hyperbolic sine of x, which is equal to
2
sinh(x) ex +ex
Computes the hyperbolic cosine of x, which is equal to
2

sinh x
cosh(x) Computes the hyperbolic tangent of x, which is equal to
cosh x

tanh(x)
Computes the inverse hyperbolic sine of x, which is equal to ln( + 2 + 1)

asinh(x)
Computes the inverse hyperbolic cosine of x, which is equal to ln( + 2 1)

1+
acosh(x) Computes the inverse hyperbolic tangent of x, which is equal to ln
1

Complex number functions

Function Description
conj(x) Computes the complex conjugate of the complex number x. Thus, if
x is equal to a + ib, then conj(x) will be equal to a ib.
angle(x) Computes the real portion of the complex number x.
real(x) Computes the imaginary portion of the complex number x.
imag(x) Computes the absolute value of magnitude of the complex number x.
abs(x) Computes the angle using the value of atan2(imag(x), real(x)); thus,
the angle value is between and .
Arithmetic operations with complex numbers

Operation Result
1 + 2 (1 + 2) + (1 + 2)

1 + 2 (1 2) + (1 2)

1 2 (12 12) + (12 21)

1 1 2 + 12 2 1 21
2 +
2 2 2
+ 2 22 + 22
|1| 12 + 12
1 1 1 (conjugate of c1)
(Assume that 1 = 1 + 1 and 2 = 2 + 2.)

PREDEFINED VARIABLES

Predefined variable Description


in MATLAB
ans Represents a value computed by an expression but not
stored in variable name.
pi Represents the number .
eps Represents the floating-point precision for the computer
being used. This is the smallest difference between two
numbers.
inf Represents infinity which for instance occurs as a result of a
division by zero. A warning message will be displayed or the
value will be printed as .
i Defined as 1, which is: 0 + 1.0000i.
j Same as i.
NaN Stands for Not a Number. Typically occurs as a result o f an
expression being undefined, as in the case of division of
zero by zero.
clock Represents the current time in a six-element row vector
containing year, month, day, hour, minute, and seconds.
date Represents the current date in a character string format.

COMMANDS FOR MANAGING VARIABLES

Command Description
clear Removes all variables from the memory.
clear x, y, z Clears/removes only variables x, y and z from the memory.
who Lists the variables currently in the workspace.
whos Displays a list of the variables currently in the memory and their
size together with information about their bytes and class.
GENERAL COMMANDS

Help Workspace information

Function Description Function Description


help Lists topics on which help is available. who Lists variables currently in the workspace.
helpwin Opens the interactive help window. whos Lists variables currently in the workspace with their size.
helpdesk Opens the web browser based help facility. what Lists m-, mat- and mex-files on the disk.
help Provides help on topic. clear Clears the workspace, all variables are removed.
topic Lists help topics containing string. clear x y z Clears only variables x, y, and z.
lookfor string Runs the demo program. clear all Clears all variables and functions from workspace.
demo mlock fun Locks function fun so that clear cannot remove it.
munlock Unlocks function fun so that clear can remove it.
fun clc Clears command window, command history is lost.
home Same as clc.
clf Clears figure window.
Directory information
Function Description
pwd Shows the current working directory.
cd Changes the current working directory.
dir Lists contents of the current directory.
ls path Lists contents of the current directory, same as dir.
editpat Gets or sets MATLAB search path.
h Modifies MATLAB search
copyfile path. Copies a file.
mkdir Creates a directory.

General information

Function Description
compute Tells you the computer type you are using.
r clock Gives you wall clock time and date as a vector.
date Tells you the date as a string.
more Controls the paged output according to the screen size.
ver Gives the license and the version information about MATLAB installed on your computer.
bench Benchmarks your computer on running MATLAB compared to other computers.

Termination

Function Description
c (Control-c) Local abort, kills the current command execution.
quit Quits MATLAB.
exit Same as quit.
ARRAYS
Colon use for a matrix
Command Description
(: , ) Refers to the elements in all the rows of a column n of the matrix A.
(, ) Refers to the elements in all the columns of row n of the matrix A. Refers to the
(: , : ) elements in all the rows between columns m and n of the matrix A.
(: , ) Refers to the elements in all the columns between rows m and n of the matrix A.

(: , : ) Refers to the elements in rows m through n and columns p through


q of the matrix A.

Built-in functions for handling arrays

Function Description Example


>> A = [5 9 2 4];
Returns the number of >> length(A)
length(A)
elements in the vector A. ans = 4
>>A = [2 3 0 8 11 ; 6 17 5 7 1]
A=
Returns a row vector [m, n],
2 3 0 8 11
size(A) where m and n are the size
6 17 5 7 1
m n of the array A.
>> size(A)
ans = 2 5
>> A = [3 1 4 ; 9 0 7]
A=
Rearrange a matrix A that 314
has r rows and s columns to 907
reshape(A, m, n) have m rows and n columns. >> B = reshape(A, 3, 2)
r times s must be equal to m B=
times n. 3 0
9 4
1 7
>> v = [3 2 1];
When v is a vector, creates a >> A = diag(v)
square matrix with the A=
diag(v) 3 0 0
elements of v in the
diagonal 0 2 0
0 0 1
>> A = [1 8 3 ; 4 2 6 ; 7 8 3]
A=
1 8 3
4 2 6
When A is a matrix, creates 7 8 3
diag(A) a vector from the diagonal >> vec = diag(A)
elements of A. vec =
1
2
3

Element-By-Element Operations
Arithmetic operators
Matrix operators Array operators
+ Addition + Addition
Subtraction Subtraction
* Multiplication .* Array multiplication
^ Exponentiation .^ Array exponentiation
/ Right division ./ Array right division
\ Left division .\ Array left division
MATLAB built-in array functions

Function Description Example


>> A = [3 7 2 16];
If A is a vector, returns the mean value of the >> mean(A)
mean(A)
elements ans = 7
>> A = [3 7 2 16 9 5 18 13 0 4];
If A is a vector, C is the largest element in A. If A >> C = max(A)
C = max(A) is a matrix, C is a row vector containing the C = 18
largest element of each column of A.

>> [d, n] = max(A)


If A is a vector, d is the largest element in A, n is d = 18
[d, n] = max(A) the position of the element (the first if several n=7
have the max value).

The same as max(A), but for the smallest >> A = [3 7 2 16];


min(A) element. >> min(A)
ans = 2
[d, n] = min(A) The same as [d, n] = max(A), but for the
smallest element.
>> A = [3 7 2 16];
If A is a vector, returns the sum of the elements >> sum(A)
sum(A)
of the vector. ans = 28
>> A = [3 7 2 16];
If A is a vector, arranges the elements of the >> sort(A)
sort(A)
vector in ascending order. ans = 2 3 7 16
>> A = [3 7 2 16];
If A is a vector, returns the median value of the >> median(A)
median(A)
elements of the vector. ans = 5
>> A = [3 7 2 16];
If A is a vector, returns the standard >> std(A)
std(A)
deviation of the elements of the vector. ans = 6.3770
>> A = [1 2 ; 3 4];
det(A) Returns the determinant of a square matrix A. >> det(A)
ans = 2
>> a = [5 6 7];
Calculates the scalar (dot) product of two vectors >> b = [4 3 2];
dot(a, b) a and b. The vector can each be row or column >> dot(a,b)
vectors. ans = 52
>> a = [5 6 7];
Calculates the cross product of two vectors a and b, >> b = [4 3 2];
cross(a, b) >> cross(a, b)
(a b). The two vectors must have 3 elements.
ans = 9 18 9
>> a = [1 2 3; 4 6 8; 1 2 3];
>> inv(A)
Ans =
Returns the inverse of a square matrix A.
inv(A) 0.5000 0.0000 0.5000
5.0000 1.5000 1.0000
3.5000 1.0000 0.5000
RANDOM NUMBERS GENERATION

The rand command


Comman Description Example
Generates a single random >> rand
rand ans = 0.9501
number between 0 and 1.
Generates an n elements row >> a = rand(1, 3)
rand(1, n) vector of random numbers a = 0.4565 0.0185 0.8214
between 0 and 1.
>> b = rand(3)
Generates an n n matrix b=
rand(n) with random numbers 0.7382 0.9355 0.8936
between 0 and 1. 0.1763 0.9165 0.0579
0.4057 0.4103 0.3529
>> c = rand(2, 3)
Generates an m n matrix
c=
rand(m, n) with random numbers
0.2028 0.6038 0.1988
between 0 and 1.
0.1987 0.2722 0.0153
Generates a row vector with >> randperm(7)
n elements that are random ans =
randperm(n) 5 2 4 7 1 6 3
permutation of integers 1
through n.

PROGRAMMING IN MATLAB

Relational operators Logical operators


Logical operator Name Description
Relational operator Interpretation
Operates on two operands (A and B). If both are
< &
Less than AND true, the result is true (1), otherwise the result is
Example: A&B
<= false (0).
Less than or equal
Operates on two operands (A and B). If either one,
> Greater than |
OR or both are true, the result is true (1), otherwise
Example: A|B
>= Greater than or equal (both are false) the result is false (0).
Equal Operates on one operand (A). Gives the opposite of
== ~
NOT the operand. True (1) if the operand is false, and
~= Not equal Example: ~A
false (0) if the operand is true.

Precedence Operation
1 (highest) Parentheses (If nested parentheses exist, inner have precedence).
2 Exponentiation.
3 Logical NOT (~).
4 Multiplication, Division.
5 Addition, Subtraction.
6 Relational operators (>, <, >=, <=, = =, ~=).
7 Logical AND (&).
8 (lowest) Logical OR (|).
Additional logical built-in functions Truth table

Function Description Example INPUT OUTPUT


AND OR XOR NOT NOT
>>xor(8, 1) A B
A&B A|B (A,B) ~A ~B
Exclusive or. Returns true (1) if one ans = 0
xor(a, b) false false false false false true true
operand is true and the other is false. >>xor(8, 0)
false true false true true true false
ans = 1
true false false true true false true
Returns 1 (true) if all elements in a >>A = [5 3 11 7 8 15] true true true true false false false
vector A are true (non-zero). Returns 0 >>all(A)
(false) if one or more elements are false ans = 1
all(A)
(zero). If A is a matrix, treats columns >>B = [3 6 11 4 0 13]
of A as vectors, returns a vector with >>all(B)
1s and 0s. ans = 0

Returns 1 (true) if any element in a >>A = [5 0 14 0 0 13]


vector A is true (non-zero). Returns 0 >>any(A)
(false) if all elements are false (zero). ans = 1
any(A)
If A is a matrix, treats columns of A as >>B = [0 0 0 0 0 0 ]
vectors, returns a vector with 1s and >>any(B)
0s. ans = 0

If A is a vector, returns the indices of >>A = [0 7 4 2 8 0 0 3 9]


find(A) the non-zero elements. >>find(A)
If A is a vector, returns the address of ans = 2 3 4 5 8 9
find(A>d ) the elements that are larger than d (any >>find(A > 4)
relational operator can be used). ans = 4 5 6

MATLAB while Structures

Command Description
break Terminates the execution of MATLAB for and while loops. In nested
loops, break will terminate only the innermost loop in which it is placed.

return Primarily used in MATLAB functions, return will cause a normal return
from a function from the point at which the return statement
is executed.

error (text) Terminates execution and displays the message contained in text
on the screen. Note, the text must be enclosed in single quotes.
Function Description
Relational A MATLAB logical relation is a comparison between two
operators variables x and y of the same size effected by one of the six
operators, <, <=, >, >=, = =, ~ =. The comparison involves
corresponding elements of x and y, and yields a matrix or scalar
of the same size with values of true or false for each of its
elements. In MATLAB, the value of false is zero, and true
has a value of one. Any non-zero quantity is interpreted as
true.

Combinatorial The operators & (AND) and | (OR) may be used to combine two
operators logical expressions.

all, any If x is a vector, all(x) returns a value of one; if all of the


elements of x are non-zero, and a value of zero otherwise. When
X is a matrix, all(X) returns a row vector of ones or zeros
obtained by applying all to each of the columns of X. The
function any operates similarly if any of the elements of x are
non-zero.

find If x is a vector, i = find(x) returns the indices of those elements of


x that are non-zero (i.e., true). Thus, replacing all the negative
elements of x by zero could be accomplished by
i = find(x < 0);
x(i) = zeros(size(i));
if, else, elseif If X is a matrix, [i, j] = find(X)operates similarly and returns
the row-column indices of non-zero elements.

Break

Return

error (text)

while
GRAPHICS

Color, line-style and marker-style options List of functions for plotting x-y data
Color style-option Line style-option Marker style-option Function Description
y yellow solid + plus sign area Creates a filled area plot.
m magenta dashed o circle bar Creates a bar graph.
c cyan : dotted * asterisk barh Creates a horizontal bar graph.
r red . dash-dot x x-mark comet Makes an animated 2-D plot.
g green . point compass Creates arrow graph for complex numbers.
b blue ^ up triangle contour Makes contour plots.
w white s square contourf Makes filled contour plots.
k black d diamond, etc. errorbar Plots a graph and puts error bars.
feather Makes a feather plot.
fill Draws filled polygons of specified color.
fplot Plots a function of a single variable.
hist Makes histograms.
loglog Creates plot with log scale on both x and y axes.
pareto Makes pareto plots.
pcolor Makes pseudo color plot of matrix.
pie Creates a pie chart.
plotyy Makes a double y-axis plot.
plotmatri Makes a scatter plot of a matrix.
x polar Plots curves in polar coordinates.
quiver Plots vector fields.
rose Makes angled histograms.
scatter Creates a scatter plot.
semilogx Makes semilog plot with log scale on the x-axis.
semilogy Makes semilog plot with log scale on the y-axis.
stairs Plots a stair graph.
stem Plots a stem graph.

Functions used for 3-D graphics


Command Description
plot3 Plots three-dimensional graph of the trajectory of a set of three parametric equations x(t),
y(t), and z(t) can be obtained using plot3(x,y,z).

mesh If x and y are two vectors containing a range of points for the evaluation of a function,
[X,Y] = meshgrid(x, y) returns two rectangular matrices containing the x and y values at each point of a two-
dimensional grid.
grid
If X and Y are rectangular arrays containing the values of the x and y coordinates at each point of a rectangular
grid , and if z is the value of a function evaluated at each of these points, mesh(X,Y,z) will produce a three-
mesh(X,Y,z) dimensional perspective graph of the points. The same results can be obtained with mesh(x,y,z) can also be
used.

If the xy grid is rectangular, these two functions are merely variations of the basic plotting program
mesh, and they operate in an identical fashion. meshc will produce a corresponding contour plot drawn on
meshc, meshz the xy plane below the three-dimensional figure, and meshz will add a vertical wall to the outside features of
the figures drawn by mesh.

Produces a three-dimensional perspective drawing. Its use is usually to draw surfaces, as opposed to plotting
surf functions, although the actual tasks are quite similar. The output of
surf will be a shaded figure. If row vectors of length n are by x = r cos and y = r sin , with 0 2, they
correspond to a circle of radius r. If G is a column vector r equal to r = [0 1 2]; then z = r*ones(size(x)) will
be a rectangular, 3 n, arrays of 0s and 2s, and surf(x, y, z) will produce a shaded surface bounded by three
circles; i.e., a cone.
SYMBOLIC MATHEMATICS
Simplification Basic operations
collect Collect common terms ccode C code representation of a symbolic expression
expand Expand polynomials and elementary functions conj Complex conjugate
factor Factorization findsym Determine symbolic variables
horner Nested polynomial representation fortran Fortran representation of a symbolic expression
numden Numerator and denominator imag Imaginary part of a complex number
simple Search for shortest form latex LaTeX representation of a symbolic expression
simplify Simplification pretty Pretty prints a symbolic expression
subexpr Rewrite in terms of subexprssions real Real part of an imaginary number
sym Create symbolic object
syms Shortcut for creating multiple symbolic objects

Arithmetic operations Symbolic expressions


+ Addition horner(S) Transposes S into its Horner, or nested,
Subtraction numden(S) representation. Returns two symbolic expressions
* Multiplication that r epresent, respectively, the numerator expression
.* Array multiplication and the denominator expression for the rational
/ Right division numeric(S) representation of S.
./ Array right division Converts S to a numeric form (S must not contain
\ Left division poly2sym(c) any symbolic variables).
.\ Array left division Converts a polynomial coefficient vector c to
^ Matrix or scalar raised to a power pretty(S) a symbolic polynomial.
.^ Array raised to a power Prints S in an output form that resembles
Complex conjugate transpose sym2poly(S) typeset mathematics.
. Real transpose symadd(A,B) Converts S to a polynomial coefficient vector.
symdiv(A,B) * Performs a symbolic addition, A + B .
symmul(A,B) Performs a symbolic division, A / B.
Performs a symbolic multiplication, A * B.
sympow(S,p)
Performs a symbolic power, S^p.
symsub(A,B) Performs a symbolic subtraction, A B.
Solution to Differential Equations

Solution of equations
compose Functional composition
dsolve Solution of differential equations
finverse Functional inverse
solve Solution of algebraic equations

Calculus
Calculus Pedagogical and graphical applications
diff Differentiate ezcontour Contour plotter
int Integrate ezcontourf Filled contour plotter
jacobian Jacobian matrix ezmesh Mesh plotter
ezmeshc Combined mesh and contour plotter
limit Limit of an expression
ezplot Function plotter
symsum Summation of series
ezplot Easy-to-use function plotter
taylor Taylor series expansion ezplot3 Three-dimensional curve plotter
ezpolar Polar coordinate plotter
Conversions ezsurf Surface plotter
char Convert symbolic object to string ezsurfc Combined surface and contour plotter
double Convert symbolic matrix to double funtool Function calculator
poly2sym Function calculator rsums Riemann sums
sym2poly Symbolic polynomial to coefficient vector taylortool Taylor series calculator

Linear algebra
Integral transforms
colspace Basis for column space
fourier Fourier transform det Determinant
ifourier Inverse Fourier transform diag Create or extract diagonals
ilaplace Inverse Laplace transform eig Eigenvalues and eigenvectors
expm Matrix exponential
iztrans Inverse Z-transform
inv Matrix inverse
laplace Laplace transform jordan Jordan canonical form
ztrans Z-transform null Basis for null space
poly Characteristic polynomial
rank Matrix rank
rref Reduced row echelon form
svd Singular value decomposition
tril Lower triangle
triu Upper triangle

CONTROL SYSTEMS

Model conversion functions


Function Purpose
C2d Continuous state-space to discrete state-space
residue Partial-fraction expansion
ss3tf State-space to transfer function
ss2zp State-space to zero-pole-gain
tf2ss Transfer function to state-space
tf2zp Transfer function to zero -pole-gain
zp2ss Zero-pole-gain to state-space
zp2tf Zero-pole-gain to transfer function

You might also like