{"id":475,"date":"2016-04-20T10:57:33","date_gmt":"2016-04-20T14:57:33","guid":{"rendered":"https:\/\/blogs.mathworks.com\/graphics\/?p=475"},"modified":"2016-04-20T10:57:33","modified_gmt":"2016-04-20T14:57:33","slug":"fplot-and-friends","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/graphics\/2016\/04\/20\/fplot-and-friends\/","title":{"rendered":"FPLOT and Friends"},"content":{"rendered":"\r\n<div class=\"content\"><p>Another new feature that I really like in <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/release-notes.html#R2016a\">R2016a<\/a> is the upgraded fplot function and all of the new members of the fplot family.<\/p><p>The <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/fplot.html\">fplot function<\/a> has been around for a long time. The basic idea is that you pass it a function which takes X coordinates as inputs and returns Y coordinates as outputs. Then fplot will use that function to draw a curve.<\/p><pre class=\"codeinput\">fplot(@(x) sin(x))\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/graphics\/files\/fplot_post_01.png\" alt=\"\"> <p>This is really useful for getting a \"quick feel\" for the shape of a function, or a family of functions.<\/p><pre class=\"codeinput\">fplot(@(x) besselj(x,1),[0 2*pi])\r\nhold <span class=\"string\">on<\/span>\r\nfplot(@(x) besselj(x,2),[0 2*pi])\r\nfplot(@(x) besselj(x,3),[0 2*pi])\r\nfplot(@(x) besselj(x,4),[0 2*pi])\r\nfplot(@(x) besselj(x,5),[0 2*pi])\r\nhold <span class=\"string\">off<\/span>\r\nlegend <span class=\"string\">show<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/graphics\/files\/fplot_post_02.png\" alt=\"\"> <p>In addition to functions, if you have the <a href=\"https:\/\/www.mathworks.com\/products\/symbolic\/\">Symbolic Math Toolbox<\/a>, fplot can also accept symbolic variables now. This gives it even more power. For example, I can reproduce that plot with a single call to fplot by calling <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/besselj.html\">besselj<\/a> with a symbolic variable for the domain and a vector for the order.<\/p><pre class=\"codeinput\">syms <span class=\"string\">x<\/span>\r\nfplot(besselj(x,1:5),[0,2*pi])\r\nlegend <span class=\"string\">show<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/graphics\/files\/fplot_post_03.png\" alt=\"\"> <p>The new version of fplot has a lot of nice refinements, such as the nice legend entries in those last two examples.<\/p><p>When we first showed the new fplot to Cleve, he gave it one of <a href=\"https:\/\/blogs.mathworks.com\/cleve\/2012\/09\/24\/supremum\/\">his favorite functions<\/a>.<\/p><p>$$tan(sin(x)) + sin(tan(x))$$<\/p><p>This is what the previous version of fplot did with that.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/graphics\/files\/R2015b_tansin.png\" alt=\"\"> <\/p><p>And here's the R2016a version.<\/p><pre class=\"codeinput\">fplot(@(x) tan(sin(x)) + sin(tan(x)))\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/graphics\/files\/fplot_post_04.png\" alt=\"\"> <p>As you can see, it does a better of resolving the details in those tricky bits. And it labeled the asymptotes for use, although it missed the one at $-\\pi\/2$. The reason Cleve likes this function is that it's a bit of a torture test!<\/p><p>And we can get even more detail if we zoom in.<\/p><pre class=\"codeinput\">xlim(pi\/2 + [-.2 .2])\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/graphics\/files\/fplot_post_05.png\" alt=\"\"> <p>Another big enhancement is that fplot can now do parametric curves as well as plotting Y as a function of X. To do this, we just pass it two functions. The first function takes the parameter value as input and returns X coordinates. The second function takes the parameter value as input and returns Y coordinates.<\/p><p>For example, I can use it to recreate the cubic B&eacute;zier curve from <a href=\"https:\/\/blogs.mathworks.com\/graphics\/2014\/10\/13\/bezier-curves\/\">this blog post<\/a>. This is a bit simpler than the way I did it in that post. Especially if you're not familiar with things like <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/kron.html\">Kronecker tensor products<\/a>. Note, you'll need the placelabel function I wrote in that earlier blog post.<\/p><pre class=\"codeinput\">clf\r\npt1 = [ 5;-10];\r\npt2 = [18; 18];\r\npt3 = [38; -5];\r\npt4 = [45; 15];\r\nplacelabel(pt1,<span class=\"string\">'pt_1'<\/span>);\r\nplacelabel(pt2,<span class=\"string\">'pt_2'<\/span>);\r\nplacelabel(pt3,<span class=\"string\">'pt_3'<\/span>);\r\nplacelabel(pt4,<span class=\"string\">'pt_4'<\/span>);\r\nxlim([0 50])\r\naxis <span class=\"string\">equal<\/span>\r\nhold <span class=\"string\">on<\/span>\r\n\r\ncubic_bezier = @(t,a,b,c,d) a*      (1-t).^3 <span class=\"keyword\">...<\/span>\r\n                        + 3*b*t   .*(1-t).^2 <span class=\"keyword\">...<\/span>\r\n                        + 3*c*t.^2.*(1-t)    <span class=\"keyword\">...<\/span>\r\n                        +   d*t.^3          ;\r\n\r\nfplot(@(t)cubic_bezier(t,pt1(1),pt2(1),pt3(1),pt4(1)), <span class=\"keyword\">...<\/span>\r\n      @(t)cubic_bezier(t,pt1(2),pt2(2),pt3(2),pt4(2)), <span class=\"keyword\">...<\/span>\r\n      [0 1])\r\n\r\nhold <span class=\"string\">off<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/graphics\/files\/fplot_post_06.png\" alt=\"\"> <p>But one of the coolest features of the new version of fplot is hiding at the bottom of <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/fplot.html\">the doc page<\/a>. If you look down there, you'll see this:<\/p><p>\r\n<h2>See Also<\/h2>\r\n<h3>Functions<\/h3>\r\n<ul><li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/fcontour.html\">fcontour<\/a><\/li>\r\n<li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/fmesh.html\">fmesh<\/a><\/li>\r\n<li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/fplot3.html\">fplot3<\/a><\/li>\r\n<li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/fsurf.html\">fsurf<\/a><\/li><\/ul>\r\n<\/p><p>That's right, there's now a whole family of fplot-like functions!<\/p><p>Let's take a look at some of these new ones.<\/p><p>First there's fplot3. It's used for 3D parametric curves, like the ones I wrote about in these earlier posts on this blog (<a href=\"https:\/\/blogs.mathworks.com\/graphics\/2014\/12\/04\/tie-a-ribbon-round-it-parametric-curves-part-1\/\">link1<\/a>, <a href=\"https:\/\/blogs.mathworks.com\/graphics\/2014\/12\/16\/down-the-tubes\/\">link2<\/a>). We just pass it 3 functions, like this:<\/p><pre class=\"codeinput\">fplot3(@(t) 3*cos(t)+cos(10*t).*cos(t), <span class=\"keyword\">...<\/span>\r\n       @(t) 3*sin(t)+cos(10*t).*sin(t), <span class=\"keyword\">...<\/span>\r\n       @(t) sin(10*t))\r\ndaspect([1 1 1])\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/graphics\/files\/fplot_post_07.png\" alt=\"\"> <p>Here's another of my favorite 3D parametric curves. It's commonly known as the <a title=\"http:\/\/paulbourke.net\/geometry\/baseball (link no longer works)\">baseball curve<\/a>.<\/p><pre class=\"codeinput\">a = .4;\r\nxfcn = @(t)sin(pi\/2-(pi\/2-a)*cos(t)).*cos(t\/2+a*sin(2*t));\r\nyfcn = @(t)sin(pi\/2-(pi\/2-a)*cos(t)).*sin(t\/2+a*sin(2*t));\r\nzfcn = @(t)cos(pi\/2-(pi\/2-a)*cos(t));\r\n\r\nfplot3(xfcn,yfcn,zfcn,[0 4*pi])\r\naxis <span class=\"string\">equal<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/graphics\/files\/fplot_post_08.png\" alt=\"\"> <p>There's also the new <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/fsurf.html\">fsurf function<\/a>. This means that instead of calling peaks to generate arrays of data ...<\/p><pre class=\"codeinput\">[x,y,z] = peaks;\r\n<\/pre><p>... and then passing that data to surf, ...<\/p><pre class=\"codeinput\">surf(x,y,z)\r\nxlim([-3 3])\r\nylim([-3 3])\r\ntitle(<span class=\"string\">'surf(peaks)'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/graphics\/files\/fplot_post_09.png\" alt=\"\"> <p>I can just call fsurf with a handle to the peaks function.<\/p><pre class=\"codeinput\">fsurf(@(x,y) peaks(x,y),[-3 3 -3 3])\r\ntitle(<span class=\"string\">'fsurf(@(x,y) peaks(x,y))'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/graphics\/files\/fplot_post_10.png\" alt=\"\"> <p>This results in a very similar picture, but look what happens when we zoom in.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/graphics\/files\/fsurf_zoom_animation.gif\" alt=\"\"> <\/p><p>It's regenerating the mesh on the fly during the zoom. With the surf command, we'd only get the resolution of our original call to peaks.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/graphics\/files\/surf_zoom_animation.gif\" alt=\"\"> <\/p><p>This makes the fplot family really useful for exploring a function with pan and zoom.<\/p><p>All of the functions I've used so far have been simple enough to write as anonymous functions. But sometimes you'll want to use more complex functions. For example, I've turned the parametric equation for the <a href=\"http:\/\/www.kleinbottle.com\/\">Klein bottle<\/a> into the following set of functions:<\/p><pre class=\"language-matlab\"><span class=\"keyword\">function<\/span> x = klein_xfcn(u,v)\r\n  mask1 = u&lt;pi;\r\n  mask2 = ~mask1;\r\n  r = klein_rfcn(u);\r\n  x = zeros(size(u));\r\n  x(mask1) = 6*cos(u(mask1)).*(1+sin(u(mask1))) + r(mask1).*cos(u(mask1)).*cos(v(mask1));\r\n  x(mask2) = 6*cos(u(mask2)).*(1+sin(u(mask2))) + r(mask2).*cos(v(mask2)+pi);\r\n<\/pre><pre class=\"language-matlab\"><span class=\"keyword\">function<\/span> y = klein_yfcn(u,v)\r\n  mask1 = u&lt;pi;\r\n  mask2 = ~mask1;\r\n  r = klein_rfcn(u);\r\n  y = zeros(size(u));\r\n  y(mask1) = 16*sin(u(mask1)) + r(mask1).*sin(u(mask1)).*cos(v(mask1));\r\n  y(mask2) = 16*sin(u(mask2));\r\n<\/pre><pre class=\"language-matlab\"><span class=\"keyword\">function<\/span> z = klein_zfcn(u,v)\r\n  r = klein_rfcn(u);\r\n  z = r.*sin(v);\r\n<\/pre><pre class=\"language-matlab\"><span class=\"keyword\">function<\/span> r = klein_rfcn(u)\r\n  r = 4*(1-cos(u)\/2);\r\n<\/pre><p>And now I can use fsurf to make a Klein bottle.<\/p><pre class=\"codeinput\">h = fsurf(@klein_xfcn,@klein_yfcn,@klein_zfcn,[0 2*pi 0 2*pi]);\r\ncamlight\r\naxis <span class=\"string\">equal<\/span>\r\ntitle(<span class=\"string\">'Klein Bottle'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/graphics\/files\/fplot_post_11.png\" alt=\"\"> <p>And we can get a better idea of its shape by making it partially transparent.<\/p><pre class=\"codeinput\">h.EdgeColor = <span class=\"string\">'none'<\/span>;\r\nh.FaceColor = [.929 .694 .125];\r\nh.FaceAlpha = .5;\r\nset(gca,<span class=\"string\">'Projection'<\/span>,<span class=\"string\">'perspective'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/graphics\/files\/fplot_post_12.png\" alt=\"\"> <p>There are a lot of goodies to play with in these functions, aren't there? And we haven't even talked about <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/fmesh.html\">fmesh<\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/fcontour.html\">fcontour<\/a>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_f83762b60ce041f9a6ce5547cca373a6() {\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='f83762b60ce041f9a6ce5547cca373a6 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' f83762b60ce041f9a6ce5547cca373a6';\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 2016 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_f83762b60ce041f9a6ce5547cca373a6()\"><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; R2016a<br><\/p><\/div><!--\r\nf83762b60ce041f9a6ce5547cca373a6 ##### SOURCE BEGIN #####\r\n%%\r\n% Another new feature that I really like in <https:\/\/www.mathworks.com\/help\/matlab\/release-notes.html#R2016a R2016a> is the upgraded fplot function and\r\n% all of the new members of the fplot family. \r\n%\r\n% The <https:\/\/www.mathworks.com\/help\/matlab\/ref\/fplot.html fplot function> \r\n% has been around for a long time. The basic idea is that you pass it a\r\n% function which takes X coordinates as inputs and returns Y coordinates as\r\n% outputs. Then fplot will use that function to draw a curve.\r\n%\r\nfplot(@(x) sin(x))\r\n\r\n%%\r\n% This is really useful for getting a \"quick feel\" for the shape of a\r\n% function, or a family of functions.\r\n%\r\nfplot(@(x) besselj(x,1),[0 2*pi])\r\nhold on\r\nfplot(@(x) besselj(x,2),[0 2*pi])\r\nfplot(@(x) besselj(x,3),[0 2*pi])\r\nfplot(@(x) besselj(x,4),[0 2*pi])\r\nfplot(@(x) besselj(x,5),[0 2*pi])\r\nhold off\r\nlegend show\r\n\r\n%%\r\n% In addition to functions, if you have the <https:\/\/www.mathworks.com\/products\/symbolic\/\r\n% Symbolic Math Toolbox>, fplot can also accept symbolic variables now.\r\n% This gives it even more power. For example, I can reproduce that plot\r\n% with a single call to fplot by calling <https:\/\/www.mathworks.com\/help\/matlab\/ref\/besselj.html besselj>\r\n% with a symbolic variable for the domain and a vector for the order.\r\n%\r\nsyms x\r\nfplot(besselj(x,1:5),[0,2*pi])\r\nlegend show\r\n\r\n%%\r\n% The new version of fplot has a lot of nice refinements, such as the nice \r\n% legend entries in those last two examples.\r\n%\r\n% When we first showed the new fplot to Cleve, he gave it one of \r\n% <https:\/\/blogs.mathworks.com\/cleve\/2012\/09\/24\/supremum\/ his favorite functions>. \r\n%\r\n% $$tan(sin(x)) + sin(tan(x))$$\r\n%\r\n% This is what the previous version of fplot did with that.\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/graphics\/files\/R2015b_tansin.png>>\r\n%\r\n% And here's the R2016a version.\r\n%\r\nfplot(@(x) tan(sin(x)) + sin(tan(x)))\r\n\r\n%%\r\n% As you can see, it does a better of resolving the details in those tricky\r\n% bits. And it labeled the asymptotes for use, although it missed the one \r\n% at $-\\pi\/2$. The reason Cleve likes this function is that it's a bit of \r\n% a torture test!\r\n%\r\n% And we can get even more detail if we zoom in.\r\n%\r\nxlim(pi\/2 + [-.2 .2])\r\n\r\n%%\r\n% Another big enhancement is that fplot can now do parametric curves as\r\n% well as plotting Y as a function of X. To do this, we just pass it two \r\n% functions. The first function takes the parameter value as input and\r\n% returns X coordinates. The second function takes the parameter value as\r\n% input and returns Y coordinates.\r\n%\r\n% For example, I can use it to\r\n% recreate the cubic B\u00c3\u00a9zier curve from\r\n% <https:\/\/blogs.mathworks.com\/graphics\/2014\/10\/13\/bezier-curves\/ this blog\r\n% post>. This is a bit simpler than the way I did it in that post. Especially if you're\r\n% not familiar with things like <https:\/\/www.mathworks.com\/help\/matlab\/ref\/kron.html Kronecker tensor products>.\r\n% Note, you'll need the placelabel function I wrote in that earlier blog post.\r\n%\r\nclf\r\npt1 = [ 5;-10];\r\npt2 = [18; 18];\r\npt3 = [38; -5];\r\npt4 = [45; 15];\r\nplacelabel(pt1,'pt_1');\r\nplacelabel(pt2,'pt_2');\r\nplacelabel(pt3,'pt_3');\r\nplacelabel(pt4,'pt_4');\r\nxlim([0 50])\r\naxis equal\r\nhold on\r\n\r\ncubic_bezier = @(t,a,b,c,d) a*      (1-t).^3 ...\r\n                        + 3*b*t   .*(1-t).^2 ...\r\n                        + 3*c*t.^2.*(1-t)    ...\r\n                        +   d*t.^3          ;\r\n\r\nfplot(@(t)cubic_bezier(t,pt1(1),pt2(1),pt3(1),pt4(1)), ...\r\n      @(t)cubic_bezier(t,pt1(2),pt2(2),pt3(2),pt4(2)), ...\r\n      [0 1])\r\n\r\nhold off\r\n\r\n%%\r\n% But one of the coolest features of the new version of fplot is hiding at\r\n% the bottom of <https:\/\/www.mathworks.com\/help\/matlab\/ref\/fplot.html the\r\n% doc page>. If you look down there, you'll see this:\r\n%\r\n% <html>\r\n% <h2>See Also<\/h2>\r\n% <h3>Functions<\/h3>\r\n% <ul><li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/fcontour.html\">fcontour<\/a><\/li>\r\n% <li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/fmesh.html\">fmesh<\/a><\/li>\r\n% <li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/fplot3.html\">fplot3<\/a><\/li>\r\n% <li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/fsurf.html\">fsurf<\/a><\/li><\/ul>\r\n% <\/html>\r\n%\r\n% That's right, there's now a whole family of fplot-like functions! \r\n%\r\n% Let's take a look at some of these new ones.\r\n%\r\n% First there's fplot3. It's used for 3D parametric curves, like the ones I\r\n% wrote about in these earlier posts on this blog\r\n% (<https:\/\/blogs.mathworks.com\/graphics\/2014\/12\/04\/tie-a-ribbon-round-it-parametric-curves-part-1\/\r\n% link1>, <https:\/\/blogs.mathworks.com\/graphics\/2014\/12\/16\/down-the-tubes\/\r\n% link2>). We just pass it 3 functions, like this:\r\n%\r\nfplot3(@(t) 3*cos(t)+cos(10*t).*cos(t), ...\r\n       @(t) 3*sin(t)+cos(10*t).*sin(t), ...\r\n       @(t) sin(10*t))\r\ndaspect([1 1 1])\r\n\r\n%%\r\n% Here's another of my favorite 3D parametric curves. It's commonly known \r\n% as the <http:\/\/paulbourke.net\/geometry\/baseball baseball curve>.\r\n%\r\na = .4;\r\nxfcn = @(t)sin(pi\/2-(pi\/2-a)*cos(t)).*cos(t\/2+a*sin(2*t));\r\nyfcn = @(t)sin(pi\/2-(pi\/2-a)*cos(t)).*sin(t\/2+a*sin(2*t));\r\nzfcn = @(t)cos(pi\/2-(pi\/2-a)*cos(t));\r\n\r\nfplot3(xfcn,yfcn,zfcn,[0 4*pi])\r\naxis equal\r\n\r\n%%\r\n% There's also the new <https:\/\/www.mathworks.com\/help\/matlab\/ref\/fsurf.html fsurf function>. \r\n% This means that instead of calling peaks to generate arrays of data ...\r\n%\r\n[x,y,z] = peaks;\r\n\r\n%%\r\n% ... and then passing that data to surf, ...\r\nsurf(x,y,z)\r\nxlim([-3 3])\r\nylim([-3 3])\r\ntitle('surf(peaks)')\r\n\r\n%%\r\n% I can just call fsurf with a handle to the peaks function. \r\n%\r\nfsurf(@(x,y) peaks(x,y),[-3 3 -3 3])\r\ntitle('fsurf(@(x,y) peaks(x,y))')\r\n\r\n%%\r\n% This results in a very similar picture, but look what happens when we zoom in.\r\n%\r\n% <<fsurf_zoom_animation.gif>>\r\n%\r\n% It's regenerating the mesh on the fly during the zoom. With the surf\r\n% command, we'd only get the resolution of our original call to peaks.\r\n%\r\n% <<surf_zoom_animation.gif>>\r\n%\r\n% This makes the fplot family really useful for exploring a function with\r\n% pan and zoom.\r\n%\r\n\r\n%%\r\n% All of the functions I've used so far have been simple enough to write as\r\n% anonymous functions. But sometimes you'll want to use more complex\r\n% functions. For example, I've turned the parametric equation for the \r\n% <http:\/\/www.kleinbottle.com\/ Klein bottle> into the \r\n% following set of functions:\r\n%\r\n%   function x = klein_xfcn(u,v)\r\n%     mask1 = u<pi;\r\n%     mask2 = ~mask1;\r\n%     r = klein_rfcn(u);\r\n%     x = zeros(size(u));\r\n%     x(mask1) = 6*cos(u(mask1)).*(1+sin(u(mask1))) + r(mask1).*cos(u(mask1)).*cos(v(mask1));\r\n%     x(mask2) = 6*cos(u(mask2)).*(1+sin(u(mask2))) + r(mask2).*cos(v(mask2)+pi);\r\n%\r\n%   function y = klein_yfcn(u,v)\r\n%     mask1 = u<pi;\r\n%     mask2 = ~mask1;\r\n%     r = klein_rfcn(u);\r\n%     y = zeros(size(u));\r\n%     y(mask1) = 16*sin(u(mask1)) + r(mask1).*sin(u(mask1)).*cos(v(mask1));\r\n%     y(mask2) = 16*sin(u(mask2));\r\n%\r\n%   function z = klein_zfcn(u,v)\r\n%     r = klein_rfcn(u);\r\n%     z = r.*sin(v);\r\n%\r\n%   function r = klein_rfcn(u)\r\n%     r = 4*(1-cos(u)\/2);\r\n%\r\n\r\n%%\r\n% And now I can use fsurf to make a Klein bottle.\r\n%\r\nh = fsurf(@klein_xfcn,@klein_yfcn,@klein_zfcn,[0 2*pi 0 2*pi]);\r\ncamlight\r\naxis equal\r\ntitle('Klein Bottle')\r\n\r\n%%\r\n% And we can get a better idea of its shape by making it partially transparent.\r\n%\r\nh.EdgeColor = 'none';\r\nh.FaceColor = [.929 .694 .125];\r\nh.FaceAlpha = .5;\r\nset(gca,'Projection','perspective')\r\n\r\n%%\r\n% There are a lot of goodies to play with in these functions, aren't there?\r\n% And we haven't even talked about\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/fmesh.html fmesh> and \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/fcontour.html fcontour>.\r\n%\r\n\r\n##### SOURCE END ##### f83762b60ce041f9a6ce5547cca373a6\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/graphics\/files\/fplot_thumbnail.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>\r\nAnother new feature that I really like in R2016a is the upgraded fplot function and all of the new members of the fplot family.The fplot function has been around for a long time. The basic idea is... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/graphics\/2016\/04\/20\/fplot-and-friends\/\">read more >><\/a><\/p>","protected":false},"author":89,"featured_media":496,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/posts\/475"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/users\/89"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/comments?post=475"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/posts\/475\/revisions"}],"predecessor-version":[{"id":497,"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/posts\/475\/revisions\/497"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/media\/496"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/media?parent=475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/categories?post=475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics\/wp-json\/wp\/v2\/tags?post=475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}