cs227-scientific computing lecture 1-some matlab...

30
CS227-Scientific Computing Lecture 1-Some MATLAB Basics

Upload: others

Post on 22-Mar-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

CS227-Scientific Computing

Lecture 1-Some MATLAB Basics

Page 2: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

Contents of this lecture

•  Using MATLAB as a calculator.

•  MATLAB’s particular way of handling vectors and matrices.

•  Plotting parametrized curves and surfaces.

Page 3: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

Using MATLAB as a calculator

Page 4: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

Using MATLAB as a calculator

Page 5: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

Variables Represent Matrices

Page 6: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

Row vectors and column vectors

Page 7: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

Row vectors and column vectors

Page 8: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

Row vectors and column vectors

Page 9: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

Creating Matrices

Page 10: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

Arithmetic with matrices

Page 11: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

Arithmetic with matrices

Page 12: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

Arithmetic with matrices

Page 13: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

Accessing Ranges of Matrix Elements

Page 14: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

Built-in Functions are Applied Componentwise to Matrices

Page 15: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

A more complicated example

Page 16: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

User-Defined Functions

Page 17: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

User-Defined Functions

Page 18: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

Plotting

•  There are two flavors of plotting---the ‘ez’ version, where the source is a known mathematical function, and the ordinary version, where the source is discrete data.

•  Here we present just a few useful techniques.

Page 19: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

Superimposed Graphs of two Functions

•  Suppose we want to superimpose the graphs of

and

in order to see where they cross. Note that there will be no solutions for x>1, and that both graphs are symmetric about the y-axis (they are even functions).

Page 20: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

Superimposed Graphs of two Functions

You can use the menus in the Figure window to alter the appearance (e.g., scaling and range of axes, placement of legends and grid lines,…).

Page 21: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

Parametrized Curve in Plane •  The curve is the set of points

for some functions x and y. (Think of this as giving the position of a point at time t.)

•  You get a nice ellipse from

Page 22: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

Parametrized Curve in Plane

Page 23: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

Curves in 3-Dimensional Space Use ezplot3(X,Y,Z) in exactly the way we

used ezplot(X,Y). Example: Helix of height 1, radius 1, with 5

turns, given by

Page 24: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

Curves in 3-Dimensional Space

Page 25: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

Surfaces in 3-Dimensional Space

•  In general, parametrized surface given by

•  Example: Sphere of radius 1 parametrized by spherical coordinates (essentially latitude and longitude):

Page 26: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

Surfaces in 3-Dimensional Space

Page 27: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

Using M-files

•  Instead of typing these 4 instructions on the command line, we can place them in a text file. (Use the Edit window in MATLAB to create it.)

•  Save the file with a name like “sphere.m” •  Type “sphere” and the command prompt

to execute.

Page 28: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

plot

•  We can use plot if the data source is a vector. The syntax is

plot(x1,y1,x2,y2,….) where x1,y1 are vectors of the same size

giving x- and y- coordinates of one series of data, x2,y2 give x and y coordinates of the next series. You can supply additional arguments specifying how to draw each data series.

Page 29: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

plot

•  The commands on the next page plot the first 19 Fibonacci numbers

1,1,2=1+1,3=2+1,5=3+2,8=5+3,13=8+5,… as red dots, and the graph of ((1+sqrt(5))/2)n/sqrt(5) as a black curve (in reality, though, a series

of line segments).

Page 30: CS227-Scientific Computing Lecture 1-Some MATLAB Basicsstraubin/scicomp_2011/Lecture1_MATLAB_Basics.pdfCS227-Scientific Computing Lecture 1-Some MATLAB Basics . Contents of this lecture

plot