math 98 - introduction to matlab programmingcpoli/math98/lecture6.pdf · math 98 - introduction to...

21

Upload: lyxuyen

Post on 17-Mar-2018

225 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Math 98 - Introduction to MATLAB Programmingcpoli/math98/lecture6.pdf · Math 98 - Introduction to MATLAB Programming Fall 2016 - Lecture 6 allF 2016 - Lecture 6 Math 98 - Introduction

Math 98 - Introduction to MATLAB Programming

Fall 2016 - Lecture 6

Fall 2016 - Lecture 6 Math 98 - Introduction to MATLAB Programming

Page 2: Math 98 - Introduction to MATLAB Programmingcpoli/math98/lecture6.pdf · Math 98 - Introduction to MATLAB Programming Fall 2016 - Lecture 6 allF 2016 - Lecture 6 Math 98 - Introduction

Reminders

Instructor: Chris Policastro

Class Website:https://math.berkeley.edu/~cpoli/math98/fall2016.html

Assignment Submission:https://bcourses.berkeley.edu

Homework 5

1 Due September 29th by 11:59pm on bCourses.

2 See homework5.pdf for simpli�ed version of assignment not using cells orstructures. LangtonAntPlot.m has been updated. See bCourses for hints.

Fall 2016 - Lecture 6 Math 98 - Introduction to MATLAB Programming

Page 3: Math 98 - Introduction to MATLAB Programmingcpoli/math98/lecture6.pdf · Math 98 - Introduction to MATLAB Programming Fall 2016 - Lecture 6 allF 2016 - Lecture 6 Math 98 - Introduction

Lecture 6: Plotting and Animation

1 Problems(problem_6_1) Animating a dot on a circle(problem_6_2) Animating temperature (Chapters 4.2,7.3 van Loan)

2 Concepts�gure, axes, plot, pausergb vector, fill, shgpcolor, colormap, shading, caxis

Fall 2016 - Lecture 6 Math 98 - Introduction to MATLAB Programming

Page 4: Math 98 - Introduction to MATLAB Programmingcpoli/math98/lecture6.pdf · Math 98 - Introduction to MATLAB Programming Fall 2016 - Lecture 6 allF 2016 - Lecture 6 Math 98 - Introduction

Cells

We have discussed string, vector and matrix arrays. Suppose we want to storetwo vectors as a matrix.

Enter Vector1=[2,4,5] and Vector2=[-4,0,10] into the command line.Form Matrix=[Vector1;Vector2].

Recall that matrix arrays must be rectangular.

Can you combine Vector1=[2,4,5] and Vector2=[-4,0] into a matrix.

Fall 2016 - Lecture 6 Math 98 - Introduction to MATLAB Programming

Page 5: Math 98 - Introduction to MATLAB Programmingcpoli/math98/lecture6.pdf · Math 98 - Introduction to MATLAB Programming Fall 2016 - Lecture 6 allF 2016 - Lecture 6 Math 98 - Introduction

Cells

A cell array is a data type used to store other arrays. The format isCellName={...}.

Enter Triangle={[1],[1,1],[1,2,1]}.

Triangle is a 1× 3 cell array consisting of three vectors of sizes 1× 1, 1× 2,and 1× 3

What is Triangle(2)? What is Triangle{2}?

Use CellName(...) to access the entry. Use CellName{...} to access thedata in the entry.

Fall 2016 - Lecture 6 Math 98 - Introduction to MATLAB Programming

Page 6: Math 98 - Introduction to MATLAB Programmingcpoli/math98/lecture6.pdf · Math 98 - Introduction to MATLAB Programming Fall 2016 - Lecture 6 allF 2016 - Lecture 6 Math 98 - Introduction

Cells

Cell arrays are rectangular.

Enter Triangle={[1];[1,1];[1,2,1]}. Can you enterTriangle={[1],[1,1];[1,2,1]}?

Nonetheless, they help us store nonrectangular arrays because each entry isitself an array.

Intialize an empty n×m cell array using

cell(n,m).

Fall 2016 - Lecture 6 Math 98 - Introduction to MATLAB Programming

Page 7: Math 98 - Introduction to MATLAB Programmingcpoli/math98/lecture6.pdf · Math 98 - Introduction to MATLAB Programming Fall 2016 - Lecture 6 allF 2016 - Lecture 6 Math 98 - Introduction

Cells

A cell array can be stored as an entry of a cell array.

Enter Triangle={Triangle,[1,3,3,1]}. Is Triangle an array of fourvectors, or an array of one cell and one vector?

Contrast the behavior of cells and vectors. If Triangle were the vector[1,1,1,1,2,1], then Triangle=[Triangle,[1,3,3,1]] would give thevector

[1, 1, 1, 1, 2, 1, 1, 3, 3, 1].

How can you access the second entry of the vector [1, 2, 1] in Triangle?

Fall 2016 - Lecture 6 Math 98 - Introduction to MATLAB Programming

Page 8: Math 98 - Introduction to MATLAB Programmingcpoli/math98/lecture6.pdf · Math 98 - Introduction to MATLAB Programming Fall 2016 - Lecture 6 allF 2016 - Lecture 6 Math 98 - Introduction

Exercise

Pascal's triangle is a collection of numbers that appears as the coe�cients in(1 + x)n

1 For n = 0 have [1]

2 For n = 1 have [1, 1]

3 For n = 2 have [1, 2, 1]

Note that [1, 2, 1] = [1, 1, 0] + [0, 1, 1]. In general we can use the formula

Row j+ Row j+1 = Row j+2

to generate more rows of Pascal's triangle.Generate the 20 rows of Pascals triangle. Store in a cell array calledPascalTriangle.What is the 10th entry of the 20th row?

Fall 2016 - Lecture 6 Math 98 - Introduction to MATLAB Programming

Page 9: Math 98 - Introduction to MATLAB Programmingcpoli/math98/lecture6.pdf · Math 98 - Introduction to MATLAB Programming Fall 2016 - Lecture 6 allF 2016 - Lecture 6 Math 98 - Introduction

Structures

We access entries of a string, matrix, or cell array by specifying an index. If wecould attach a string to the index, then it would be easier to access thecorresponding entry. The string would help us look up the data in the entry.

Fall 2016 - Lecture 6 Math 98 - Introduction to MATLAB Programming

Page 10: Math 98 - Introduction to MATLAB Programmingcpoli/math98/lecture6.pdf · Math 98 - Introduction to MATLAB Programming Fall 2016 - Lecture 6 allF 2016 - Lecture 6 Math 98 - Introduction

Structures

A structure array is a collection of �elds and values. The format is

StructureName = struct(field1, value1, ..., fieldN, valueN)

field1 through fieldN are strings. value1 through valueN are cells. We lookup the �eld field1 to access the value value1.

1 Conve r t e r 1=s t r u c t ( 'V ' , 5 ) ;2

3 Value=Conve r t e r1 .V ;

The structure is called Converter1. It contains the �eld V. It contains thevalue 5. Here Value equals 5.

Fall 2016 - Lecture 6 Math 98 - Introduction to MATLAB Programming

Page 11: Math 98 - Introduction to MATLAB Programmingcpoli/math98/lecture6.pdf · Math 98 - Introduction to MATLAB Programming Fall 2016 - Lecture 6 allF 2016 - Lecture 6 Math 98 - Introduction

Structures

Enter Converter2=struct(`First',[1],`Second',[1,1])

Note that Converter2.First is the 1× 1 vector [1] and Converter2.Second

is the 1× 2 vector [1, 1].

Fall 2016 - Lecture 6 Math 98 - Introduction to MATLAB Programming

Page 12: Math 98 - Introduction to MATLAB Programmingcpoli/math98/lecture6.pdf · Math 98 - Introduction to MATLAB Programming Fall 2016 - Lecture 6 allF 2016 - Lecture 6 Math 98 - Introduction

Exercise: Roman numerals

Recall that we studied Roman numerals in homework 3.

Write a structure called Converter with �elds I,II,III,IV,V,VI,VII,VIII,IX,X andvalues the corresponding number 1,2,3,4,5,6,7,8,9,10.

Fall 2016 - Lecture 6 Math 98 - Introduction to MATLAB Programming

Page 13: Math 98 - Introduction to MATLAB Programmingcpoli/math98/lecture6.pdf · Math 98 - Introduction to MATLAB Programming Fall 2016 - Lecture 6 allF 2016 - Lecture 6 Math 98 - Introduction

(problem_6_1) Animating a dot on a circle

In homework assignment 6, you are asked to animate a dot moving on a circle.The circle is moving.

As preparation, we want to animate a dot moving on a circle. The circle isstationary.

Make an animation that

plots a black circle of radius 1 with center (0, 0)

plots a red dot on the circle

shows the dot moving counterclockwise around the circle

Fall 2016 - Lecture 6 Math 98 - Introduction to MATLAB Programming

Page 14: Math 98 - Introduction to MATLAB Programmingcpoli/math98/lecture6.pdf · Math 98 - Introduction to MATLAB Programming Fall 2016 - Lecture 6 allF 2016 - Lecture 6 Math 98 - Introduction

Plot

What happens when you type

plot(0:.1:2*pi,cos(0:.1:2*pi))

Now tryplot(0:.1:2*pi,sin(0:.1:2*pi))

A plot appears in a �gure. MATLAB used the same �gure to plot sine. Thiserased the plot of cosine. If we had entered

hold on

before plotting sine, then MATLAB would have plotted sine in the same �gurewithout erasing cosine.

Enter figure. Enter plot(0:.1:2*pi,sin(0:.1:2*pi))

If we want to plot sine in another �gure, then we use figure to intialize ablank �gure.

Use clf to clear a �gure. Use close to close the �gure.

Fall 2016 - Lecture 6 Math 98 - Introduction to MATLAB Programming

Page 15: Math 98 - Introduction to MATLAB Programmingcpoli/math98/lecture6.pdf · Math 98 - Introduction to MATLAB Programming Fall 2016 - Lecture 6 allF 2016 - Lecture 6 Math 98 - Introduction

Plot

Suppose we want to

plot a circle of radius 1 centered at the point (0, 0).

mark the point (0, 0) with a red dot

1 p l o t ( cos ( 0 : . 1 : 2 ∗ p i ) , s i n ( 0 : . 1 : 2 ∗ p i ) , ' L ineWidth ' , 5 ) ;2

3 ho ld on ;4

5 p l o t ( [ 0 ] , [ 0 ] , ' o ' , ' MarkerFaceCo lor ' , ' r ' , ' Marke rS i ze ' , 30);

6

7 %p l o t ( [ 0 ] , [ 0 ] , ' o ' , ' MarkerS ize ' , 3 0 , ' MarkerFaceColor ' , ' r' ) ;

Fall 2016 - Lecture 6 Math 98 - Introduction to MATLAB Programming

Page 16: Math 98 - Introduction to MATLAB Programmingcpoli/math98/lecture6.pdf · Math 98 - Introduction to MATLAB Programming Fall 2016 - Lecture 6 allF 2016 - Lecture 6 Math 98 - Introduction

Axes

We can adjust the axes of a plot using

gca.

Think of gca as a handle to the axes of the plot.

Set hPlot=gca. Type hPlot.XLim=[-2,2] and hPlot.YLim=[-2,2]. How dothe axes change?

Note that hPlot is a structure with many �elds controlling the appearance ofthe axes

Spacing of tick marks on x axis and y axis

Labels for tick marks

...

Fall 2016 - Lecture 6 Math 98 - Introduction to MATLAB Programming

Page 17: Math 98 - Introduction to MATLAB Programmingcpoli/math98/lecture6.pdf · Math 98 - Introduction to MATLAB Programming Fall 2016 - Lecture 6 allF 2016 - Lecture 6 Math 98 - Introduction

Plot

1 p l o t ( cos ( 0 : . 1 : 2 ∗ p i ) , s i n ( 0 : . 1 : 2 ∗ p i ) , ' L ineWidth ' , 5 ) ;2 hP lo t=gca ;3 hP lo t =4

5 Axes w i th p r o p e r t i e s :6

7 XLim : [−1 1 ]8 YLim : [−1 1 ]9 XScale : ' l i n e a r '10 YScale : ' l i n e a r '11 G r i d L i n e S t y l e : '− '12 Po s i t i o n : [ 0 . 1 300 0 .1100 0 .7750 0 . 8 150 ]13 Un i t s : ' no rma l i z ed '14

15 hP lo t . XLim=[−10 ,10] ;

Fall 2016 - Lecture 6 Math 98 - Introduction to MATLAB Programming

Page 18: Math 98 - Introduction to MATLAB Programmingcpoli/math98/lecture6.pdf · Math 98 - Introduction to MATLAB Programming Fall 2016 - Lecture 6 allF 2016 - Lecture 6 Math 98 - Introduction

Animation

The commandpause

is used to halt the a procedure for a speci�ed number of seconds.

1 f u n c t i o n [ ]= CircDotMovie ( )2

3 p l o t ( cos ( 0 : 0 . 0 0 1 : 2 ∗ p i ) , s i n ( 0 : 0 . 0 0 1 : 2 ∗ p i ) , ' k ' , ' L ineWidth' , 5 ) ;

4

5 a x i s equa l ;6 ho ld on ;7

8 pause (3 ) ;9

10 p l o t ( [ 0 ] , [ 0 ] , ' o ' , ' Marke rS i ze ' ,30 , ' MarkerFaceCo lor ' , ' r ' );

11

12 end

Fall 2016 - Lecture 6 Math 98 - Introduction to MATLAB Programming

Page 19: Math 98 - Introduction to MATLAB Programmingcpoli/math98/lecture6.pdf · Math 98 - Introduction to MATLAB Programming Fall 2016 - Lecture 6 allF 2016 - Lecture 6 Math 98 - Introduction

Exercise: Random walk

Create an animation with �rst frame

1 �gure with axes -10 to 10 in x direction and -2 to 2 in the y direction

2 black horizontal line between [-10,0] and [10,0]

3 adjust the axes to have tick marks at -10,-9,. . . ,9,10

4 a red dot at [0,0]

and subsequent frames

1 red dot moves left with probability 1/2 or right with probability 1/2

2 animation end when red dot at [-10,0] or [10,0]

Fall 2016 - Lecture 6 Math 98 - Introduction to MATLAB Programming

Page 20: Math 98 - Introduction to MATLAB Programmingcpoli/math98/lecture6.pdf · Math 98 - Introduction to MATLAB Programming Fall 2016 - Lecture 6 allF 2016 - Lecture 6 Math 98 - Introduction

(problem_6_2) Animating temperature

Consider a rectangular plate with vertices at

(0, 0), (6, 0), (6, 4), (0, 4)

We want to animate temperature changes on the plate. The intial temperatureis given by

T (x, y) = 100e−0.4((x−1)2+0.7(y−3)2) + 80e−0.2(2(x−5)2+1.5(y−1)2)

The temperature changes according to averaging. See the project for morediscussion of the model involving averaging.

Fall 2016 - Lecture 6 Math 98 - Introduction to MATLAB Programming

Page 21: Math 98 - Introduction to MATLAB Programmingcpoli/math98/lecture6.pdf · Math 98 - Introduction to MATLAB Programming Fall 2016 - Lecture 6 allF 2016 - Lecture 6 Math 98 - Introduction

Review of (problem_6_2)

We need to use the following commands. See van Loan chapter 7.3 for moreinformation

1 linspace(a,b,n) produces a vector with n equally spaced numbersbetween a and b.

2 pcolor takes a matrix and displays a color over each entry. The color isdetermined by the value of the entry according to a color map. `hot' is abuilt in color map.

3 caxis manual is used to prevent the color map changing between frames.

4 shading interp is used to produce a blended image.

Fall 2016 - Lecture 6 Math 98 - Introduction to MATLAB Programming