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

Simple Tutorial for MATLAB - University of California, San ...maeresearch.ucsd.edu/mceneaney/mae107/matlab_info/matlab_tutorial.pdf · Matlab Editor Varible matlab is a weakly typed

Mar 29, 2019

Download

Documents

doankhanh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Simple Tutorial for MATLAB - University of California, San ...maeresearch.ucsd.edu/mceneaney/mae107/matlab_info/matlab_tutorial.pdf · Matlab Editor Varible matlab is a weakly typed

Simple Tutorial for MATLAB

MAE 107 2011 Fall

Professor: William M. McEneaney

TA: J.J. Gao

Page 2: Simple Tutorial for MATLAB - University of California, San ...maeresearch.ucsd.edu/mceneaney/mae107/matlab_info/matlab_tutorial.pdf · Matlab Editor Varible matlab is a weakly typed

Outline

Overview

Software Layout / Interfaces

Useful commands and structures

Examples

Page 3: Simple Tutorial for MATLAB - University of California, San ...maeresearch.ucsd.edu/mceneaney/mae107/matlab_info/matlab_tutorial.pdf · Matlab Editor Varible matlab is a weakly typed

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

Page 4: Simple Tutorial for MATLAB - University of California, San ...maeresearch.ucsd.edu/mceneaney/mae107/matlab_info/matlab_tutorial.pdf · Matlab Editor Varible matlab is a weakly typed

Interface 1 – Main Window

Page 5: Simple Tutorial for MATLAB - University of California, San ...maeresearch.ucsd.edu/mceneaney/mae107/matlab_info/matlab_tutorial.pdf · Matlab Editor Varible matlab is a weakly typed

Interface 2 –Editor

Page 6: Simple Tutorial for MATLAB - University of California, San ...maeresearch.ucsd.edu/mceneaney/mae107/matlab_info/matlab_tutorial.pdf · Matlab Editor Varible matlab is a weakly typed

Interface 3 – Help

Page 7: Simple Tutorial for MATLAB - University of California, San ...maeresearch.ucsd.edu/mceneaney/mae107/matlab_info/matlab_tutorial.pdf · Matlab Editor Varible matlab is a weakly typed

Interface 4 – Figures

Page 8: Simple Tutorial for MATLAB - University of California, San ...maeresearch.ucsd.edu/mceneaney/mae107/matlab_info/matlab_tutorial.pdf · Matlab Editor Varible matlab is a weakly typed

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.

Page 9: Simple Tutorial for MATLAB - University of California, San ...maeresearch.ucsd.edu/mceneaney/mae107/matlab_info/matlab_tutorial.pdf · Matlab Editor Varible matlab is a weakly typed

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)

Page 10: Simple Tutorial for MATLAB - University of California, San ...maeresearch.ucsd.edu/mceneaney/mae107/matlab_info/matlab_tutorial.pdf · Matlab Editor Varible matlab is a weakly typed

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),

Page 11: Simple Tutorial for MATLAB - University of California, San ...maeresearch.ucsd.edu/mceneaney/mae107/matlab_info/matlab_tutorial.pdf · Matlab Editor Varible matlab is a weakly typed

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,

Page 12: Simple Tutorial for MATLAB - University of California, San ...maeresearch.ucsd.edu/mceneaney/mae107/matlab_info/matlab_tutorial.pdf · Matlab Editor Varible matlab is a weakly typed

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

Page 13: Simple Tutorial for MATLAB - University of California, San ...maeresearch.ucsd.edu/mceneaney/mae107/matlab_info/matlab_tutorial.pdf · Matlab Editor Varible matlab is a weakly typed

Example 1

Page 14: Simple Tutorial for MATLAB - University of California, San ...maeresearch.ucsd.edu/mceneaney/mae107/matlab_info/matlab_tutorial.pdf · Matlab Editor Varible matlab is a weakly typed

Example 2

Page 15: Simple Tutorial for MATLAB - University of California, San ...maeresearch.ucsd.edu/mceneaney/mae107/matlab_info/matlab_tutorial.pdf · Matlab Editor Varible matlab is a weakly typed

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

Page 16: Simple Tutorial for MATLAB - University of California, San ...maeresearch.ucsd.edu/mceneaney/mae107/matlab_info/matlab_tutorial.pdf · Matlab Editor Varible matlab is a weakly typed

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

Page 17: Simple Tutorial for MATLAB - University of California, San ...maeresearch.ucsd.edu/mceneaney/mae107/matlab_info/matlab_tutorial.pdf · Matlab Editor Varible matlab is a weakly typed

Example 5 (1)(textbook page45 example 2.1)

Page 18: Simple Tutorial for MATLAB - University of California, San ...maeresearch.ucsd.edu/mceneaney/mae107/matlab_info/matlab_tutorial.pdf · Matlab Editor Varible matlab is a weakly typed

Example 5 (2)(textbook page45 algorithm 2.1)