{"id":330,"date":"2012-10-01T12:20:08","date_gmt":"2012-10-01T17:20:08","guid":{"rendered":"https:\/\/blogs.mathworks.com\/cleve\/?p=330"},"modified":"2016-12-05T13:53:10","modified_gmt":"2016-12-05T18:53:10","slug":"chebfun-numerical-computing-with-functions","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/cleve\/2012\/10\/01\/chebfun-numerical-computing-with-functions\/","title":{"rendered":"CHEBFUN, Numerical Computing With Functions"},"content":{"rendered":"&nbsp;\r\n<div class=\"content\"><!--introduction-->I recently attended \"Chebfun and Beyond\", a three-day workshop in Oxford, England. <i>Chebfun<\/i> is a mathematical research and open source software project for numerical computing with functions. I plan to write a series of Cleve's Corner blog posts about Chebfun.\r\n\r\n<!--\/introduction-->\r\n<h3>Contents<\/h3>\r\n<div>\r\n<ul>\r\n \t<li><a href=\"#9fa14414-868d-45bb-b1c9-889454b97901\">Chebfun<\/a><\/li>\r\n \t<li><a href=\"#a234e002-193f-484c-b652-00229fa44ca0\">Runge's Function<\/a><\/li>\r\n \t<li><a href=\"#23e87440-285c-4167-87a4-113318793658\">Accuracy<\/a><\/li>\r\n \t<li><a href=\"#93d0c7a7-8602-4193-84f3-c3a93c138280\">Methods<\/a><\/li>\r\n \t<li><a href=\"#d351b936-cfee-40e2-875e-e4de68de9044\">Chebfun Project<\/a><\/li>\r\n<\/ul>\r\n<\/div>\r\n<h4>Chebfun<a name=\"9fa14414-868d-45bb-b1c9-889454b97901\"><\/a><\/h4>\r\nFor a description of Chebfun we quote the web site, <a href=\"http:\/\/www.chebfun.org\/\">&lt;http:\/\/www.chebfun.org\/<\/a>&gt;\r\n<p style=\"margin-left: 3ex;\">Chebfun is a collection of algorithms and an open-source software system\r\nin object-oriented MATLAB which extends familiar powerful methods of\r\nnumerical computation involving numbers to continuous or\r\npiecewise-continuous functions.<\/p>\r\n\r\n<h4>Runge's Function<a name=\"a234e002-193f-484c-b652-00229fa44ca0\"><\/a><\/h4>\r\nExercise 3.9 and the program <a href=\"https:\/\/www.mathworks.com\/content\/dam\/mathworks\/mathworks-dot-com\/moler\/ncm\/rungeinterp.m\"><tt>rungeinterp<\/tt><\/a> from <a href=\"https:\/\/www.mathworks.com\/moler\/index_ncm.html\"><i>Numerical Computing with MATLAB<\/i><\/a> involves an example due to Carle Runge,\r\n\r\n$$ f(x) = \\frac{1}{1+25x^2} $$\r\n\r\nThe program demonstrates the fact that interpolation of $f$ by polynomials based on sampling $f(x)$ at equally spaced points does not provide uniformly accurate approximants. Chebfun provides the answer to part 3.9b of the exercise, which asks how the interpolation points should be distributed to generate satisfactory interpolants. The answer, Chebyshev points, also generates the first half of Chebfun's name.\r\n\r\nHere is a chebfun of Runge's function, plotted with linestyle <tt>'.-'<\/tt> to show the Chebyshev points.\r\n<pre class=\"codeinput\">f = @(x) 1.\/(1 + 25*x.^2);\r\nF = chebfun(f);\r\nplot(F,<span class=\"string\">'.-'<\/span>)\r\nxlabel(<span class=\"string\">'x'<\/span>)\r\ntitle(<span class=\"string\">'Runge''s function'<\/span>)\r\n<\/pre>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/cleve\/chebfun_1_01.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n\r\nYou can see that the interpolation points are concentrated nearer the ends of the interval, at the zeros of the Chebyshev polynomials,\r\n\r\n$$ x_j = - \\cos {j \\frac {\\pi}{n}}, \\ \\ \\ j = 0, ..., n $$\r\n\r\nHere we need a fair number of points to get a polynomial approximation that is accurate to floating point precision across the entire interval.\r\n<pre class=\"codeinput\">n = length(F)-1\r\n<\/pre>\r\n<pre class=\"codeoutput\">n =\r\n\r\n   182\r\n\r\n<\/pre>\r\nThe use of Chebyshev points not only leads to accurate approximations, it also makes it possible to use powerful mathematical tools including Fourier transforms and barycentric coordinates in the underlying operations.\r\n<h4>Accuracy<a name=\"23e87440-285c-4167-87a4-113318793658\"><\/a><\/h4>\r\nWe can assess the accuracy for this example by computing the residual at a large number of randomly chosen points in the interval.\r\n<pre class=\"codeinput\">x = 2*rand(2^10,1)-1;\r\nresidual = max(abs(f(x) - F(x)))\r\n<\/pre>\r\n<pre class=\"codeoutput\">residual =\r\n\r\n   1.8874e-15\r\n\r\n<\/pre>\r\nThe computed residual results from three quantities, all of roughly the same size, the actual error $|f(x) - F(x)|$, as well as the floating rounding errors generated in evaluating <tt>f(x)<\/tt> and <tt>F(x)<\/tt>.\r\n<h4>Methods<a name=\"93d0c7a7-8602-4193-84f3-c3a93c138280\"><\/a><\/h4>\r\nThe query\r\n<pre class=\"codeinput\">length(methods(<span class=\"string\">'chebfun'<\/span>))\r\n<\/pre>\r\n<pre class=\"codeoutput\">ans =\r\n\r\n   203\r\n\r\n<\/pre>\r\nreveals that there are over 200 methods defined for the <tt>chebfun<\/tt> object. There are additional methods defined for some subordinate objects. The overall design objective has been to take familiar MATLAB operations on vectors and generalize them to functions. For example <tt>sum<\/tt>,\r\n<pre class=\"codeinput\">I = sum(F)\r\n<\/pre>\r\n<pre class=\"codeoutput\">I =\r\n\r\n    0.5494\r\n\r\n<\/pre>\r\ncomputes the definite integral,\r\n\r\n$$ I = \\int_{-1}^{1} {F(x) dx} $$\r\n\r\nand <tt>cumsum<\/tt>,\r\n<pre class=\"codeinput\">G = cumsum(F);\r\nplot(G)\r\nxlabel(<span class=\"string\">'x'<\/span>)\r\ntitle(<span class=\"string\">'indefinite integral'<\/span>)\r\n<\/pre>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/cleve\/chebfun_1_02.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n\r\ncomputes the indefinite integral\r\n\r\n$$ G(x) = \\int_{-1}^{x} {F(s) ds} $$\r\n<h4>Chebfun Project<a name=\"d351b936-cfee-40e2-875e-e4de68de9044\"><\/a><\/h4>\r\nProfessor Nick Trefethen and his student Zachary Battles began the Chebfun project in 2001 at the Numerical Analysis Group at Oxford. Today there is a second group at the University of Delaware under Professor Toby Driscoll. There have been a number of graduate and postdoctoral students over the years at both institutions. Dr. Nick Hale at Oxford currently manages the project.\r\n\r\nThe MATLAB <tt>publish<\/tt> command has been used to prepare the first edition of the documentation, as well as prepare the LaTeX source for a hard copy book that will be published shortly.\r\n\r\nVersion 4.2 of the Chebfun software was released in March and <a href=\"http:\/\/www.chebfun.org\/\">is available<\/a> from the Chebfun web site. The Chebfun team is using an open source software development model.\r\n\r\n<script>\/\/ <![CDATA[\r\nfunction grabCode_9eaf0f38973f41c5b4a81ae0cb0b4ce6() {\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='9eaf0f38973f41c5b4a81ae0cb0b4ce6 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 9eaf0f38973f41c5b4a81ae0cb0b4ce6';\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 2012 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('\r\n\r\n<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>\r\n\r\n\r\n\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }\r\n\/\/ ]]><\/script>\r\n<p style=\"text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;\">\r\n<a><span style=\"font-size: x-small; font-style: italic;\">Get\r\nthe MATLAB code<noscript>(requires JavaScript)<\/noscript><\/span><\/a>\r\n\r\nPublished with MATLAB\u00ae R2012b<\/p>\r\n<p class=\"footer\">\r\nPublished with MATLAB\u00ae R2012b<\/p>\r\n\r\n<\/div>\r\n<!--\r\n9eaf0f38973f41c5b4a81ae0cb0b4ce6 ##### SOURCE BEGIN #####\r\n%% CHEBFUN, Numerical Computing With Functions\r\n% I recently attended \"Chebfun and Beyond\",\r\n% a three-day workshop in Oxford, England.\r\n% _Chebfun_ is a mathematical research and open source software\r\n% project for numerical computing with functions.\r\n% I plan to write a series of Cleve's Corner blog posts about Chebfun.\r\n\r\n%% Chebfun\r\n% For a description of Chebfun we quote the web site,\r\n% <http:\/\/www2.maths.ox.ac.uk\/chebfun\/ http:\/\/www2.maths.ox.ac.uk\/chebfun>\r\n%\r\n% <html>\r\n%\r\n<p style=\"margin-left:3ex;\">\r\n% Chebfun is a collection of algorithms and an open-source software system\r\n% in object-oriented MATLAB which extends familiar powerful methods of\r\n% numerical computation involving numbers to continuous or\r\n% piecewise-continuous functions.\r\n%\r\n\r\n% <\/html>\r\n%\r\n\r\n%% Runge's Function\r\n% Exercise 3.9 and the program\r\n% <https:\/\/www.mathworks.com\/moler.htmlncm\/rungeinterp.m |rungeinterp|> from\r\n% <https:\/\/www.mathworks.com\/moler\/index_ncm.html % _Numerical Computing with MATLAB_>\r\n% involves an example due to Carle Runge,\r\n%\r\n% $$ f(x) = \\frac{1}{1+25x^2} $$\r\n%\r\n% The program demonstrates the fact that interpolation of $f$ by polynomials\r\n% based on sampling $f(x)$ at equally spaced points does not provide\r\n% uniformly accurate approximants.  Chebfun provides the answer to\r\n% part 3.9b of the exercise, which asks how the interpolation points should be\r\n% distributed to generate satisfactory interpolants.  The answer,\r\n% Chebyshev points, also generates the first half of Chebfun's name.\r\n\r\n%%\r\n% Here is a chebfun of Runge's function, plotted with linestyle |'.-'|\r\n% to show the Chebyshev points.\r\n\r\nf = @(x) 1.\/(1 + 25*x.^2);\r\nF = chebfun(f);\r\nplot(F,'.-')\r\nxlabel('x')\r\ntitle('Runge''s function')\r\n\r\n%%\r\n% You can see that the interpolation points are concentrated nearer\r\n% the ends of the interval, at the zeros of the Chebyshev polynomials,\r\n%\r\n% $$ x_j = - \\cos {j \\frac {\\pi}{n}}, \\ \\ \\  j = 0, ..., n $$\r\n%\r\n\r\n%%\r\n% Here we need a fair number of points to get a polynomial approximation\r\n% that is accurate to floating point precision across the entire interval.\r\n\r\nn = length(F)-1\r\n\r\n%%\r\n% The use of Chebyshev points not only leads to accurate approximations,\r\n% it also makes it possible to use powerful mathematical tools including\r\n% Fourier transforms and barycentric coordinates in the underlying operations.\r\n\r\n%% Accuracy\r\n% We can assess the accuracy for this example by computing the residual\r\n% at a large number of randomly chosen points in the interval.\r\n\r\nx = 2*rand(2^10,1)-1;\r\nresidual = max(abs(f(x) - F(x)))\r\n\r\n%%\r\n% The computed residual results from three quantities, all of roughly the same\r\n% size, the actual error $|f(x) - F(x)|$, as well as the floating rounding\r\n% errors generated in evaluating |f(x)| and |F(x)|.\r\n\r\n%% Methods\r\n% The query\r\n\r\nlength(methods('chebfun'))\r\n\r\n%%\r\n% reveals that there are over 200 methods defined for the |chebfun| object.\r\n% There are additional methods defined for some subordinate objects.\r\n% The overall design objective has been to take familiar MATLAB operations\r\n% on vectors and generalize them to functions.\r\n% For example |sum|,\r\n\r\nI = sum(F)\r\n\r\n%%\r\n% computes the definite integral,\r\n%\r\n% $$  I = \\int_{-1}^{1} {F(x) dx} $$\r\n%\r\n% and |cumsum|,\r\n\r\nG = cumsum(F);\r\nplot(G)\r\nxlabel('x')\r\ntitle('indefinite integral')\r\n\r\n%%\r\n% computes the indefinite integral\r\n%\r\n% $$ G(x) = \\int_{-1}^{x} {F(s) ds} $$\r\n%\r\n\r\n%% Chebfun Project\r\n% Professor Nick Trefethen and his student Zachary Battles began\r\n% the Chebfun project in 2001 at the Numerical Analysis Group at Oxford.\r\n% Today there is a second group at the University of Delaware under Professor\r\n% Toby Driscoll.  There have been a number of graduate and postdoctoral\r\n% students over the years at both institutions.  Dr. Nick Hale at Oxford\r\n% currently manages the project.\r\n\r\n%%\r\n% The MATLAB |publish| command has been used to prepare the first edition of\r\n% the documentation, as well as prepare the LaTeX source for a hard copy book\r\n% that will be published shortly.\r\n\r\n%%\r\n% Version 4.2 of the Chebfun software was released in March and\r\n% <http:\/\/www2.maths.ox.ac.uk\/chebfun\/download is available>\r\n% from the Chebfun web site.\r\n% The Chebfun team is using an open source software development model.\r\n\r\n##### SOURCE END ##### 9eaf0f38973f41c5b4a81ae0cb0b4ce6\r\n-->","protected":false},"excerpt":{"rendered":"<!--introduction-->I recently attended \"Chebfun and Beyond\", a three-day workshop in Oxford, England. <i>Chebfun<\/i> is a mathematical research and open source software project for numerical computing with functions. I plan to write a series of Cleve's Corner blog posts about Chebfun.\r\n\r\n<!--\/introduction-->... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/cleve\/2012\/10\/01\/chebfun-numerical-computing-with-functions\/\">read more >><\/a><\/p>","protected":false},"author":78,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/330"}],"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=330"}],"version-history":[{"count":12,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/330\/revisions"}],"predecessor-version":[{"id":2176,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/330\/revisions\/2176"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/media?parent=330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/categories?post=330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/tags?post=330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}