{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Matlab_Session_2

# Matlab_Session_2 - Matlab Tutorial Session 2 Beginning...

This preview shows pages 1–5. Sign up to view the full content.

Matlab Tutorial Session 2 Beginning Simple Programming in Matlab (Expected time: 1.5 hours to 2 hours) In session 1, everything was done directly at the command prompt. In this tutorial, the following topics are covered: 1. Using Matlab editor to write simple programs and running these programs. 2. FOR and WHILE loops. 3. Evaluating conditions IF-ELSE and related constructs. 4. Absence of GOTO-type branching and how to program without it. 5. More on workspace management and saving the results to a file. 6. Making simple plots. This tutorial is intended to provide enough Matlab knowledge to do some useful things. Short Review of Session 1 In Matlab, all variables are matrices. A point missed last time was that variable names distinguish between lowercase and uppercase characters. Thus a is not same as A . Variables are defined / altered spontaneously (as the context occurs) and they reside in the workspace . Matrices are defined using square braces + comma or space (for elements on same row) + semicolon or return key (for separating rows), e.g. >> A=[1,2,3;4,5,6] A = 1 2 3 4 5 6 Matrices can also be defined using built-in functions such as ones , eye , rand etc. Matrix elements are accessed using round brackets, e.g. >> A(2,3) ans = 6

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
The colon operator defines a vector of equally-spaced numbers, e.g. >> 1.2 : 0.1 : 1.8 ans = 1.200 1.300 1.400 1.500 1.600 1.700 1.800 >> A(1,:) ans = 1 2 3 There are two types of operations matrix operations and array operations. Array operators begin with a dot ( . ). Examples are >> A'*A % Note: A’ represents transpose of matrix A ans = 17 22 27 22 29 36 27 36 45 >> A.*A ans = 1 4 9 16 25 36 Mathematical functions such as sin , cos , sqrt , etc. all act on matrices, e.g., >> sin( A*pi/6 ) ans = 0.5000 0.8660 1.0000 0.8660 0.5000 0.0000 Many built-in matrix analysis functions such as eig , lu , rank , expm , etc. are available, e.g. >> [V,D]=eig( A'*A ) V = -0.4082 -0.8060 0.4287 0.8165 -0.1124 0.5663 -0.4082 0.5812 0.7039 D = -0.0000 0 0
0 0.5973 0 0 0 90.4027 Using Matlab Editor Matlab editor is started by giving edit command at command prompt Edit command given at Command prompt This is the Matlab Editor Line numbers for program The working of the editor is discussed below. An example is taken all along for illustration. 1. The picture on next page shows the written program. Explanation is as follows: a. All lines end with a semicolon except lines 15 and 18, because we want to see the output on screen of only these two statements when program is run. The output of other statements is suppressed by ending them with a semicolon. b. Matlab editor shows comments (which are indicated by the % sign) in green, statements in black, keywords such as for , end in blue, strings in red. (Keywords and strings will be encountered in this tutorial)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
c. The program is a simple sequence of statements that could be otherwise written directly at the command prompt. There are no formal beginning or end indicators for the program.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 15

Matlab_Session_2 - Matlab Tutorial Session 2 Beginning...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online