{"id":3259,"date":"2018-04-30T12:00:45","date_gmt":"2018-04-30T17:00:45","guid":{"rendered":"https:\/\/blogs.mathworks.com\/cleve\/?p=3259"},"modified":"2018-04-27T19:40:51","modified_gmt":"2018-04-28T00:40:51","slug":"matlab-history-modern-matlab-part-2","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/cleve\/2018\/04\/30\/matlab-history-modern-matlab-part-2\/","title":{"rendered":"MATLAB History, Modern MATLAB, part 2"},"content":{"rendered":"<div class=\"content\"><!--introduction--><p>The ACM Special Interest Group on Programming Languages, SIGPLAN, expects to hold the fourth in a series of conferences on the History of Programming Languages in 2020, see <a href=\"https:\/\/hopl4.sigplan.org\/\">HOPL-IV<\/a>.  The first drafts of papers are to be submitted by August 2018.  That long lead time gives me the opportunity to write a detailed history of MATLAB. I plan to write the paper in sections, which I'll post in this blog as they are available.<\/p><p>This is the sixth such installment and the second of a three-part post about modern MATLAB.<\/p><p>MATLAB is not just a Matrix Laboratory any more. It has evolved over the past 35 years into a rich technical computing environment.  Here are some samples of the features now available.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#a6288cee-f3e1-4cc1-b2bd-d6bc5ec52e6f\">Handle Graphics&reg;<\/a><\/li><li><a href=\"#237648db-04bb-4228-9dfa-98c8e6f95c53\">Structures, 1996<\/a><\/li><li><a href=\"#d3446331-0daf-43b1-9679-e84e3d9022f4\">Table, 2013<\/a><\/li><li><a href=\"#08f4004e-5fe3-42d1-846c-3edfd0481b26\">Objects, 2008<\/a><\/li><li><a href=\"#113e3203-c2f1-41fd-b586-9f0393b1503a\">ODEs<\/a><\/li><li><a href=\"#1c95920e-8a97-4a41-b9b9-31722118670f\">Graphs, 2015<\/a><\/li><li><a href=\"#de11cbbf-cab4-4741-a36e-0854b2506516\">Image Processing Toolbox&#8482;<\/a><\/li><li><a href=\"#367543f3-fcbc-4647-8013-330b6e1c77a8\">Symbolic Math Toolbox&#8482;<\/a><\/li><li><a href=\"#317f50f0-6b46-4d40-955e-b4be0aa46de5\">Homework<\/a><\/li><\/ul><\/div><h4>Handle Graphics&reg;<a name=\"a6288cee-f3e1-4cc1-b2bd-d6bc5ec52e6f\"><\/a><\/h4><p>Handle Graphics has evolved over the years into a rich, tree-structured, object-oriented environment for producing publication quality plots and graphs.  Let's see just one example.<\/p><pre class=\"codeinput\">   sinc = @(x) sin(x).\/x;\r\n   x = (-5:.03:5)*pi;\r\n   px = plot(x,sinc( x));\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/cleve\/files\/modern2_blog_01.png\" alt=\"\"> <p>This is not ready yet for publication.  We need to fine tune the annotation.<\/p><pre class=\"codeinput\">   ax = px.Parent;\r\n   ax.XLim = pi*[-5 5];\r\n   ax.XTick = pi*(-4:2:4);\r\n   ax.XTickLabel = {<span class=\"string\">'-4\\pi'<\/span>,<span class=\"string\">'-2\\pi'<\/span>,<span class=\"string\">'0'<\/span>,<span class=\"string\">'2\\pi'<\/span>,<span class=\"string\">'4\\pi'<\/span>};\r\n   ax.YLim = [-0.3 1.1];\r\n   ax.Title.String = <span class=\"string\">'sinc(x)'<\/span>;\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/cleve\/files\/modern2_blog_02.png\" alt=\"\"> <p>This plot is now ready to be included in a textbook or expository paper. We created two Handle Graphics objects, <tt>px<\/tt> and <tt>ax<\/tt>. <tt>px<\/tt> is a <tt>line<\/tt> object with 38 properties. <tt>ax<\/tt> is an <tt>axes<\/tt> object with 137 properties. Setting a few of these properties to values other than their defaults improves the scaling and annotation.<\/p><h4>Structures, 1996<a name=\"237648db-04bb-4228-9dfa-98c8e6f95c53\"><\/a><\/h4><p>Structures, and associated \"dot notation\", were introduced in 1996. As we just saw, Handle Graphics now uses this notation. For another example, let's create a grade book for a small class.<\/p><pre class=\"codeinput\">   Math101.name = {<span class=\"string\">'Alice Jones'<\/span>;<span class=\"string\">'Bob Smith'<\/span>;<span class=\"string\">'Charlie Brown'<\/span>};\r\n   Math101.grade = {<span class=\"string\">'A-'<\/span>; <span class=\"string\">'B+'<\/span>; <span class=\"string\">'C'<\/span>};\r\n   Math101.year = [4; 2; 3]\r\n<\/pre><pre class=\"codeoutput\">Math101 = \r\n  struct with fields:\r\n\r\n     name: {3&times;1 cell}\r\n    grade: {3&times;1 cell}\r\n     year: [3&times;1 double]\r\n<\/pre><p>To call the roll, we need the list of names.<\/p><pre class=\"codeinput\">   disp(Math101.name)\r\n<\/pre><pre class=\"codeoutput\">    'Alice Jones'\r\n    'Bob Smith'\r\n    'Charlie Brown'\r\n<\/pre><p>Changing Charlie's grade involves both structure and array notation.<\/p><pre class=\"codeinput\">   Math101.grade(3) = {<span class=\"string\">'W'<\/span>};\r\n   disp(Math101.grade)\r\n<\/pre><pre class=\"codeoutput\">    'A-'\r\n    'B+'\r\n    'W'\r\n<\/pre><h4>Table, 2013<a name=\"d3446331-0daf-43b1-9679-e84e3d9022f4\"><\/a><\/h4><p>Tables, which were introduced in 2013, allow us to print the grade book in a more readable form.<\/p><pre class=\"codeinput\">   T = struct2table(Math101)\r\n<\/pre><pre class=\"codeoutput\">T =\r\n  3&times;3 table\r\n         name          grade       year   \r\n    _______________    _____    __________\r\n    'Alice Jones'      'A-'     4.0000e+00\r\n    'Bob Smith'        'B+'     2.0000e+00\r\n    'Charlie Brown'    'W'      3.0000e+00\r\n<\/pre><h4>Objects, 2008<a name=\"08f4004e-5fe3-42d1-846c-3edfd0481b26\"><\/a><\/h4><p>Major enhancements to the MATLAB object-oriented programming capabilities were made in 2008. Creating classes can simplify programming tasks that involve specialized data structures or large numbers of functions that interact with particular kinds of data. MATLAB classes support function and operator overloading, controlled access to properties and methods, reference and value semantics, and events and listeners.<\/p><p>Handle Graphics is one large, complex example of the object-oriented approach to MATLAB programming.<\/p><p>The <tt>datetime<\/tt> object, introduced in 2014, is another example. For many years, computations involving dates and time were done with using a collection of a dozen functions including <tt>clock<\/tt>, <tt>datenum<\/tt> and <tt>datestr<\/tt>.  The <tt>datetime<\/tt> object combines and extends the capabilities of all those functions.<\/p><p>Here is an unexpected use of <tt>datetime<\/tt>.  How long is a <i>microcentury<\/i>? Let's start with the date that I am publishing this post.<\/p><pre class=\"codeinput\">   t1 = datetime(<span class=\"string\">'today'<\/span>)\r\n<\/pre><pre class=\"codeoutput\">t1 = \r\n  datetime\r\n   27-Apr-2018\r\n<\/pre><p>Add 100 years.<\/p><pre class=\"codeinput\">   [y,m,d] = ymd(t1);\r\n   t2 = datetime(y+100,m,d)\r\n<\/pre><pre class=\"codeoutput\">t2 = \r\n  datetime\r\n   27-Apr-2118\r\n<\/pre><p>A century is the difference between these two times.  This difference is a <tt>duration<\/tt> object, expressed in hours:minutes:seconds.<\/p><pre class=\"codeinput\">   c = t2 - t1\r\n<\/pre><pre class=\"codeoutput\">c = \r\n  duration\r\n   876576:00:00\r\n<\/pre><p>Here is a microcentury, in the same units.<\/p><pre class=\"codeinput\">   microc = 1.e-6*c\r\n<\/pre><pre class=\"codeoutput\">microc = \r\n  duration\r\n   00:52:35\r\n<\/pre><p>So, a microcentury is 52 minutes and 35 seconds.  The optimum time for an academic lecture.<\/p><p>But wait -- some centuries are longer than most, because they have four leap years, not the usual three.  The year 2100, which we have just included in our century, will not be a leap year, On the other hand, the year 2000, which is divisible by 400, was a leap year.  We need more precision.<\/p><pre class=\"codeinput\">   microcentury_short = duration(microc, <span class=\"string\">'Format'<\/span>,<span class=\"string\">'hh:mm:ss.SSS'<\/span>)\r\n<\/pre><pre class=\"codeoutput\">microcentury_short = \r\n  duration\r\n   00:52:35.673\r\n<\/pre><p>Subtract, rather than add, 100 years.<\/p><pre class=\"codeinput\">   t0 = datetime(y-100,m,d);\r\n   microcentury_long = 1.e-6*duration(t1-t0, <span class=\"string\">'Format'<\/span>,<span class=\"string\">'hh:mm:ss.SSS'<\/span>)\r\n<\/pre><pre class=\"codeoutput\">microcentury_long = \r\n  duration\r\n   00:52:35.760\r\n<\/pre><h4>ODEs<a name=\"113e3203-c2f1-41fd-b586-9f0393b1503a\"><\/a><\/h4><p>The numerical solution of ordinary differential equations has been a vital part of MATLAB since its commercial beginning. ODEs are the heart of Simulink&reg;, MATLAB's companion product.<\/p><p>The Van der Pol oscillator is a classical ODE example.<\/p><p>$$ \\ddot{y} = \\mu (1-y^2) \\dot{y} - y$$<\/p><p>The parameter $\\mu$ is the strength of the nonlinear damping term. When $\\mu = 0$, we have the basic harmonic oscillator.<\/p><p>The MATLAB code expresses the oscillator as a pair of first order equations.<\/p><pre class=\"codeinput\">    mu = 5;\r\n    vdp = @(t,y) [0 1; -1 mu*(1-y(1)^2)]*y;\r\n    tspan = [0 30];\r\n    y0 = [0 0.01]';\r\n<\/pre><pre class=\"codeinput\">    [t,y] = ode23s(vdp,tspan,y0);\r\n<\/pre><pre class=\"codeinput\">    plot(t,y,<span class=\"string\">'.-'<\/span>)\r\n    legend({<span class=\"string\">'y'<\/span>,<span class=\"string\">'dy\/dt'<\/span>})\r\n    xlabel(<span class=\"string\">'t'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/cleve\/files\/modern2_blog_03.png\" alt=\"\"> <p>The Van der Pol oscillator, with the parameter <tt>mu<\/tt> set to a value of 5, is a mildly <i>stiff<\/i> differential equation. In anticipation, I used the <tt>ode23s<\/tt> solver; the <tt>'s'<\/tt> in the name indicates it is for stiff equations.  In the plot you can see some clustering of steps where the solution is varying rapidly.  A nonstiff solver would have taken many more steps.<\/p><p>A stiff ode solver uses an <i>implicit<\/i> method requiring the solution of a set of simultaneous linear equations at each step. The iconic MATLAB backslash operator is quietly at work here.<\/p><h4>Graphs, 2015<a name=\"1c95920e-8a97-4a41-b9b9-31722118670f\"><\/a><\/h4><p>Support for <i>graph theory<\/i> was added in 2015. In this context, a <i>graph<\/i> is a set of nodes or vertices, together with a set of edges or lines between the nodes. There are two kinds of graphs. A <tt>graph<\/tt> is <i>undirected<\/i> if an edge between two nodes means that the nodes are connected to each other.  A <tt>digraph<\/tt> is <i>directed<\/i> if the connections are one-way.  Edges can have different weights, or all the weights can be the same.<\/p><pre class=\"codeinput\">   G = digraph(1,2:5);\r\n   G = addedge(G,2,6:15);\r\n   G = addedge(G,15,16:20)\r\n<\/pre><pre class=\"codeoutput\">G = \r\n  digraph with properties:\r\n\r\n    Edges: [19&times;1 table]\r\n    Nodes: [20&times;0 table]\r\n<\/pre><p>Graphs have no inherent geometric structure.  The tricky part of plotting a graph is locating the nodes in two- or three-dimensional space.  The graph plot methods offer several different layouts.<\/p><pre class=\"codeinput\">   plot(G,<span class=\"string\">'layout'<\/span>,<span class=\"string\">'force'<\/span>,<span class=\"string\">'nodecolor'<\/span>,burnt_orange)\r\n   no_ticks\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/cleve\/files\/modern2_blog_04.png\" alt=\"\"> <h4>Image Processing Toolbox&#8482;<a name=\"de11cbbf-cab4-4741-a36e-0854b2506516\"><\/a><\/h4><p>The Image Processing Toolbox has been available since the 1990's. It is now at Version 10.2.  The command<\/p><pre class=\"language-matlab\">help <span class=\"string\">images<\/span>\r\n<\/pre><p>lists over 500 functions. As expected, the toolbox makes extensive use of array functionality.<\/p><p>This image, \"Psychedelic Pluto\", combines image processing and numerical linear algebra.  The <a href=\"https:\/\/www.nasa.gov\/image-feature\/psychedelic-pluto\">NASA press release<\/a> says \"New Horizons scientists made this false color image of Pluto using a technique called <b>principal component analysis<\/b> (emphasis mine) to highlight the many subtle color differences between Pluto's distinct regions.\"  The image was presented by Will Grundy of the New Horizons&#8217; surface composition team on Nov. 9, 2015.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/cleve\/files\/psychedelic_pluto.png\" alt=\"\"> <\/p><p><i>Image Credit: NASA\/JHUAPL\/SwRI<\/i><\/p><p>Images of Pluto are of particular interest because MATLAB and Simulink <a href=\"https:\/\/blogs.mathworks.com\/steve\/2015\/07\/19\/new-horizons-pluto-program-used-matlab-and-image-processing-for-navigation\/\">were used extensively<\/a> in both navigation and the flight control system for the New Horizons flyby mission.<\/p><h4>Symbolic Math Toolbox&#8482;<a name=\"367543f3-fcbc-4647-8013-330b6e1c77a8\"><\/a><\/h4><p>The Symbolic Math Toolbox has also been available since the 1990's. It is now at version 8.1.  The command<\/p><pre class=\"language-matlab\">help <span class=\"string\">symbolic<\/span>\r\n<\/pre><p>lists almost 500 functions.<\/p><p>The toolbox provides functions for solving, plotting, and manipulating symbolic math equations.  The operations include differentiation, integration, simplification, transforms, and equation solving. Computations can be performed either analytically or using variable-precision arithmetic.<\/p><p>The toolbox has recently been integrated with the Live Editor, which I will discuss in my next blog post.  This example does not yet use the Live Editor.<\/p><p>The statement<\/p><pre class=\"codeinput\">    x = sym(<span class=\"string\">'x'<\/span>)\r\n<\/pre><pre class=\"codeoutput\">x =\r\nx\r\n<\/pre><p>creates a symbolic variable that just represents itself. The variable does not have a numeric value.  The statement<\/p><pre class=\"codeinput\">    f = 1\/(4*cos(x)+5)\r\n<\/pre><pre class=\"codeoutput\">f =\r\n1\/(4*cos(x) + 5)\r\n<\/pre><p>creates a symbolic function of that variable.<\/p><p>Without the Live Editor, the closest we can come to LaTeX style typeset mathematics in the command window is<\/p><pre class=\"codeinput\">    pretty(f)\r\n<\/pre><pre class=\"codeoutput\">      1\r\n------------\r\n4 cos(x) + 5\r\n\r\n<\/pre><p>Let's differentiate <tt>f<\/tt> twice.<\/p><pre class=\"codeinput\">    f2 = diff(f,2)\r\n<\/pre><pre class=\"codeoutput\">f2 =\r\n(4*cos(x))\/(4*cos(x) + 5)^2 + (32*sin(x)^2)\/(4*cos(x) + 5)^3\r\n<\/pre><p>And then integrate the result twice.  Do we get back to where we started?<\/p><pre class=\"codeinput\">g = int(int(f2))\r\n<\/pre><pre class=\"codeoutput\">g =\r\n-8\/(tan(x\/2)^2 + 9)\r\n<\/pre><p>No, <tt>f<\/tt> and <tt>g<\/tt> are not the same expression.  Let's plot both of them.<\/p><pre class=\"codeinput\">    subplot(211)\r\n    ezplot(f)\r\n    subplot(212)\r\n    ezplot(g)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/cleve\/files\/modern2_blog_05.png\" alt=\"\"> <p>The two plots have the same shape, but the y axes are not the same. Let's compute the difference; this is the \"error\" made by differentiating twice and then integrating twice.<\/p><pre class=\"codeinput\">    e = f - g\r\n<\/pre><pre class=\"codeoutput\">e =\r\n8\/(tan(x\/2)^2 + 9) + 1\/(4*cos(x) + 5)\r\n<\/pre><p>The error isn't zero.  What is it?  First a plot.<\/p><pre class=\"codeinput\">   half_fig\r\n   ezplot(e)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/cleve\/files\/modern2_blog_06.png\" alt=\"\"> <p>Now let's do the most difficult computation of this entire example.<\/p><pre class=\"codeinput\">    s = simplify(e)\r\n<\/pre><pre class=\"codeoutput\">s =\r\n1\r\n<\/pre><p>So, <tt>e<\/tt> is an elaborate way of writing the constant function, 1. That's the \"constant of integration\", the infamous +C that we always forget to include in indefinite integration.<\/p><h4>Homework<a name=\"317f50f0-6b46-4d40-955e-b4be0aa46de5\"><\/a><\/h4><p>Here's your homework for today.  Where did the limits on the y axis for <tt>ezplot(e)<\/tt> come from?<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_0bb836b832294d4388e8765cf8f4b69d() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='0bb836b832294d4388e8765cf8f4b69d ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 0bb836b832294d4388e8765cf8f4b69d';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2018 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_0bb836b832294d4388e8765cf8f4b69d()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2018a<br><\/p><\/div><!--\r\n0bb836b832294d4388e8765cf8f4b69d ##### SOURCE BEGIN #####\r\n%% MATLAB History, Modern MATLAB, part 2\r\n% The ACM Special Interest Group on Programming Languages, SIGPLAN,\r\n% expects to hold the fourth in a series of conferences on\r\n% the History of Programming Languages in 2020, see\r\n% <https:\/\/hopl4.sigplan.org\/ HOPL-IV>.  The first drafts of\r\n% papers are to be submitted by August 2018.  That long lead time\r\n% gives me the opportunity to write a detailed history of MATLAB.\r\n% I plan to write the paper in sections, which I'll post in\r\n% this blog as they are available.\r\n%\r\n% This is the sixth such installment and the second of a three-part\r\n% post about modern MATLAB.\r\n%\r\n% MATLAB is not just a Matrix Laboratory any more.\r\n% It has evolved over the past 35 years into a rich technical \r\n% computing environment.  Here are some samples of the features\r\n% now available.\r\n\r\n%% Handle Graphics(R)\r\n% Handle Graphics has evolved over the years into a rich, tree-structured,\r\n% object-oriented environment for producing publication quality plots \r\n% and graphs.  Let's see just one example.\r\n\r\n   sinc = @(x) sin(x).\/x;\r\n   x = (-5:.03:5)*pi;\r\n   px = plot(x,sinc( x));\r\n   \r\n%%\r\n% This is not ready yet for publication.  We need to fine tune the\r\n% annotation.\r\n\r\n   ax = px.Parent;\r\n   ax.XLim = pi*[-5 5];\r\n   ax.XTick = pi*(-4:2:4);\r\n   ax.XTickLabel = {'-4\\pi','-2\\pi','0','2\\pi','4\\pi'};\r\n   ax.YLim = [-0.3 1.1];\r\n   ax.Title.String = 'sinc(x)';\r\n   \r\n%%\r\n% This plot is now ready to be included in a textbook or \r\n% expository paper.\r\n% We created two Handle Graphics objects, |px| and |ax|.\r\n% |px| is a |line| object with 38 properties.\r\n% |ax| is an |axes| object with 137 properties.\r\n% Setting a few of these properties to values other than their defaults\r\n% improves the scaling and annotation.  \r\n\r\n%% Structures, 1996\r\n% Structures, and associated \"dot notation\", were introduced in 1996.\r\n% As we just saw, Handle Graphics now uses this notation.\r\n% For another example, let's create a grade book for a small class.\r\n\r\n   Math101.name = {'Alice Jones';'Bob Smith';'Charlie Brown'};\r\n   Math101.grade = {'A-'; 'B+'; 'C'};\r\n   Math101.year = [4; 2; 3]\r\n   \r\n%%\r\n% To call the roll, we need the list of names.\r\n\r\n   disp(Math101.name)\r\n   \r\n%%\r\n% Changing Charlie's grade involves both structure and array notation.\r\n\r\n   Math101.grade(3) = {'W'};\r\n   disp(Math101.grade)\r\n   \r\n%% Table, 2013\r\n% Tables, which were introduced in 2013, allow us to print the grade book\r\n% in a more readable form.\r\n\r\n   T = struct2table(Math101)\r\n\r\n%% Objects, 2008\r\n% Major enhancements to the MATLAB object-oriented programming\r\n% capabilities were made in 2008.\r\n% Creating classes can simplify programming tasks that involve \r\n% specialized data structures or large numbers of functions that\r\n% interact with particular kinds of data.\r\n% MATLAB classes support function and operator overloading, controlled\r\n% access to properties and methods, reference and value semantics,\r\n% and events and listeners.\r\n%\r\n% Handle Graphics is one large, complex example of the object-oriented\r\n% approach to MATLAB programming.\r\n%\r\n% The |datetime| object, introduced in 2014, is another example.\r\n% For many years, computations involving dates and time were done with\r\n% using a collection of a dozen functions including |clock|,\r\n% |datenum| and |datestr|.  The |datetime| object combines and extends\r\n% the capabilities of all those functions.\r\n\r\n%%\r\n% Here is an unexpected use of |datetime|.  How long is a _microcentury_?\r\n% Let's start with the date that I am publishing this post.\r\n\r\n   t1 = datetime('today')\r\n   \r\n%%\r\n% Add 100 years.\r\n\r\n   [y,m,d] = ymd(t1);\r\n   t2 = datetime(y+100,m,d)\r\n   \r\n%%\r\n% A century is the difference between these two times.  This difference\r\n% is a |duration| object, expressed in hours:minutes:seconds.\r\n\r\n   c = t2 - t1\r\n   \r\n%%\r\n% Here is a microcentury, in the same units.\r\n\r\n   microc = 1.e-6*c\r\n   \r\n%%\r\n% So, a microcentury is 52 minutes and 35 seconds.  The optimum time\r\n% for an academic lecture.\r\n\r\n%%\r\n% But wait REPLACE_WITH_DASH_DASH some centuries are longer than most, because they\r\n% have four leap years, not the usual three.  The year 2100,\r\n% which we have just included in our century, will not be a leap year,\r\n% On the other hand, the year 2000, which is divisible by 400,\r\n% was a leap year.  We need more precision.\r\n\r\n   microcentury_short = duration(microc, 'Format','hh:mm:ss.SSS')\r\n   \r\n%%\r\n% Subtract, rather than add, 100 years.\r\n\r\n   t0 = datetime(y-100,m,d);\r\n   microcentury_long = 1.e-6*duration(t1-t0, 'Format','hh:mm:ss.SSS')\r\n   \r\n%% ODEs\r\n% The numerical solution of ordinary differential equations has been\r\n% a vital part of MATLAB since its commercial beginning.\r\n% ODEs are the heart of Simulink(R), MATLAB's companion product.\r\n%\r\n% The Van der Pol oscillator is a classical ODE example.\r\n%\r\n% $$ \\ddot{y} = \\mu (1-y^2) \\dot{y} - y$$\r\n%\r\n% The parameter $\\mu$ is the strength of the nonlinear damping term.\r\n% When $\\mu = 0$, we have the basic harmonic oscillator.\r\n\r\n%%\r\n% The MATLAB code expresses the oscillator as a pair of first\r\n% order equations.\r\n\r\n    mu = 5;\r\n    vdp = @(t,y) [0 1; -1 mu*(1-y(1)^2)]*y;\r\n    tspan = [0 30];\r\n    y0 = [0 0.01]';       \r\n    \r\n%%\r\n\r\n    [t,y] = ode23s(vdp,tspan,y0);\r\n\r\n%%\r\n    \r\n    plot(t,y,'.-')\r\n    legend({'y','dy\/dt'})\r\n    xlabel('t')\r\n    \r\n%%\r\n% The Van der Pol oscillator, with the parameter |mu| set\r\n% to a value of 5, is a mildly _stiff_ differential equation.\r\n% In anticipation, I used the |ode23s| solver; the |'s'| in the name\r\n% indicates it is for stiff equations.  In the plot you can see some\r\n% clustering of steps where the solution is varying rapidly.  A nonstiff\r\n% solver would have taken many more steps.\r\n\r\n%%\r\n% A stiff ode solver uses an _implicit_ method requiring the solution\r\n% of a set of simultaneous linear equations at each step. The iconic\r\n% MATLAB backslash operator is quietly at work here.\r\n\r\n%% Graphs, 2015\r\n% Support for _graph theory_ was added in 2015.\r\n% In this context, a _graph_ is a set of nodes or vertices,\r\n% together with a set of edges or lines between the nodes.\r\n% There are two kinds of graphs.\r\n% A |graph| is _undirected_ if an edge between two nodes means that\r\n% the nodes are connected to each other.  A |digraph| is _directed_\r\n% if the connections are one-way.  Edges can have different weights,\r\n% or all the weights can be the same.\r\n\r\n   G = digraph(1,2:5);\r\n   G = addedge(G,2,6:15);\r\n   G = addedge(G,15,16:20)\r\n   \r\n%%\r\n% Graphs have no inherent geometric structure.  The tricky part of\r\n% plotting a graph is locating the nodes in two- or three-dimensional\r\n% space.  The graph plot methods offer several different layouts.\r\n\r\n   plot(G,'layout','force','nodecolor',burnt_orange)\r\n   no_ticks\r\n\r\n%% Image Processing Toolbox(TM)\r\n% The Image Processing Toolbox has been available since the 1990's.\r\n% It is now at Version 10.2.  The command\r\n%\r\n%   help images\r\n%\r\n% lists over 500 functions.\r\n% As expected, the toolbox makes extensive use of array functionality.\r\n\r\n%%\r\n% This image, \"Psychedelic Pluto\", combines image processing and\r\n% numerical linear algebra.  The\r\n% <https:\/\/www.nasa.gov\/image-feature\/psychedelic-pluto\r\n% NASA press release> says \"New Horizons scientists made this false color\r\n% image of Pluto using a technique called *principal component analysis*\r\n% (emphasis mine)\r\n% to highlight the many subtle color differences between Pluto's\r\n% distinct regions.\"  The image was presented by Will Grundy of \r\n% the New Horizons\u00e2\u20ac&#x2122; surface composition team on Nov. 9, 2015.\r\n%\r\n% <<psychedelic_pluto.png>>\r\n%\r\n% _Image Credit: NASA\/JHUAPL\/SwRI_\r\n\r\n%%\r\n% Images of Pluto are of particular interest because MATLAB and Simulink\r\n% <https:\/\/blogs.mathworks.com\/steve\/2015\/07\/19\/new-horizons-pluto-program-used-matlab-and-image-processing-for-navigation\/\r\n% were used extensively> in both navigation and the flight control system\r\n% for the New Horizons flyby mission.\r\n   \r\n%% Symbolic Math Toolbox(TM)\r\n% The Symbolic Math Toolbox has also been available since the 1990's.\r\n% It is now at version 8.1.  The command\r\n%\r\n%   help symbolic\r\n%\r\n% lists almost 500 functions.\r\n\r\n%%\r\n% The toolbox provides functions for solving, plotting, and manipulating\r\n% symbolic math equations.  The operations include differentiation,\r\n% integration, simplification, transforms, and equation solving.\r\n% Computations can be performed either analytically or using\r\n% variable-precision arithmetic.\r\n\r\n%%\r\n% The toolbox has recently been integrated with the Live Editor,\r\n% which I will discuss in my next blog post.  This example does\r\n% not yet use the Live Editor.\r\n\r\n%%\r\n% The statement\r\n\r\n    x = sym('x')\r\n    \r\n%%\r\n% creates a symbolic variable that just represents itself.\r\n% The variable does not have a numeric value.  The statement\r\n    \r\n    f = 1\/(4*cos(x)+5)\r\n    \r\n%%\r\n% creates a symbolic function of that variable.\r\n    \r\n%%\r\n% Without the Live Editor, the closest we can come to LaTeX style\r\n% typeset mathematics in the command window is\r\n\r\n    pretty(f)\r\n        \r\n%%\r\n% Let's differentiate |f| twice.\r\n \r\n    f2 = diff(f,2)\r\n    \r\n%%\r\n% And then integrate the result twice.  Do we get back to where\r\n% we started?\r\n\r\ng = int(int(f2))\r\n    \r\n%%\r\n% No, |f| and |g| are not the same expression.  Let's plot both of them.\r\n\r\n    subplot(211)\r\n    ezplot(f)\r\n    subplot(212)\r\n    ezplot(g)\r\n    \r\n%%\r\n% The two plots have the same shape, but the y axes are not the same.\r\n% Let's compute the difference; this is the \"error\" made by\r\n% differentiating twice and then integrating twice.\r\n\r\n    e = f - g\r\n    \r\n%%\r\n% The error isn't zero.  What is it?  First a plot.\r\n\r\n   half_fig\r\n   ezplot(e)\r\n\r\n%%\r\n% Now let's do the most difficult computation of this entire example.\r\n\r\n    s = simplify(e)\r\n    \r\n%%\r\n% So, |e| is an elaborate way of writing the constant function, 1.\r\n% That's the \"constant of integration\", the infamous +C that we always\r\n% forget to include in indefinite integration.\r\n\r\n%% Homework\r\n% Here's your homework for today.  Where did the limits on the y axis\r\n% for |ezplot(e)| come from?\r\n##### SOURCE END ##### 0bb836b832294d4388e8765cf8f4b69d\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/cleve\/files\/psychedelic_pluto.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><!--introduction--><p>The ACM Special Interest Group on Programming Languages, SIGPLAN, expects to hold the fourth in a series of conferences on the History of Programming Languages in 2020, see <a href=\"https:\/\/hopl4.sigplan.org\/\">HOPL-IV<\/a>.  The first drafts of papers are to be submitted by August 2018.  That long lead time gives me the opportunity to write a detailed history of MATLAB. I plan to write the paper in sections, which I'll post in this blog as they are available.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/cleve\/2018\/04\/30\/matlab-history-modern-matlab-part-2\/\">read more >><\/a><\/p>","protected":false},"author":78,"featured_media":3287,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[12,24,23,4],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/3259"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/users\/78"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/comments?post=3259"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/3259\/revisions"}],"predecessor-version":[{"id":3327,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/3259\/revisions\/3327"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/media\/3287"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/media?parent=3259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/categories?post=3259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/tags?post=3259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}