{"id":53,"date":"2017-07-08T18:35:01","date_gmt":"2017-07-09T01:35:01","guid":{"rendered":"http:\/\/blogs.reed.edu\/projectproject\/?p=53"},"modified":"2017-07-11T14:40:13","modified_gmt":"2017-07-11T21:40:13","slug":"visualizing-multivariable-functions-and-their-derivative","status":"publish","type":"post","link":"https:\/\/blogs.reed.edu\/projectproject\/2017\/07\/08\/visualizing-multivariable-functions-and-their-derivative\/","title":{"rendered":"Visualizing multivariable functions and their derivative"},"content":{"rendered":"<p>In a first course in calculus, many students encounter an image similar to the following:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-80\" src=\"http:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/tangent-300x214.png\" alt=\"\" width=\"300\" height=\"214\" srcset=\"https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/tangent-300x214.png 300w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/tangent-768x547.png 768w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/tangent-1024x729.png 1024w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/tangent-1200x854.png 1200w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><br \/>\nSuch an illustration highlights a key property of the single variable derivative: it&#8217;s the <i>best linear approximation<\/i> of a function at a point. For functions of more than one variable, the derivative exhibits this same characteristic, yet there is no obvious corresponding picture. What would an analogous visualization look like for a multivariable function?<\/p>\n<p>For the past few weeks, I&#8217;ve been working towards a visualization of multivariable functions and their derivatives. Check out the end result <a href=\"http:\/\/demo.chrishenn.net\">here<\/a>, or read on to hear about my process. I assume some knowledge of calculus and mathematical notation.<\/p>\n<p><!--more--><\/p>\n<h2>Visualizing vector fields in the plane<\/h2>\n<p>To make matters simple, I narrowed my focus to functions [latex]f : \\mathbf{R}^2 \\to \\mathbf{R}^2[\/latex]. The derivative of such a function is also a transform [latex]\\mathbf{R}^2 \\to \\mathbf{R}^2[\/latex]. Thus to build a visual representation of the derivative, I first needed a general purpose visualization of vector fields in the plane.<\/p>\n<p>Consider a particular function [latex]f : \\mathbf{R}^2 \\to \\mathbf{R^2}[\/latex] given by<br \/>\n$$<br \/>\nf(x,\\ y) = \\left( \\frac{x^3 + y^3}{3}, \\frac{x^3}{3} &#8211; y \\right)<br \/>\n$$<br \/>\nWhat does [latex]f[\/latex] look like? A common representation of such functions selects a uniformly-spaced set of points in [latex]\\mathbf{R}^2[\/latex], and draws an arrow at each point representing the magnitude and direction of the vector field:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-81\" src=\"http:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/vector-field-300x298.png\" alt=\"\" width=\"300\" height=\"298\" srcset=\"https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/vector-field-300x298.png 300w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/vector-field-150x150.png 150w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/vector-field-768x764.png 768w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/vector-field-1024x1019.png 1024w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/vector-field-1200x1194.png 1200w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/vector-field.png 1496w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><br \/>\nThis is sometimes called a <b>vector plot<\/b>. For static mediums like a textbook or chalk board, this is an intuitive visualization of [latex]f[\/latex]. However, I wanted to make use of additional visual techniques made possible with computer graphics. I came up with the following:<\/p>\n<ol>\n<li>Draw a line in [latex]\\mathbf{R}^2[\/latex].<\/li>\n<li>Consider the line as a discrete collection of points (a <b>polyline<\/b>).<\/li>\n<li>Apply a function [latex]\\mathbf{R}^2 \\to \\mathbf{R}^2[\/latex] to those points.<\/li>\n<li>Draw a new line through the transformed points.<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-82\" src=\"http:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/viz-steps-300x153.png\" alt=\"\" width=\"300\" height=\"153\" srcset=\"https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/viz-steps-300x153.png 300w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/viz-steps-768x393.png 768w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/viz-steps.png 1003w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><br \/>\nPerforming those steps on a grid of lines in [latex][-2,2] \\times [-2,2][\/latex] with the function [latex]f[\/latex] from above produces the following visual:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-77\" src=\"http:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/grid-and-transform-300x167.png\" alt=\"\" width=\"300\" height=\"167\" srcset=\"https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/grid-and-transform-300x167.png 300w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/grid-and-transform-768x427.png 768w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/grid-and-transform-1024x569.png 1024w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/grid-and-transform-1200x667.png 1200w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/grid-and-transform.png 1694w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><br \/>\nNeat-o! The transformed grid gives some sense of how [latex]f[\/latex] deforms and stretches the Euclidean plane. As another example, the linear map given by<br \/>\n$$<br \/>\nA = \\begin{pmatrix}<br \/>\n2 &amp; 1 \\\\<br \/>\n0 &amp; 2<br \/>\n\\end{pmatrix} \\in \\textrm{Mat}_{2 \\times 2}(\\mathbf{R})<br \/>\n$$<br \/>\nyields the following picture when applied to a grid around the origin:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-78\" src=\"http:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/grid-transformed-linear-300x107.png\" alt=\"\" width=\"300\" height=\"107\" srcset=\"https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/grid-transformed-linear-300x107.png 300w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/grid-transformed-linear-768x273.png 768w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/grid-transformed-linear-1024x364.png 1024w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/grid-transformed-linear-1200x427.png 1200w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/grid-transformed-linear.png 1563w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><br \/>\nAs one might expect, the linear map sends linear subspaces to linear subspaces (straight lines to straight lines). The visualization has a rather vivid aesthetic&#8212;there is no curvature in the transformed result. This will be helpful for understanding the multivariable derivative, which is always a linear map.<\/p>\n<p>To give the visualization more <a href=\"https:\/\/bost.ocks.org\/mike\/constancy\/\">object constancy<\/a>, I animate between the starting and ending states of each line as well. As a fun aside, the data of such an animation are computed by what mathematicians call a <b>straight line homotopy<\/b>; as a coder, this is a tween function of SVG <code>path<\/code> elements. If you would like to peek under the hood of the visualization, be sure to check out the main <a href=\"https:\/\/d3js.org\/\">d3<\/a>-based drawing method <a href=\"https:\/\/github.com\/chnn\/multivariable-derivative-viz\/blob\/a3f0f96610475006b6491c75c473ecda03a784de\/app\/components\/grid-plot\/component.js#L68-L110\">here<\/a>.<\/p>\n<h2>The multivariable derivative<\/h2>\n<p>Inspired by the common single variable visualization of the derivative, I wanted to plot functions [latex]f : \\mathbf{R}^2 \\to \\mathbf{R}^2[\/latex] alongside a derivative-based approximation function. What does this approximation look like in the multivariable case?<\/p>\n<p>If it exists, the derivative of a multivariable function [latex]f : \\mathbf{R}^n \\to \\mathbf{R}^m[\/latex] at a point [latex]\\mathbf{a}[\/latex] is a linear function [latex]T[\/latex] such that [latex]h : \\mathbf{R}^n \\to \\mathbf{R}^m[\/latex] given by<br \/>\n$$<br \/>\nh(\\mathbf{x}) = f(\\mathbf{a}) + T(\\mathbf{x &#8211; a})<br \/>\n$$<br \/>\napproximates [latex]f[\/latex] well near [latex]\\mathbf{a}[\/latex]. More precisely, we require that<br \/>\n$$<br \/>\n\\lim_{\\mathbf{x} \\to \\mathbf{a}} \\frac{| f(\\mathbf{x}) &#8211; h(\\mathbf{x}) |}{| \\mathbf{x &#8211; a} |} = 0<br \/>\n$$<br \/>\nIf such a linear transform [latex]T[\/latex] exists, it is unique and is given by the <b>Jacobian matrix of partial derivatives<\/b><\/p>\n<p>$$<br \/>\nDf(x_1,\\ x_2,\\ \\ldots,\\ x_n) := \\begin{pmatrix}<br \/>\n\\frac{\\partial f_1}{\\partial x_1} &amp; \\frac{\\partial f_1}{\\partial x_2 } &amp; \\ldots &amp; \\frac{\\partial f_1}{\\partial x_n} \\\\[1.1em]<br \/>\n\\frac{\\partial f_2}{\\partial x_1} &amp; \\frac{\\partial f_2}{\\partial x_2 } &amp; \\ldots &amp; \\frac{\\partial f_2}{\\partial x_n} \\\\[1.1em]<br \/>\n\\vdots &amp; \\vdots &amp; \\ddots &amp; \\vdots \\\\[0.4em]<br \/>\n\\frac{\\partial f_m}{\\partial x_1} &amp; \\frac{\\partial f_m}{\\partial x_2} &amp; \\ldots &amp; \\frac{\\partial f_m}{\\partial x_n}<br \/>\n\\end{pmatrix}<br \/>\n$$<\/p>\n<p>Suppose we choose [latex]\\mathbf{a} = (1.8,\\ 1.4)[\/latex] and compute [latex]h[\/latex] for the particular [latex]f[\/latex] given before. Plotting both [latex]h[\/latex] and [latex]f[\/latex] together (with [latex]h[\/latex] in green) yields the following visual:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-79\" src=\"http:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/grid-transformed-overlaid-300x250.png\" alt=\"\" width=\"300\" height=\"250\" srcset=\"https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/grid-transformed-overlaid-300x250.png 300w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/grid-transformed-overlaid-768x640.png 768w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/grid-transformed-overlaid-1024x854.png 1024w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/grid-transformed-overlaid-1200x1000.png 1200w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/grid-transformed-overlaid.png 1875w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><br \/>\nImmediately we can see the essential properties of the derivative: near the chosen point [latex]\\mathbf{a}[\/latex], the function [latex]h[\/latex] closely approximates [latex]f[\/latex]. Moreover, this approximation is linear; the grid transformed by [latex]h[\/latex] consists only of straight lines, indicating that it is a linear function. Be sure to check out the <a href=\"http:\/\/demo.chrishenn.net\">full animated version<\/a> of this visualization to see different functions at work!<\/p>\n<h2>Extending the visualization to complex functions<\/h2>\n<p>Conveniently, this visualization can also be adapted to visualizing functions [latex]\\mathbf{C} \\to \\mathbf{C}[\/latex]. Just like functions of real numbers, complex functions can be differentiated and have an approximation function<br \/>\n$$<br \/>\nh(z) = f(a) + f'(a)(z &#8211; a)<br \/>\n$$<br \/>\nwhere [latex]a,\\ z \\in \\mathbf{C}[\/latex] and [latex]f'[\/latex] is the derivative of [latex]f[\/latex].<\/p>\n<p>Typically a point in the complex plane is written as [latex]a + bi[\/latex] for some [latex]a,\\ b \\in \\mathbf{R}[\/latex]. We could alternatively notate this point as [latex](a, b)[\/latex], which looks just like a point in [latex]\\mathbf{R}^2[\/latex]! These points operate under a different arithmetic, but can be fed to the same visualization algorithm. In this case, we get a depiction of the points on the real-imaginary plane. Here, for example, is a visualization of the complex exponential function [latex]f(z) = e^z[\/latex] and its derivative:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-76\" src=\"http:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/complex-exponential-300x200.png\" alt=\"\" width=\"300\" height=\"200\" srcset=\"https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/complex-exponential-300x200.png 300w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/complex-exponential-768x512.png 768w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/complex-exponential-1024x683.png 1024w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/complex-exponential-1200x800.png 1200w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/complex-exponential.png 1875w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><\/p>\n<h2>Closing thoughts<\/h2>\n<p>Next up I&#8217;m planning a 3D-printable version of this same visualization. The idea is to perform similar deformation of a lattice in [latex]\\mathbf{R}^3[\/latex].<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75\" src=\"http:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/3d-lattice.jpg\" alt=\"\" width=\"576\" height=\"454\" srcset=\"https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/3d-lattice.jpg 576w, https:\/\/blogs.reed.edu\/projectproject\/files\/2017\/07\/3d-lattice-300x236.jpg 300w\" sizes=\"auto, (max-width: 576px) 85vw, 576px\" \/><br \/>\nMore on this soon!<\/p>\n<p>For the curious or computer-minded, the code for this visualization is <a href=\"https:\/\/github.com\/chnn\/multivariable-derivative-viz\">on GitHub<\/a>. Highlights include a <a href=\"https:\/\/github.com\/chnn\/multivariable-derivative-viz\/blob\/a3f0f96610475006b6491c75c473ecda03a784de\/app\/utils\/complex-numbers.js\">totally bonkers JavaScript implementation of complex arithmetic<\/a> or the main <a href=\"https:\/\/github.com\/chnn\/multivariable-derivative-viz\/blob\/a3f0f96610475006b6491c75c473ecda03a784de\/app\/components\/grid-plot\/component.js\">plot component<\/a>. I would also <a href=\"mailto:chris@chrishenn.net\">love to hear<\/a> any further ideas for visualization in this area.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a first course in calculus, many students encounter an image similar to the following: Such an illustration highlights a key property of the single variable derivative: it&#8217;s the best linear approximation of a function at a point. For functions of more than one variable, the derivative exhibits this same characteristic, yet there is no &hellip; <a href=\"https:\/\/blogs.reed.edu\/projectproject\/2017\/07\/08\/visualizing-multivariable-functions-and-their-derivative\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Visualizing multivariable functions and their derivative&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1575,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[6,4,7,5,8],"class_list":["post-53","post","type-post","status-publish","format-standard","hentry","category-vector-calc","tag-calculus","tag-derivative","tag-math","tag-multivariable","tag-visualization"],"_links":{"self":[{"href":"https:\/\/blogs.reed.edu\/projectproject\/wp-json\/wp\/v2\/posts\/53","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.reed.edu\/projectproject\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.reed.edu\/projectproject\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.reed.edu\/projectproject\/wp-json\/wp\/v2\/users\/1575"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.reed.edu\/projectproject\/wp-json\/wp\/v2\/comments?post=53"}],"version-history":[{"count":19,"href":"https:\/\/blogs.reed.edu\/projectproject\/wp-json\/wp\/v2\/posts\/53\/revisions"}],"predecessor-version":[{"id":195,"href":"https:\/\/blogs.reed.edu\/projectproject\/wp-json\/wp\/v2\/posts\/53\/revisions\/195"}],"wp:attachment":[{"href":"https:\/\/blogs.reed.edu\/projectproject\/wp-json\/wp\/v2\/media?parent=53"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.reed.edu\/projectproject\/wp-json\/wp\/v2\/categories?post=53"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.reed.edu\/projectproject\/wp-json\/wp\/v2\/tags?post=53"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}