barbara/pratik/yu. outline matlab desktop m-files variables arrays plots

21
Matlab Introduction Barbara/Pratik/Yu

Upload: austen-holmes

Post on 05-Jan-2016

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Barbara/Pratik/Yu. Outline Matlab desktop M-files Variables Arrays Plots

Matlab IntroductionBarbara/Pratik/Yu

Page 2: Barbara/Pratik/Yu. Outline Matlab desktop M-files Variables Arrays Plots

OutlineMatlab desktopM-filesVariablesArraysPlots

Page 3: Barbara/Pratik/Yu. Outline Matlab desktop M-files Variables Arrays Plots

Matlab Desktop

Command Window

Workspace

Command History

Page 4: Barbara/Pratik/Yu. Outline Matlab desktop M-files Variables Arrays Plots

M-filesSave set of commands in a file Long code that requires more than a couple

of commandsAllows you to save codeExtension that is used in matlab

Examples -- Filename.m, anyname.m

Page 5: Barbara/Pratik/Yu. Outline Matlab desktop M-files Variables Arrays Plots

How to create an m-fileClick on File in upper left hand sideClick on newClick on blank m-fileType codeSave as

Page 6: Barbara/Pratik/Yu. Outline Matlab desktop M-files Variables Arrays Plots

How to open/edit/close an m-fileClick on fileClick on openThen choose the m-fileMake any changes needed to the fileClick on saveThen close the file by the x located in the

upper right hand or File -> Close

Page 7: Barbara/Pratik/Yu. Outline Matlab desktop M-files Variables Arrays Plots

Variable Assignmentx=4 --Simple numerical

assignment

t = 10 – 4 -- Result of an operation

y = pi --Special assignment

z = 3 + y --Result of an operation with variables

Page 8: Barbara/Pratik/Yu. Outline Matlab desktop M-files Variables Arrays Plots

ArraysVariables in Matlab can be accessed by

indexingEvery element of an array is designated by its

unique row and column or a number pair

The syntax is A(row#,column#)

Page 9: Barbara/Pratik/Yu. Outline Matlab desktop M-files Variables Arrays Plots

VectorsVectors are 1-dimensional arraysThere are several ways to create a vectort= xmin:dx:xmaxtime= [ 1,2,3,4,5 ]t=linspace(xmin,xmax,dx)x=linspace(0,5,1)

Page 10: Barbara/Pratik/Yu. Outline Matlab desktop M-files Variables Arrays Plots

Commandsclear----Clears the workspaceclc—clears the command windowsave – saves the file from the

command windowload—load the specified file from the

command window

Page 11: Barbara/Pratik/Yu. Outline Matlab desktop M-files Variables Arrays Plots

Plotsplot(x,y) --plots 2-d data with x axis with

values of x and y axis with values of y

Page 12: Barbara/Pratik/Yu. Outline Matlab desktop M-files Variables Arrays Plots

Change plot line colorFor example

plot(year,country,’b’) will make the plot line color blue

Color options g green r red c cyan m magenta k black

Page 13: Barbara/Pratik/Yu. Outline Matlab desktop M-files Variables Arrays Plots

Change Line StyleFor example

plot(year, country,’-’) will make the line style solid

Other line styles:-- dashed: dotted-. dash dot

Page 14: Barbara/Pratik/Yu. Outline Matlab desktop M-files Variables Arrays Plots

Change plot markerplot(year, country, ’d’) diagonal cross

Other marker options + vertical cross * star

d diamond

0.5 1 1.5 2 2.5 30

0.5

1

1.5

2

2.5

3

3.5

Y-Values

Y-Values

Page 15: Barbara/Pratik/Yu. Outline Matlab desktop M-files Variables Arrays Plots

Plot typesBar graph

bar(x,y)

Stem graph stem(x,y)

Categ

ory

1

Categ

ory

2

Categ

ory

3

Categ

ory

40246

Bar

Bar

Page 16: Barbara/Pratik/Yu. Outline Matlab desktop M-files Variables Arrays Plots

Annotating PlotsTo make a label for the x-axis

xlabel(‘Year’)

To make a label for the y-axisylabel(‘Country’)

To make a title for the Plottitle(‘Gas Prices’)

Page 17: Barbara/Pratik/Yu. Outline Matlab desktop M-files Variables Arrays Plots

Creating a SignalCreate a continuous signal

F(t)=A * sin(ωt)

Page 18: Barbara/Pratik/Yu. Outline Matlab desktop M-files Variables Arrays Plots

Define a discrete time axisThere are several ways to create the time axis—(creating a vector)

t= xmin:dx:xmaxtime= [ 1,2,3,4 ]t=linspace(xmin,xmax,dx)x=linspace(0,100,5)

Page 19: Barbara/Pratik/Yu. Outline Matlab desktop M-files Variables Arrays Plots

Plotting dataType in the following code (excluding the comments

denoted by %):

A=2; %assign value for variable A frequency=0.5; %assign value for variable frequencyw=2*pi*frequency; %set value for w tmin=0; %assign starting time tmax=10; %assign end timedt=0.01; % assign increment time t=tmin:dt:tmax; %define time vector f=A*sin(w*t); %define the continuous time signal function plot(t,f,'r'); %plot with color of red. 'r'-redxlabel('time: t'); %set lable for x axisylabel('signal: f(t)'); %set lable for y axis title('function of continuous time signal'); %set title

Page 20: Barbara/Pratik/Yu. Outline Matlab desktop M-files Variables Arrays Plots
Page 21: Barbara/Pratik/Yu. Outline Matlab desktop M-files Variables Arrays Plots