cs227-scientific computing lecture 1-some matlab...
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/19.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/20.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/21.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/22.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/23.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/24.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/25.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/26.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/27.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/28.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/29.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022081521/5e813f2339af136ee91424a1/html5/thumbnails/30.jpg)
plot