cs227-scientific computing lecture 1-some matlab...
TRANSCRIPT
CS227-Scientific Computing
Lecture 1-Some MATLAB Basics
Contents of this lecture
• Using MATLAB as a calculator.
• MATLAB’s particular way of handling vectors and matrices.
• Plotting parametrized curves and surfaces.
Using MATLAB as a calculator
Using MATLAB as a calculator
Variables Represent Matrices
Row vectors and column vectors
Row vectors and column vectors
Row vectors and column vectors
Creating Matrices
Arithmetic with matrices
Arithmetic with matrices
Arithmetic with matrices
Accessing Ranges of Matrix Elements
Built-in Functions are Applied Componentwise to Matrices
A more complicated example
User-Defined Functions
User-Defined Functions
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.
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).
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,…).
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
Parametrized Curve in Plane
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
Curves in 3-Dimensional Space
Surfaces in 3-Dimensional Space
• In general, parametrized surface given by
• Example: Sphere of radius 1 parametrized by spherical coordinates (essentially latitude and longitude):
Surfaces in 3-Dimensional Space
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.
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.
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).
plot