User Tools

Site Tools


gibson:teaching:fall-2014:math445:lecture16-diary

====== Differences ====== This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
gibson:teaching:fall-2014:math445:lecture16-diary [2014/11/18 06:53]
gibson created
gibson:teaching:fall-2014:math445:lecture16-diary [2014/11/18 07:25] (current)
gibson [surf, surfc]
Line 11: Line 11:
   * axis (equal, tight)   * axis (equal, tight)
   ​   ​
-====== meshgrid: ​======+===== meshgrid: =====
  
 The ''​meshgrid''​ function is essential for Matlab'​s 3D graphics. Meshgrid creates 2D arrays of x,y data covering the x,y, plane, over which a function can be evaluated and graphed. Example: The ''​meshgrid''​ function is essential for Matlab'​s 3D graphics. Meshgrid creates 2D arrays of x,y data covering the x,y, plane, over which a function can be evaluated and graphed. Example:
  
 <code matlab> <code matlab>
->> x = linspace(-2,​2,​5)+>> x = linspace(-2,​2,​5) ​
  
 x = x =
Line 46: Line 46:
      ​3 ​    ​3 ​    ​3 ​    ​3 ​    3      ​3 ​    ​3 ​    ​3 ​    ​3 ​    3
 </​code>​ </​code>​
-Observe how the output matrices X varies left to right, along the x axis, and Y varies up and down, along the y axis. Together they provide x,y, coordinates for a grid of points on the x,y plane. ​+It is conventional to use capital letters (X,Y) for the matrix output of meshgrid from small letter (x,y) vector inputs. ​Observe how the output matrices X varies left to right, along the x axis, and Y varies up and down, along the y axis. Together they provide x,y, coordinates for a grid of points on the x,y plane in the region $-2 \leq x \leq 2$, $-3 \leq y \leq 3$.
    
-===== pcolor: pseudocolor plot =====+We can then evaluate a function $z f(x,y)$ over that 2D array via elementwise matrix operations. For example, this Matlab code would evaluate $z f(x,​y) ​x^2 + y^2$
  
-% 3D graphics: plot z=f(x,y) versus x,y+<code matlab>​ 
 +X.^2 + Y.^2 
 +</​code>​
  
-% need to evaluate f over aplane x,y+===== pcolor: pseudocolor plot =====
  
-% define simple ​function ​f(x,y) = 1/2 x^2 + y^2+The ''​pcolor'' ​function ​produces a pseudocolor or checkerboard plot of Z as a function of x,y
 +<code matlab>​ 
 +pcolor(X,​Y,​Z 
 +colorbar 
 +axis equal  
 +axis tight 
 +xlabel('​x'​);​ ylabel('​y'​);​ title('​z ​= x^2 + y^2') 
 +</​code>​ 
 +You can modify the appearance of the pseudocolor with the ''​shading''​ command. Try 
 +''​shading flat'',​ ''​shading interp'',​ and ''​shading faceted''​.
  
-linspace(-2,2,5)+===== contourcontourf =====
  
-x = +The ''​contour''​ function plots contours or level curves of z=f(x,​y). ​That is, it plots curves 
- +on which f(x,y) is constant.
-    -2    -1     ​0 ​    ​1 ​    2 +
- +
-y = linspace(-3,​3,​6) +
- +
-y = +
- +
-   ​-3.0000 ​  ​-1.8000 ​  ​-0.6000 ​   0.6000 ​   1.8000 ​   3.0000 +
- +
-[X,Y] = meshgrid(x,​y);​ +
-+
- +
-X = +
- +
-    -2    -1     ​0 ​    ​1 ​    2 +
-    -2    -1     ​0 ​    ​1 ​    2 +
-    -2    -1     ​0 ​    ​1 ​    2 +
-    -2    -1     ​0 ​    ​1 ​    2 +
-    -2    -1     ​0 ​    ​1 ​    2 +
-    -2    -1     ​0 ​    ​1 ​    2 +
- +
-+
- +
-Y = +
- +
-   ​-3.0000 ​  ​-3.0000 ​  ​-3.0000 ​  ​-3.0000 ​  ​-3.0000 +
-   ​-1.8000 ​  ​-1.8000 ​  ​-1.8000 ​  ​-1.8000 ​  ​-1.8000 +
-   ​-0.6000 ​  ​-0.6000 ​  ​-0.6000 ​  ​-0.6000 ​  ​-0.6000 +
-    0.6000 ​   0.6000 ​   0.6000 ​   0.6000 ​   0.6000 +
-    1.8000 ​   1.8000 ​   1.8000 ​   1.8000 ​   1.8000 +
-    3.0000 ​   3.0000 ​   3.0000 ​   3.0000 ​   3.0000 +
- +
-+
- +
-X = +
- +
-    -2    -1     ​0 ​    ​1 ​    2 +
-    -2    -1     ​0 ​    ​1 ​    2 +
-    -2    -1     ​0 ​    ​1 ​    2 +
-    -2    -1     ​0 ​    ​1 ​    2 +
-    -2    -1     ​0 ​    ​1 ​    2 +
-    -2    -1     ​0 ​    ​1 ​    2 +
- +
-+
- +
-Y = +
- +
-   ​-3.0000 ​  ​-3.0000 ​  ​-3.0000 ​  ​-3.0000 ​  ​-3.0000 +
-   ​-1.8000 ​  ​-1.8000 ​  ​-1.8000 ​  ​-1.8000 ​  ​-1.8000 +
-   ​-0.6000 ​  ​-0.6000 ​  ​-0.6000 ​  ​-0.6000 ​  ​-0.6000 +
-    0.6000 ​   0.6000 ​   0.6000 ​   0.6000 ​   0.6000 +
-    1.8000 ​   1.8000 ​   1.8000 ​   1.8000 ​   1.8000 +
-    3.0000 ​   3.0000 ​   3.0000 ​   3.0000 ​   3.0000 +
- +
-% Now use componentwise arithmetic to generate ​z=f(x,y) +
-Z = 0.5*X.^2 + Y.^2 +
- +
-Z = +
- +
-   ​11.0000 ​   9.5000 ​   9.0000 ​   9.5000 ​  ​11.0000 +
-    5.2400 ​   3.7400 ​   3.2400 ​   3.7400 ​   5.2400 +
-    2.3600 ​   0.8600 ​   0.3600 ​   0.8600 ​   2.3600 +
-    2.3600 ​   0.8600 ​   0.3600 ​   0.8600 ​   2.3600 +
-    5.2400 ​   3.7400 ​   3.2400 ​   3.7400 ​   5.2400 +
-   ​11.0000 ​   9.5000 ​   9.0000 ​   9.5000 ​  ​11.0000 +
- +
-% simplest 3D plot is a contour plot +
-contour(X,Y,Z) +
-% contour plot shows levels ​curves, Z = constant +
-y = linspace(-3,3,20) +
- +
-+
- +
-  Columns 1 through 7 +
- +
-   ​-3.0000 ​  ​-2.6842 ​  ​-2.3684 ​  ​-2.0526 ​  ​-1.7368 ​  ​-1.4211 ​  ​-1.1053 +
- +
-  Columns 8 through 14 +
- +
-   ​-0.7895 ​  ​-0.4737 ​  ​-0.1579 ​   0.1579 ​   0.4737 ​   0.7895 ​   1.1053 +
- +
-  Columns 15 through 20 +
- +
-    1.4211 ​   1.7368 ​   2.0526 ​   2.3684 ​   2.6842 ​   3.0000 +
- +
-x = linspace(-2,​2,​20) +
- +
-x = +
- +
-  Columns 1 through 7 +
- +
-   ​-2.0000 ​  ​-1.7895 ​  ​-1.5789 ​  ​-1.3684 ​  ​-1.1579 ​  ​-0.9474 ​  ​-0.7368 +
- +
-  Columns 8 through 14 +
- +
-   ​-0.5263 ​  ​-0.3158 ​  ​-0.1053 ​   0.1053 ​   0.3158 ​   0.5263 ​   0.7368 +
- +
-  Columns 15 through 20 +
- +
-    0.9474 ​   1.1579 ​   1.3684 ​   1.5789 ​   1.7895 ​   2.0000+
  
 +<code matlab>
 [X,Y] = meshgrid(x,​y);​ [X,Y] = meshgrid(x,​y);​
 Z = X.^2 + Y.^2; Z = X.^2 + Y.^2;
 contour(X,​Y,​Z) contour(X,​Y,​Z)
-xlabel('​x'​) +xlabel('​x'​)ylabel('​y'); title('​z= x^2 + y^2')
-ylabel('​y'​)+
 colorbar colorbar
-title('​z= x^2 + y^2') 
 axis equal  axis equal 
 axis tight axis tight
-contourf(X,Y,Z% filled contour plot +</​code>​ 
-axis equal +As you can see, the level curves of $z = f(x,y= x^2 + y^2$ are circles $x^2 + y^2 = c$. 
-axis tight +
-colorbar+
  
 +''​contourf''​ is the same as ''​contour'',​ except that the regions between contour lines are filled with color. ​
  
 +===== surf, surfc =====
  
-pcolor(X,Y,Z +The previous plots were all looking straight down at the (x,yplane, with the value of z = f(x,yencoded ​as a color. The ''​surf''​ function will plot z = f(x,y) in 3D, as a surface of height z over the (x,y) plane. ​
-pcolor(X,Y,Z% draw each matrix elem as a color +
-colorbar +
-shading flat % takes away grid lines +
-shading interp % interpolate shading+
  
- +<code matlab>
-% 3D graphics +
- +
-clf()+
 surf(X,Y,Z) % draw z=f(x,y) as a surface over x,y surf(X,Y,Z) % draw z=f(x,y) as a surface over x,y
-xlabel('​x'​) +xlabel('​x'​)ylabel('​y'​)zlabel('​z'​) 
-ylabel('​y'​) +axis equal; axis tight 
-zlabel('​z'​) +</​code>​
-shading flat +
-shading interp+
  
-% Parametric plot of sphere+It's also possible to draw more complicated surfaces (surfaces that are not simple graphs ​of the form 
 +$z = f(x,y)$). Here's an example of how to draw a sphere ​by parameterizing its surface in terms of angles $\phi$ and $\theta$. ​
  
 +<code matlab>
 % make mesh over theta, phi % make mesh over theta, phi
-theta = linspace(0,​2*pi,​50);​ +theta = linspace(0,​2*pi,​50); ​% angle between x and y 
-phi = linspace(0,​pi,​25);​ +phi   ​= linspace(0,​pi,​25); ​  ​angle down from z axis
- +
-those are vectors, transform to matrix mesh +
-[Theta, Phi] = meshgrid[theta,​ phi]; +
- ​[Theta,​ Phi] = meshgrid[theta,​ phi]; +
-                        | +
-Error: Unbalanced or unexpected parenthesis or bracket.+
    
-[Theta, Phi] = meshgrid(theta,​ phi);+[Theta, Phi] = meshgrid(theta,​ phi); % form 2D mesh in theta, phi 
 + 
 +% Parameterize surface of sphere in terms of theta, phi 
 +% (note that x^2 + y^2 + z^2 = 1)
 X = cos(Theta).*sin(Phi);​ X = cos(Theta).*sin(Phi);​
 Y = sin(Theta).*sin(Phi);​ Y = sin(Theta).*sin(Phi);​
-Z = cos(Phi);+Z = cos(Phi); ​    ​
  
 % Draw parametrized surface of sphere with surf % Draw parametrized surface of sphere with surf
 surf(X,​Y,​Z);​ axis equal surf(X,​Y,​Z);​ axis equal
 xlabel('​x'​);​ ylabel('​y'​);​ zlabel('​z'​) xlabel('​x'​);​ ylabel('​y'​);​ zlabel('​z'​)
- 
-% use extra variable C for color coding 
-C = X; 
-surf(X,​Y,​Z,​C);​ axis equal 
-xlabel('​x'​);​ ylabel('​y'​);​ zlabel('​z'​) 
-C = sin(X); 
-surf(X,​Y,​Z,​C);​ axis equal 
- 
-% surfc : surfaceplot with contour on x,y 
-surfc(X,​Y,​Z,​C);​ axis equal 
- 
- 
-% quiver plot 
-% used to show vector fields 
-load y.asc 
-load x.asc 
-load vx.asc 
-load vy.asc 
-clf() 
-quiver(x,​y,​vx,​vy); ​ 
 axis equal axis equal
-axis tight +</​code>​
-xlabel('​x'​);​ ylabel('​y'​);​ +
-title('​vector field vx,vy over plane x,y')+
  
  
-% subplots 
-% make an array of plots within a figure window 
-clf() 
-subplot(2,​2,​1) % 1st subplot in 2 x 2 array 
-quiver(x,​y,​vx,​vy);​ 
-axis equal 
-axis tight 
-subplot(2,​2,​2) % 2nd subplot in 2 x 2 array 
- 
-surfc(X,​Y,​Z,​C);​ axis equal 
-subplot(2,​2,​3) % 2nd subplot in 2 x 2 array 
-surf(X,​Y,​Z,​Z);​ axis equal 
-subplot(2,​2,​4) % 4th 
-x = linspace(0,​pi,​20);​ 
  
gibson/teaching/fall-2014/math445/lecture16-diary.1416322439.txt.gz · Last modified: 2014/11/18 06:53 by gibson