This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
gibson:teaching:spring-2016:math445:lecture:graphics3d [2016/03/29 08:27] gibson [1d graphs] |
gibson:teaching:spring-2016:math445:lecture:graphics3d [2016/03/29 09:02] (current) gibson [Math 445 lecture 16: 3D graphics] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Math 445 lecture 16: 3D graphics ====== | + | ====== Math 445 lecture 16: 2D, 3D graphics ====== |
+ | Some examples of plotting 2d functions $f(x,y)$ in Matlab. | ||
+ | |||
+ | ---- | ||
==== 1d graphs ==== | ==== 1d graphs ==== | ||
Line 7: | Line 10: | ||
How would you make a graph of $f(x) = (x-1)^2$ on $-3 \leq x \leq 3$ in Matlab? You would | How would you make a graph of $f(x) = (x-1)^2$ on $-3 \leq x \leq 3$ in Matlab? You would | ||
- Create vector ''x'' of gridpoints with ''linspace'' | - Create vector ''x'' of gridpoints with ''linspace'' | ||
- | - Evaluate $f(x)$ on gridpoints using dot operations | + | - Evaluate $f(x)$ on gridpoints using dot operations on ''x'' |
- Use ''plot(x,f)'' to draw the graph | - Use ''plot(x,f)'' to draw the graph | ||
Line 21: | Line 24: | ||
ylabel('f(x) = (x-1)^2') | ylabel('f(x) = (x-1)^2') | ||
</file> | </file> | ||
+ | produces | ||
+ | {{ :gibson:teaching:spring-2016:math445:onedplot.png?direct&400 |}} | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ==== 2d graphs with meshgrid ==== | ||
+ | |||
+ | Graphing a 2d function $f(x,y)$ is very similar. For example, to make a graph of the function | ||
+ | $f(x,y) = 2*(x-1)^2 + (y-1)^2$ on $-4 \leq x \leq 4, -4 \leq y \leq 4$ | ||
+ | |||
+ | - Create vectors ''x'' and ''y'' of gridpoints on $x$ and $y$ axes with ''linspace'' | ||
+ | - Create matrices ''X'' and ''Y'' of gridpoints on 2d mesh with ''meshgrid'' | ||
+ | - Evaluate $f(x,y)$ on mesh of gridpoints using dot operations on ''X,Y'' | ||
+ | - Use ''pcolor(x,y,f)'' to draw the graph (or ''contour'', ''contourf'', ''surf'', ''surfc'') | ||
+ | |||
+ | For example, | ||
+ | <file matlab plot2d.m> | ||
+ | % Plot f(x,y) = 2*(x-1)^2 + (y-1)^2 on -4 < x < 4, -4 < y < 4 | ||
+ | |||
+ | x = linspace(-4,4,30); % create 1d gridpoints on x and y axes | ||
+ | y = linspace(-4,4,30); | ||
+ | |||
+ | [X,Y] = meshgrid(x,y); % create 2d mesh of gridpoints over x,y | ||
+ | |||
+ | F = 2*(X-1).^2 + (Y-1).^2; % evaluate the function f(x,y) over mesh | ||
+ | |||
+ | pcolor(x,y,F); % or contour(X,Y,F), surf(x,y,F), etc. | ||
+ | colorbar() % plot a colorbar | ||
+ | |||
+ | xlabel('x'); | ||
+ | ylabel('y'); | ||
+ | title('f(x,y) = 2(x-1)^2 + (y-1)^2') | ||
+ | |||
+ | shading interp % or faceted, flat | ||
+ | </file> | ||
+ | |||
+ | {{ :gibson:teaching:spring-2016:math445:lecture:twodplot.png?direct&400 |}} | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ==== quiver plots and the gradient=== | ||
+ | |||
+ | The function $f(x,y) = x e^{-x^2 - y^2}$ has gradient | ||
+ | |||
+ | \begin{eqnarray*} | ||
+ | \nabla f = \left( \begin{array}{c} \partial f/\partial x \\ \partial f/\partial y \end{array} \right) = \left( \begin{array}{c} (1-x^2) e^{-x^2 - y^2} \\ -2xy e^{-x^2 - y^2} \end{array} \right) | ||
+ | \end{eqnarray*} | ||
+ | |||
+ | Here's the Matlab code to make a contour plot of $f(x,y)$ with its gradient superimposed as a quiver plot. | ||
+ | |||
+ | <file matlab plotgradient.m> | ||
+ | % Plot f(x,y) = x e^(-x^2 - y^2) with contours and its gradient as arrows. | ||
+ | x = linspace(-2, 2, 30); | ||
+ | y = linspace(-2, 2, 30); | ||
+ | |||
+ | [X,Y] = meshgrid(x,y); | ||
+ | |||
+ | F = X .* exp(-X.^2 - Y.^2); % evaluate f(x,y) on mesh | ||
+ | contour(x,y,F); % draw contour plot of f | ||
+ | |||
+ | % let dfdy = y component of grad F = df/dy | ||
+ | |||
+ | dfdx = (1-2*X.^2) .* exp(-X.^2 - Y.^2); % evaluate dfdx on mesh | ||
+ | dfdy = -2*X.*Y .* exp(-X.^2 - Y.^2); % evaluate dfdy on mesh | ||
+ | |||
+ | hold on | ||
+ | quiver(x,y,dfdx,dfdy); % draw quiver plot | ||
+ | |||
+ | xlabel('x') | ||
+ | ylabel('y') | ||
+ | title('f(x,y) = x e^{-x^2 - y^2} with \nabla f arrow plot') | ||
+ | colorbar() | ||
+ | </file> | ||
+ | |||
+ | {{ :gibson:teaching:spring-2016:math445:lecture:quiver.png?direct&500 |}} |