simple tutorial for matlab - university of california, san...

18
Simple Tutorial for MATLAB MAE 107 2011 Fall Professor: William M. McEneaney TA: J.J. Gao

Upload: doankhanh

Post on 29-Mar-2019

219 views

Category:

Documents


0 download

TRANSCRIPT

Simple Tutorial for MATLAB

MAE 107 2011 Fall

Professor: William M. McEneaney

TA: J.J. Gao

Outline

Overview

Software Layout / Interfaces

Useful commands and structures

Examples

Overview

MATLAB

• Short for: Matrix Laboratory

• A numerical computing environment

• Non pre-compiled programming language

• It allows:

• matrix manipulations

• plotting of functions and data

• implementation of algorithms

• Creation of user interfaces

Interface 1 – Main Window

Interface 2 –Editor

Interface 3 – Help

Interface 4 – Figures

Syntax and VariablesTwo ways to input command:

at the prompt >> in the Command Windows

Matlab Editor

Varible

matlab is a weakly typed language.

variables can be assigned without declaring their

type.

Vectors / Matrices

all variables are treated as vectors/matrices.

It provides many convenient ways for creating

vectors, matrices, and multi-dimenstional arrays.

Useful Commands & Structures (1)Basic:

help

clc (clear command window)

clear (clear all variables)

close all (close all figures windows)

Operators:

‘ (matrx transpose)

* (matrix multiplication )

.* (array multiplication (element-wise))

the same for other operations, like ^ (power),

/ (divide)

Useful Commands & Structures (2)

Vector/Matrix

: (colon, create vectors, array subscripting, and

for-loop iterators )

c = 1:10; d =1:3:10;

a = [1 2 3 4; 5 6 7 8;3 4 5 6];

zeros, ones; rand;

max, min, sum, mean, std, var, round, floor, ceil

size, cat(concatenate),

Useful Commands & Structures (3)

exp, log, log10, sqrt, abs, conj, i (j), real,

imag,

figure, plot, subplot

grid, hold, legend

xlabel, ylabel, title, xlim, ylim

meshgrid (Generate X and Y arrays for

3-D plots)

poly, roots,

Loops/Conditional Statement

for loop

if statement

while loop

for i= 1:2:11

<execute stuff>

end

while i < 11

<execute stuff>

i= i+ 2;

end

if i < 11

<execute stuff>

else if i > 11

<execute other stuff>

end

CODE 2

Example 1

Example 2

Example 3 (Taylor polynomial)

-1.5 -1 -0.5 0 0.5 1 1.50

0.5

1

1.5

2

2.5

3

3.5

4

4.5exp(t) and its taylor series approximation

x

magnitude

exp(x)

P2(x)

P9

Example 4(textbook page40 algorithm 2.1)

• Evaluate this polynomial using Horner’s rule.

0 1 2 3 40

0.5

1

1.5

2

2.5x 10

4

x

evaula

ted p

oly

nom

ial valu

e

Example 5 (1)(textbook page45 example 2.1)

Example 5 (2)(textbook page45 algorithm 2.1)