Sie sind auf Seite 1von 21

MATLAB Basic

What is MATLAB?
A software environment for interactive numerical computations. MATLAB stands for MATrix LABoratory Examples: Matrix computations and linear algebra Solving nonlinear equations Numerical solution of differential equations Mathematical optimization Statistics and data analysis Signal processing

Matlab Windows
o Command line Interface ( Main Window) o Editor Window
o Present Directory o Directory Contents and Workspace variables o Command line o Command History

MATLAB BASICS
Variables and Arrays Array: A collection of data values organized into rows
and columns, and known by a single name.
Row 1 Row 2 Row 3 arr(3,2) Row 4 Col 1 Col 2 Col 3 Col 4 Col 5
4

MATLAB BASICS
Arrays The fundamental unit of data in MATLAB Scalars are also treated as arrays by MATLAB (1 row and 1 column). Row and column indices of an array start from 1. Arrays can be classified as vectors and matrices.

MATLAB BASICS
Vector: Array with one dimension Matrix: Array with more than one dimension Size of an array is specified by the number of rows and the number of columns, with the number of rows mentioned first (For example: n x m array). Total number of elements in an array is the product of the number of rows and the number of columns.

MATLAB BASICS
1 2 a= 3 4 5 6 b=[1 2 3 4] 1 c= 3 5 a(2,1)=3
Row # Column #
7

3x2 matrix 6 elements 1x4 array 4 elements, row vector 3x1 array 3 elements, column vector

b(3)=3

c(2)=3

MATLAB BASICS
Variables
A region of memory containing an array, which is known by a user-specified name. Contents can be used or modified at any time. Variable names must begin with a letter, followed by any combination of letters, numbers and the underscore (_) character. The MATLAB language is Case Sensitive. NAME, name and Name are all different variables.

MATLAB BASICS
Initializing Variables in Assignment Statements An assignment statement has the general form var = expression Examples:
>> var = 40 * i; >> var2 = var / 5; >> array = [1 2 3 4]; >> x = 1; y = 2; >> a = [3.4]; >> b = [1.0 2.0 3.0 4.0]; >> c = [1.0; 2.0; 3.0]; >> d = [1, 2, 3; 4, 5, 6]; >> e = [1, 2, 3 4, 5, 6]; >> a2 = [0 1+8]; >> b2 = [a2(2) 7 a]; >> c2(2,3) = 5; >> d2 = [1 2]; >> d2(4) = 4;

; semicolon suppresses the automatic echoing of values but it slows down the execution.
9

MATLAB BASICS
Initializing with Shortcut Expressions first: increment: last
Colon operator: a shortcut notation used to initialize arrays with thousands of elements >> x = 1 : 2 : 10; >> angles = (0.01 : 0.01 : 1) * pi; Transpose operator: () swaps the rows and columns of an array 1 1 >> f = [1:4]; >> g = 1:4; >> h = [ g g ]; 2 2 h= 3 3 4 4
10

MATLAB BASICS
Initializing with Keyboard Input
The input function displays a prompt string in the Command Window and then waits for the user to respond. my_val = input( Enter an input value: ); in1 = input( Enter data: ); in2 = input( Enter data: ,`s`);
11

MATLAB BASICS
MATLAB Script Files A MATLAB script file (Called an M-file) is a text file that contains one or more MATLAB commands and, optionally, comments. The file is saved with the extension ".m". When the filename (without the extension) is issued as a command in MATLAB, the file is opened, read, and the commands are executed as if input from the keyboard. The result is similar to running a program in C.

12

MATLAB BASICS
MATLAB Function Files A MATLAB function file (called an M-file) is a text file that contains a MATLAB function and, optionally, comments. The file is saved with the function name and the usual MATLAB script file extension, ".m". A MATLAB function may be called from the command line or from any other M-file.

13

MATLAB BASICS
MATLAB Function Files When the function is called in MATLAB, the file is accessed, the function is executed, and control is returned to the MATLAB workspace. Since the function is not part of the MATLAB workspace, its variables and their values are not known after control is returned. Any values to be returned must be specified in the function syntax.
14

MATLAB BASICS
FUNCTIONS in MATLAB Syntax function [out1, out2, ...] = funname(in1, in2, ...) Description function [out1, out2, ...] = funname(in1, in2, ...) defines function funname that accepts inputs in1, in2, etc. and returns outputs out1, out2, etc.

15

MATLAB BASICS
EXAMPLE: function [det_A]=solvexf(r) ; A=[5 2*r r ; 3 6 2*r-1 ; 2 r-1 3*r]; det_A=det(A); Calling the function: [detA]=solvexf(1)

16

MATLAB BASICS
IF ELSE statement
if (condition statement) (matlab commands) end if (condition statement) (matlab commands) elseif (condition statement) (matlab commands) else (matlab commands) end

17

MATLAB BASICS
FOR statement
for a=0:50 disp('Hello World') end for a=10:10:50 for b=0:0.1:1 disp('MATLAB') end end
18

MATLAB BASICS
Summary
help command lookfor keyword which clear clc who, whos Ctrl+c % Online help Lists related commands Version and location info Clears the workspace Clears the command window Lists content of the workspace Aborts operation Continuation Comments

19

MATLAB BASICS
PROBLEM 1x(t)

2 1

-2

-1

USE MATLAB TO PLOT THIS SIGNAL

20

MATLAB BASICS
UNIT STEP FUNCTION UNIT STEP DELAYED BY t0

21

Das könnte Ihnen auch gefallen