{"id":187,"date":"2009-06-11T18:01:47","date_gmt":"2009-06-11T18:01:47","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2009\/06\/11\/rooting-around-in-matlab-part-1\/"},"modified":"2009-06-11T18:11:01","modified_gmt":"2009-06-11T18:11:01","slug":"rooting-around-in-matlab-part-1","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2009\/06\/11\/rooting-around-in-matlab-part-1\/","title":{"rendered":"Rooting Around in MATLAB &#8211; Part 1"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>I've been interested in teaching for a long time, including ways to use MATLAB.  One concept that students might need to understand\r\n         early in their college careers is that of finding roots or zeros of functions. To understand at least some of the algorithms,\r\n         you might want to teach the students about <a href=\"http:\/\/en.wikipedia.org\/wiki\/Fixed_point_iteration\">fixed points<\/a> for functions.  It's the basis for some methods of solving equations or finding roots, algorithms such as <a href=\"http:\/\/en.wikipedia.org\/wiki\/Fixed_point_iteration#Applications\">Newton's method<\/a>, finding <a href=\"http:\/\/en.wikipedia.org\/wiki\/Fixed_point_iteration#Examples\">square roots<\/a>, and more.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Example Function<\/a><\/li>\r\n         <li><a href=\"#5\">Find the Roots or Zeros<\/a><\/li>\r\n         <li><a href=\"#9\">The Series of Posts<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Example Function<a name=\"1\"><\/a><\/h3>\r\n   <p>Let's start with a simple cubic polynomial <tt>f<\/tt>.\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/187\/findFP_eq61733.png\"> <\/p>\r\n   <p>Here's one common way to represent this polynomial in MATLAB, using coefficients of descending powers of the independent variable.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">p = [1 0 1 -1];<\/pre><p>I can then use <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/polyval.html\"><tt>polyval<\/tt><\/a> to evaluate the polynomial.  And then I can plot it.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">x = -2:0.1:2;\r\ny = polyval(p,x);\r\nplot(x,y)\r\ntitle <span style=\"color: #A020F0\">f<\/span>\r\ngrid<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/187\/findFP_01.png\"> <p>I could also represent the polynomial as an <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/matlab_prog\/f4-70115.html\">anonymous function<\/a> and plot it with <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/fplot.html\"><tt>fplot<\/tt><\/a>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">f = @(x) x.^3 + x - 1;\r\nfplot(f,[-2 2])\r\ntitle <span style=\"color: #A020F0\">f<\/span>\r\ngrid <span style=\"color: #A020F0\">on<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/187\/findFP_02.png\"> <h3>Find the Roots or Zeros<a name=\"5\"><\/a><\/h3>\r\n   <p>I have at least 2 choices in MATLAB for finding a zero or root of this polynomial.  The first is to use <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/roots.html\"><tt>roots<\/tt><\/a> to get all the possible zeros.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">rsolution = roots([1 0 1 -1])<\/pre><pre style=\"font-style:oblique\">rsolution =\r\n     -0.34116 +     1.1615i\r\n     -0.34116 -     1.1615i\r\n      0.68233              \r\n<\/pre><p>You can see that this polynomial has one real root between 0 and 1, and 2 complex roots.<\/p>\r\n   <p>You can also use <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/fzero.html\"><tt>fzero<\/tt><\/a>, one of MATLAB's optimization functions, to find the value.  Here we'll select 0.5 as the initial guess.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">fzsolution = fzero(f,0.5)<\/pre><pre style=\"font-style:oblique\">fzsolution =\r\n      0.68233\r\n<\/pre><p>In the next post, I'll describe a way to solve this same problem using an algorithm based on fixed point iteration.<\/p>\r\n   <h3>The Series of Posts<a name=\"9\"><\/a><\/h3>\r\n   <p>In addition to this post, there will be two more.  Until they are published, the following two links will not be available.<\/p>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=187\">Rooting Around in MATLAB - Part 1 (this post)<\/a><\/li>\r\n         <li><a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=188\">Rooting Around in MATLAB - Part 2<\/a><\/li>\r\n         <li><a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=189\">Rooting Around in MATLAB - Part 3<\/a><\/li>\r\n      <\/ul>\r\n   <\/div><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_6b06c53e3b814b7bb99b36d42975b140() {\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='6b06c53e3b814b7bb99b36d42975b140 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 6b06c53e3b814b7bb99b36d42975b140';\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        author = 'Loren Shure';\r\n        copyright = 'Copyright 2009 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 author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\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      \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_6b06c53e3b814b7bb99b36d42975b140()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.8<br><\/p>\r\n<\/div>\r\n<!--\r\n6b06c53e3b814b7bb99b36d42975b140 ##### SOURCE BEGIN #####\r\n%% Rooting Around in MATLAB - Part 1\r\n% I've been interested in teaching for a long time, including ways to\r\n% use MATLAB.  One concept that students might need to understand early\r\n% in their college careers is that of finding roots or zeros of functions.\r\n% To understand at least some of the algorithms, you might want to teach\r\n% the students about\r\n% <http:\/\/en.wikipedia.org\/wiki\/Fixed_point_iteration fixed points> for\r\n% functions.  It's the basis for some methods of solving equations or\r\n% finding roots, algorithms such as \r\n% <http:\/\/en.wikipedia.org\/wiki\/Fixed_point_iteration#Applications Newton's method>,\r\n% finding\r\n% <http:\/\/en.wikipedia.org\/wiki\/Fixed_point_iteration#Examples square roots>,\r\n% and more.\r\n%% Example Function\r\n% Let's start with a simple cubic polynomial |f|.\r\n%\r\n% $f(x) = x^3 + x - 1$ \r\n%\r\n%% \r\n% Here's one common way to represent this polynomial in MATLAB, using\r\n% coefficients of descending powers of the independent variable.\r\np = [1 0 1 -1];\r\n%%\r\n% I can then use <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/polyval.html |polyval|>\r\n% to evaluate the polynomial.  And then I can plot it.\r\nx = -2:0.1:2;\r\ny = polyval(p,x);\r\nplot(x,y)\r\ntitle f\r\ngrid\r\n\r\n%%\r\n% I could also represent the polynomial as an \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/matlab_prog\/f4-70115.html anonymous function>\r\n% and plot it with\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/fplot.html |fplot|>.\r\nf = @(x) x.^3 + x - 1;\r\nfplot(f,[-2 2])\r\ntitle f\r\ngrid on\r\n%% Find the Roots or Zeros\r\n% I have at least 2 choices in MATLAB for finding a zero or root of this\r\n% polynomial.  The first is to use\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/roots.html |roots|>\r\n% to get all the possible zeros.\r\nrsolution = roots([1 0 1 -1])\r\n%%\r\n% You can see that this polynomial has one real root between 0 and 1, and 2\r\n% complex roots.\r\n%% \r\n% You can also use\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/fzero.html |fzero|>,\r\n% one of MATLAB's optimization functions, to find the value.  Here we'll\r\n% select 0.5 as the initial guess.\r\nfzsolution = fzero(f,0.5)\r\n%% \r\n% In the next post, I'll describe a way to solve this same problem using an\r\n% algorithm based on fixed point iteration.\r\n%% The Series of Posts\r\n% In addition to this post, there will be two more.  Until they are\r\n% published, the following two links will not be available.\r\n%\r\n% * <https:\/\/blogs.mathworks.com\/loren\/?p=187 Rooting Around in MATLAB - Part 1 (this post)>\r\n% * <https:\/\/blogs.mathworks.com\/loren\/?p=188 Rooting Around in MATLAB - Part 2>\r\n% * <https:\/\/blogs.mathworks.com\/loren\/?p=189 Rooting Around in MATLAB - Part 3>\r\n%\r\n\r\n\r\n\r\n##### SOURCE END ##### 6b06c53e3b814b7bb99b36d42975b140\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      I've been interested in teaching for a long time, including ways to use MATLAB.  One concept that students might need to understand\r\n         early in their college careers is that of... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2009\/06\/11\/rooting-around-in-matlab-part-1\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[25,31],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/187"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=187"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/187\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}