Sie sind auf Seite 1von 6

What are we interested in?

Dr. Ruth Aguilar Ponce Fall 2013

Matlab Comand Line Command execution

M-files

Mat-files Data storage & loading

Matlab Getting Started

functions

Display Windows

Variables
Variable names:
Must start with a letter

Graphic (Figure) Window


Displays plots and graphs
E.g: surf(magic(30))

Matlab is case sensitive, i.e. one & OnE are different variables.

Assignment statement:
Variable = number; Variable = expression;

Created in response to graphics commands.

M-file editor/debugger window


Create and edit scripts of commands called M-files.

Example:
>> tutorial = 1234; >> tutorial = 1234 tutorial = 1234

NOTE: when a semiend of each command, the result is not displayed.

Vectors, Matrices and Linear Algebra


Vectors Matrices Solutions to Systems of Linear Equations.

Vectors
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.

Matrices
A Matrix array is two-dimensional, having both multiple rows and multiple columns, similar to vector arrays: it begins with [, and end with ] spaces or commas are used to separate elements in a row semicolon or enter is used to separate rows.
A is an m x n matrix.

Matrix Index
The matrix indices begin from 1 The matrix indices must be positive integer
Given:

Example: >> f = [ 1 2 3; 4 5 6] f= 1 2 3 4 5 6

A(-2), A(0) Error: ??? Subscript indices must either be real positive integers or logicals. A(4,2) Error: ??? Index exceeds matrix dimensions.

the main diagonal

Operators (arithmetic)

Operators (Element by Element)

+ * / ^

addition subtraction multiplication division power complex conjugate transpose

.* element-by-element multiplication ./ element-by-element division .^ element-by-element power

Matrices
Transpose Identity Matrix B=A eye(n) returns an n x n identity matrix eye(m,n) returns an m x n matrix with ones on the main diagonal and zeros elsewhere. C=A+B C=A B B = A, where C = A*B B = inv(A), A must be a square matrix in this case. rank (A) returns the rank of the matrix A. B = A.^2 C =A*A matrix. squares each element in the matrix computes A*A, and A must be a square is a scalar.

Solutions to Systems of Linear Equations


Example: a system of 3 linear equations with 3 unknowns (x1, x2, x3):

Addition and subtraction Scalar Multiplication Matrix Multiplication Matrix Inverse Matrix Powers

3x1 + 2x2 x3 = 10 -x1 + 3x2 + 2x3 = 5 x1 x2 x3 = -1


Let : A 3 1 1 2 3 1 1 2 1

x1 x2 x3

10 b 5 1

Then, the system can be described as:

Determinant

det (A), and A must be a square matrix.


are scalars.

Ax = b

A, B, C are matrices, and m, n,

Control Structures

Example: (if else and elseif clauses) if temperature > 100 disp ( Too hot equipment malfunctioning. ) elseif temperature > 90 disp ( Normal operating range. ); else disp ( Too cold turn off equipment. ) end

While Loop Syntax while (condition) Matlab Commands end


Dummy Example
while ((a>3) & (b==5)) Some Matlab Commands; end

Visualization: Plotting

Basic Task: Plot the function sin(x) between 0 x

Create an x-array of 100 samples between 0 and 4 .


>>x=linspace(0,4*pi,100);
1 0.8

Example: >> s = linspace (-5, 5, 100); >> coeff = [ 1 3 3 1]; >> A = polyval (coeff, s); >> plot (s, A), >> xlabel ('s') >> ylabel ('A(s)')

Calculate sin(.) of the x-array


>>y=sin(x);

0.6 0.4 0.2 0 -0.2 -0.4 -0.6

Plot the y-array


>>plot(y)

-0.8 -1

10

20

30

40

50

60

70

80

90

100

Plot the function e-x/3sin(x) between 0 x


Multiply the arrays y and y1 correctly
>>y2=y.*y1;

Display Facilities

title(.)
This is the sinus function 1 0.8

Plot the y2-array


>>plot(y2)

0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 -0.1 -0.2 -0.3 0 10 20 30 40 50 60 70 80 90 100

xlabel(.) ylabel(.)

0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1 0 10 20 30 40 50 x (secs) 60 70 80 90 100

Use of M-File
Click to create a new M-File

Writing User Defined Functions


Functions are m-files which can be executed by specifying some inputs and supply some desired outputs. The code telling the Matlab that an m-file is actually a function is
function out1=functionname(in1) function out1=functionname(in1,in2,in3) function [out1,out2]=functionname(in1,in2)

A text file containing script or function or program to run

You should write this command at the beginning of the m-file and you should save the m-file with a file name same as the function name

Writing User Defined Functions


Another function which takes an input array and returns the sum and product of its elements as outputs

Example
function [V,D,r]=properties(A) % Esta funcin encuentra los % eigenvalores, eigenvectores % y rango de A, dode A es una % matriz cuadrada [m,n]=size(A); if m!=n display else r = rank(A); [V,D] = eig(A); end

function C = mult(A) r = rank(A);

The function sumprod(.) can be called from command window or an m-file as

Function mult computes the multiplication of A by its transpose The variable r is only visible inside the function

To get all the output parameters the funcion must be called as:
[V,D,r]=properties(A)

Useful Commands

The two commands used most by Matlab users are


>>help functionname

>>lookfor keyword

Das könnte Ihnen auch gefallen