How can I plot a function with two variables in octave or matlab?










6














I want to use octave to plot fairly simple functions with two variables like : f(x,y) = x^2 + 3y . It is very easy to plot single variable functions, but I am having alot of trouble finding out how to do two variable functions. Does anyone know the best way of doing this? I would really appreciate any suggestions.



Thanks,
Zach










share|improve this question




























    6














    I want to use octave to plot fairly simple functions with two variables like : f(x,y) = x^2 + 3y . It is very easy to plot single variable functions, but I am having alot of trouble finding out how to do two variable functions. Does anyone know the best way of doing this? I would really appreciate any suggestions.



    Thanks,
    Zach










    share|improve this question


























      6












      6








      6


      2





      I want to use octave to plot fairly simple functions with two variables like : f(x,y) = x^2 + 3y . It is very easy to plot single variable functions, but I am having alot of trouble finding out how to do two variable functions. Does anyone know the best way of doing this? I would really appreciate any suggestions.



      Thanks,
      Zach










      share|improve this question















      I want to use octave to plot fairly simple functions with two variables like : f(x,y) = x^2 + 3y . It is very easy to plot single variable functions, but I am having alot of trouble finding out how to do two variable functions. Does anyone know the best way of doing this? I would really appreciate any suggestions.



      Thanks,
      Zach







      matlab plot octave






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 23 '17 at 6:45









      Christoph

      38.4k846116




      38.4k846116










      asked Jun 1 '13 at 1:08









      Zach

      62311029




      62311029






















          3 Answers
          3






          active

          oldest

          votes


















          9














          Plotting a function of two variables would normally mean a 3-dimensional plot - in MATLAB you would use the function plot3 for that. To plot your function f(x,y) in the interval [-10,10] for both X and Y, you could use the following commands:



          x = [-10:.1:10];
          y = [-10:.1:10];
          plot3(x, y, x.^2 + 3*y)
          grid on





          share|improve this answer




















          • Thank you very much, it works perfectly in octave!
            – Zach
            Jun 1 '13 at 4:23


















          2














          In case it may help someone out there... I ran in Octave the code in the accepted answer and I got this plot:



          enter image description here



          But I really wanted the function for every point in the Cartesian product of x and y, not just along the diagonal, so I used the function mesh to get this 3D plot with the projected contour lines in the x,y plane:



          x = [-10:.1:10];
          y = [-10:.1:10];
          [xx, yy] = meshgrid (x, y);
          z = xx.^2 + 3*yy;
          mesh(x, y, z)
          meshc(xx,yy,z)
          xlabel ("x");
          ylabel ("y");
          zlabel ("f(x,y)");
          title ("f(x,y) = x^2 + 3y");
          grid on


          enter image description here



          To get rid of the mesh-wire texture of the plot, the function surf did the trick:



          x = [-10:.1:10]; 
          y = [-10:.1:10];
          [xx, yy] = meshgrid (x, y);
          z = xx.^2 + 3*yy;
          h = surf(xx,yy,z);
          colormap hsv;
          set(h,'linestyle','none');
          xlabel ("x");
          ylabel ("y");
          zlabel ("f(x,y)");
          title ("f(x,y) = x^2 + 3y");


          enter image description here



          Another way to plot is as a heatmap with contour lines:



          x = [-10:.1:10];
          y = [-10:.1:10];
          [xx, yy] = meshgrid (x, y);
          z = xx.^2 + yy.*3;
          contourf(xx,yy,z);
          colormap hsv;
          xlabel ("x");
          ylabel ("y");
          zlabel ("f(x,y)");
          title ("f(x,y) = x^2 + 3y");
          grid on


          enter image description here



          And for completeness, the levels can be labeled:



          x = [-10:.1:10];
          y = [-10:.1:10];
          [xx, yy] = meshgrid (x, y);
          z = xx.^2 + 3*yy;
          [C,h] = contour(xx,yy,z);
          clabel(C,h)
          xlabel ("x");
          ylabel ("y");
          zlabel ("f(x,y)");
          title ("f(x,y) = x^2 + 3y");
          grid on


          enter image description here






          share|improve this answer


















          • 1




            This is a superior answer. The visuals help beginners searching for assistance best with things like this. Showing multiple options provides highest value.
            – SecretAgentMan
            Nov 11 '18 at 15:47










          • All you need now is surfc (see documentation) to make this answer canonical. I hope you add.
            – SecretAgentMan
            Nov 11 '18 at 16:12



















          1














          In addition to the excellent answers from @Toni and @esskov, for future plotters of functions with two variables, the contour and contourf functions are useful for some applications.



          Contour & Filled Contour Plots



          MATLAB Code (2018b):



          x = [-10:.1:10]; 
          y = [-20:.1:20];
          [xx, yy] = meshgrid (x, y);
          z = xx.^2 + 3*yy; % Borrowed 4 lines from @Toni

          figure
          s(1) = subplot(1,2,1), hold on % Left Plot
          [M,c] = contour(xx,yy,z); % Contour Plot
          c.ShowText = 'on'; % Label Contours
          c.LineWidth = 1.2; % Contour Line Width
          xlabel('X')
          ylabel('Y')
          box on
          s(2) = subplot(1,2,2), hold on % Right Plot
          [M2,c2] = contourf(xx,yy,z);
          colorbar % Add Colorbar
          xlabel('X')
          ylabel('Y')
          box on
          title(s(1),'Contour Plot')
          title(s(2),'Filled Contour Plot')



          Update: Added example of surfc



          3D Surface with contours below



          h = surfc(xx,yy,z)





          share|improve this answer






















          • I didn't realize you had added the contour plot when I edited my answer with a flat contour plot.
            – Toni
            Nov 11 '18 at 16:08











          • @Toni No worries. Your answer was masterful and I just thought this should be added too. I see you added contourf at same time as my post. Your answer is now just about canonical...
            – SecretAgentMan
            Nov 11 '18 at 16:09











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16868074%2fhow-can-i-plot-a-function-with-two-variables-in-octave-or-matlab%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          9














          Plotting a function of two variables would normally mean a 3-dimensional plot - in MATLAB you would use the function plot3 for that. To plot your function f(x,y) in the interval [-10,10] for both X and Y, you could use the following commands:



          x = [-10:.1:10];
          y = [-10:.1:10];
          plot3(x, y, x.^2 + 3*y)
          grid on





          share|improve this answer




















          • Thank you very much, it works perfectly in octave!
            – Zach
            Jun 1 '13 at 4:23















          9














          Plotting a function of two variables would normally mean a 3-dimensional plot - in MATLAB you would use the function plot3 for that. To plot your function f(x,y) in the interval [-10,10] for both X and Y, you could use the following commands:



          x = [-10:.1:10];
          y = [-10:.1:10];
          plot3(x, y, x.^2 + 3*y)
          grid on





          share|improve this answer




















          • Thank you very much, it works perfectly in octave!
            – Zach
            Jun 1 '13 at 4:23













          9












          9








          9






          Plotting a function of two variables would normally mean a 3-dimensional plot - in MATLAB you would use the function plot3 for that. To plot your function f(x,y) in the interval [-10,10] for both X and Y, you could use the following commands:



          x = [-10:.1:10];
          y = [-10:.1:10];
          plot3(x, y, x.^2 + 3*y)
          grid on





          share|improve this answer












          Plotting a function of two variables would normally mean a 3-dimensional plot - in MATLAB you would use the function plot3 for that. To plot your function f(x,y) in the interval [-10,10] for both X and Y, you could use the following commands:



          x = [-10:.1:10];
          y = [-10:.1:10];
          plot3(x, y, x.^2 + 3*y)
          grid on






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jun 1 '13 at 1:29









          esskov

          33825




          33825











          • Thank you very much, it works perfectly in octave!
            – Zach
            Jun 1 '13 at 4:23
















          • Thank you very much, it works perfectly in octave!
            – Zach
            Jun 1 '13 at 4:23















          Thank you very much, it works perfectly in octave!
          – Zach
          Jun 1 '13 at 4:23




          Thank you very much, it works perfectly in octave!
          – Zach
          Jun 1 '13 at 4:23













          2














          In case it may help someone out there... I ran in Octave the code in the accepted answer and I got this plot:



          enter image description here



          But I really wanted the function for every point in the Cartesian product of x and y, not just along the diagonal, so I used the function mesh to get this 3D plot with the projected contour lines in the x,y plane:



          x = [-10:.1:10];
          y = [-10:.1:10];
          [xx, yy] = meshgrid (x, y);
          z = xx.^2 + 3*yy;
          mesh(x, y, z)
          meshc(xx,yy,z)
          xlabel ("x");
          ylabel ("y");
          zlabel ("f(x,y)");
          title ("f(x,y) = x^2 + 3y");
          grid on


          enter image description here



          To get rid of the mesh-wire texture of the plot, the function surf did the trick:



          x = [-10:.1:10]; 
          y = [-10:.1:10];
          [xx, yy] = meshgrid (x, y);
          z = xx.^2 + 3*yy;
          h = surf(xx,yy,z);
          colormap hsv;
          set(h,'linestyle','none');
          xlabel ("x");
          ylabel ("y");
          zlabel ("f(x,y)");
          title ("f(x,y) = x^2 + 3y");


          enter image description here



          Another way to plot is as a heatmap with contour lines:



          x = [-10:.1:10];
          y = [-10:.1:10];
          [xx, yy] = meshgrid (x, y);
          z = xx.^2 + yy.*3;
          contourf(xx,yy,z);
          colormap hsv;
          xlabel ("x");
          ylabel ("y");
          zlabel ("f(x,y)");
          title ("f(x,y) = x^2 + 3y");
          grid on


          enter image description here



          And for completeness, the levels can be labeled:



          x = [-10:.1:10];
          y = [-10:.1:10];
          [xx, yy] = meshgrid (x, y);
          z = xx.^2 + 3*yy;
          [C,h] = contour(xx,yy,z);
          clabel(C,h)
          xlabel ("x");
          ylabel ("y");
          zlabel ("f(x,y)");
          title ("f(x,y) = x^2 + 3y");
          grid on


          enter image description here






          share|improve this answer


















          • 1




            This is a superior answer. The visuals help beginners searching for assistance best with things like this. Showing multiple options provides highest value.
            – SecretAgentMan
            Nov 11 '18 at 15:47










          • All you need now is surfc (see documentation) to make this answer canonical. I hope you add.
            – SecretAgentMan
            Nov 11 '18 at 16:12
















          2














          In case it may help someone out there... I ran in Octave the code in the accepted answer and I got this plot:



          enter image description here



          But I really wanted the function for every point in the Cartesian product of x and y, not just along the diagonal, so I used the function mesh to get this 3D plot with the projected contour lines in the x,y plane:



          x = [-10:.1:10];
          y = [-10:.1:10];
          [xx, yy] = meshgrid (x, y);
          z = xx.^2 + 3*yy;
          mesh(x, y, z)
          meshc(xx,yy,z)
          xlabel ("x");
          ylabel ("y");
          zlabel ("f(x,y)");
          title ("f(x,y) = x^2 + 3y");
          grid on


          enter image description here



          To get rid of the mesh-wire texture of the plot, the function surf did the trick:



          x = [-10:.1:10]; 
          y = [-10:.1:10];
          [xx, yy] = meshgrid (x, y);
          z = xx.^2 + 3*yy;
          h = surf(xx,yy,z);
          colormap hsv;
          set(h,'linestyle','none');
          xlabel ("x");
          ylabel ("y");
          zlabel ("f(x,y)");
          title ("f(x,y) = x^2 + 3y");


          enter image description here



          Another way to plot is as a heatmap with contour lines:



          x = [-10:.1:10];
          y = [-10:.1:10];
          [xx, yy] = meshgrid (x, y);
          z = xx.^2 + yy.*3;
          contourf(xx,yy,z);
          colormap hsv;
          xlabel ("x");
          ylabel ("y");
          zlabel ("f(x,y)");
          title ("f(x,y) = x^2 + 3y");
          grid on


          enter image description here



          And for completeness, the levels can be labeled:



          x = [-10:.1:10];
          y = [-10:.1:10];
          [xx, yy] = meshgrid (x, y);
          z = xx.^2 + 3*yy;
          [C,h] = contour(xx,yy,z);
          clabel(C,h)
          xlabel ("x");
          ylabel ("y");
          zlabel ("f(x,y)");
          title ("f(x,y) = x^2 + 3y");
          grid on


          enter image description here






          share|improve this answer


















          • 1




            This is a superior answer. The visuals help beginners searching for assistance best with things like this. Showing multiple options provides highest value.
            – SecretAgentMan
            Nov 11 '18 at 15:47










          • All you need now is surfc (see documentation) to make this answer canonical. I hope you add.
            – SecretAgentMan
            Nov 11 '18 at 16:12














          2












          2








          2






          In case it may help someone out there... I ran in Octave the code in the accepted answer and I got this plot:



          enter image description here



          But I really wanted the function for every point in the Cartesian product of x and y, not just along the diagonal, so I used the function mesh to get this 3D plot with the projected contour lines in the x,y plane:



          x = [-10:.1:10];
          y = [-10:.1:10];
          [xx, yy] = meshgrid (x, y);
          z = xx.^2 + 3*yy;
          mesh(x, y, z)
          meshc(xx,yy,z)
          xlabel ("x");
          ylabel ("y");
          zlabel ("f(x,y)");
          title ("f(x,y) = x^2 + 3y");
          grid on


          enter image description here



          To get rid of the mesh-wire texture of the plot, the function surf did the trick:



          x = [-10:.1:10]; 
          y = [-10:.1:10];
          [xx, yy] = meshgrid (x, y);
          z = xx.^2 + 3*yy;
          h = surf(xx,yy,z);
          colormap hsv;
          set(h,'linestyle','none');
          xlabel ("x");
          ylabel ("y");
          zlabel ("f(x,y)");
          title ("f(x,y) = x^2 + 3y");


          enter image description here



          Another way to plot is as a heatmap with contour lines:



          x = [-10:.1:10];
          y = [-10:.1:10];
          [xx, yy] = meshgrid (x, y);
          z = xx.^2 + yy.*3;
          contourf(xx,yy,z);
          colormap hsv;
          xlabel ("x");
          ylabel ("y");
          zlabel ("f(x,y)");
          title ("f(x,y) = x^2 + 3y");
          grid on


          enter image description here



          And for completeness, the levels can be labeled:



          x = [-10:.1:10];
          y = [-10:.1:10];
          [xx, yy] = meshgrid (x, y);
          z = xx.^2 + 3*yy;
          [C,h] = contour(xx,yy,z);
          clabel(C,h)
          xlabel ("x");
          ylabel ("y");
          zlabel ("f(x,y)");
          title ("f(x,y) = x^2 + 3y");
          grid on


          enter image description here






          share|improve this answer














          In case it may help someone out there... I ran in Octave the code in the accepted answer and I got this plot:



          enter image description here



          But I really wanted the function for every point in the Cartesian product of x and y, not just along the diagonal, so I used the function mesh to get this 3D plot with the projected contour lines in the x,y plane:



          x = [-10:.1:10];
          y = [-10:.1:10];
          [xx, yy] = meshgrid (x, y);
          z = xx.^2 + 3*yy;
          mesh(x, y, z)
          meshc(xx,yy,z)
          xlabel ("x");
          ylabel ("y");
          zlabel ("f(x,y)");
          title ("f(x,y) = x^2 + 3y");
          grid on


          enter image description here



          To get rid of the mesh-wire texture of the plot, the function surf did the trick:



          x = [-10:.1:10]; 
          y = [-10:.1:10];
          [xx, yy] = meshgrid (x, y);
          z = xx.^2 + 3*yy;
          h = surf(xx,yy,z);
          colormap hsv;
          set(h,'linestyle','none');
          xlabel ("x");
          ylabel ("y");
          zlabel ("f(x,y)");
          title ("f(x,y) = x^2 + 3y");


          enter image description here



          Another way to plot is as a heatmap with contour lines:



          x = [-10:.1:10];
          y = [-10:.1:10];
          [xx, yy] = meshgrid (x, y);
          z = xx.^2 + yy.*3;
          contourf(xx,yy,z);
          colormap hsv;
          xlabel ("x");
          ylabel ("y");
          zlabel ("f(x,y)");
          title ("f(x,y) = x^2 + 3y");
          grid on


          enter image description here



          And for completeness, the levels can be labeled:



          x = [-10:.1:10];
          y = [-10:.1:10];
          [xx, yy] = meshgrid (x, y);
          z = xx.^2 + 3*yy;
          [C,h] = contour(xx,yy,z);
          clabel(C,h)
          xlabel ("x");
          ylabel ("y");
          zlabel ("f(x,y)");
          title ("f(x,y) = x^2 + 3y");
          grid on


          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 '18 at 23:43

























          answered Nov 11 '18 at 14:53









          Toni

          1,4531755




          1,4531755







          • 1




            This is a superior answer. The visuals help beginners searching for assistance best with things like this. Showing multiple options provides highest value.
            – SecretAgentMan
            Nov 11 '18 at 15:47










          • All you need now is surfc (see documentation) to make this answer canonical. I hope you add.
            – SecretAgentMan
            Nov 11 '18 at 16:12













          • 1




            This is a superior answer. The visuals help beginners searching for assistance best with things like this. Showing multiple options provides highest value.
            – SecretAgentMan
            Nov 11 '18 at 15:47










          • All you need now is surfc (see documentation) to make this answer canonical. I hope you add.
            – SecretAgentMan
            Nov 11 '18 at 16:12








          1




          1




          This is a superior answer. The visuals help beginners searching for assistance best with things like this. Showing multiple options provides highest value.
          – SecretAgentMan
          Nov 11 '18 at 15:47




          This is a superior answer. The visuals help beginners searching for assistance best with things like this. Showing multiple options provides highest value.
          – SecretAgentMan
          Nov 11 '18 at 15:47












          All you need now is surfc (see documentation) to make this answer canonical. I hope you add.
          – SecretAgentMan
          Nov 11 '18 at 16:12





          All you need now is surfc (see documentation) to make this answer canonical. I hope you add.
          – SecretAgentMan
          Nov 11 '18 at 16:12












          1














          In addition to the excellent answers from @Toni and @esskov, for future plotters of functions with two variables, the contour and contourf functions are useful for some applications.



          Contour & Filled Contour Plots



          MATLAB Code (2018b):



          x = [-10:.1:10]; 
          y = [-20:.1:20];
          [xx, yy] = meshgrid (x, y);
          z = xx.^2 + 3*yy; % Borrowed 4 lines from @Toni

          figure
          s(1) = subplot(1,2,1), hold on % Left Plot
          [M,c] = contour(xx,yy,z); % Contour Plot
          c.ShowText = 'on'; % Label Contours
          c.LineWidth = 1.2; % Contour Line Width
          xlabel('X')
          ylabel('Y')
          box on
          s(2) = subplot(1,2,2), hold on % Right Plot
          [M2,c2] = contourf(xx,yy,z);
          colorbar % Add Colorbar
          xlabel('X')
          ylabel('Y')
          box on
          title(s(1),'Contour Plot')
          title(s(2),'Filled Contour Plot')



          Update: Added example of surfc



          3D Surface with contours below



          h = surfc(xx,yy,z)





          share|improve this answer






















          • I didn't realize you had added the contour plot when I edited my answer with a flat contour plot.
            – Toni
            Nov 11 '18 at 16:08











          • @Toni No worries. Your answer was masterful and I just thought this should be added too. I see you added contourf at same time as my post. Your answer is now just about canonical...
            – SecretAgentMan
            Nov 11 '18 at 16:09
















          1














          In addition to the excellent answers from @Toni and @esskov, for future plotters of functions with two variables, the contour and contourf functions are useful for some applications.



          Contour & Filled Contour Plots



          MATLAB Code (2018b):



          x = [-10:.1:10]; 
          y = [-20:.1:20];
          [xx, yy] = meshgrid (x, y);
          z = xx.^2 + 3*yy; % Borrowed 4 lines from @Toni

          figure
          s(1) = subplot(1,2,1), hold on % Left Plot
          [M,c] = contour(xx,yy,z); % Contour Plot
          c.ShowText = 'on'; % Label Contours
          c.LineWidth = 1.2; % Contour Line Width
          xlabel('X')
          ylabel('Y')
          box on
          s(2) = subplot(1,2,2), hold on % Right Plot
          [M2,c2] = contourf(xx,yy,z);
          colorbar % Add Colorbar
          xlabel('X')
          ylabel('Y')
          box on
          title(s(1),'Contour Plot')
          title(s(2),'Filled Contour Plot')



          Update: Added example of surfc



          3D Surface with contours below



          h = surfc(xx,yy,z)





          share|improve this answer






















          • I didn't realize you had added the contour plot when I edited my answer with a flat contour plot.
            – Toni
            Nov 11 '18 at 16:08











          • @Toni No worries. Your answer was masterful and I just thought this should be added too. I see you added contourf at same time as my post. Your answer is now just about canonical...
            – SecretAgentMan
            Nov 11 '18 at 16:09














          1












          1








          1






          In addition to the excellent answers from @Toni and @esskov, for future plotters of functions with two variables, the contour and contourf functions are useful for some applications.



          Contour & Filled Contour Plots



          MATLAB Code (2018b):



          x = [-10:.1:10]; 
          y = [-20:.1:20];
          [xx, yy] = meshgrid (x, y);
          z = xx.^2 + 3*yy; % Borrowed 4 lines from @Toni

          figure
          s(1) = subplot(1,2,1), hold on % Left Plot
          [M,c] = contour(xx,yy,z); % Contour Plot
          c.ShowText = 'on'; % Label Contours
          c.LineWidth = 1.2; % Contour Line Width
          xlabel('X')
          ylabel('Y')
          box on
          s(2) = subplot(1,2,2), hold on % Right Plot
          [M2,c2] = contourf(xx,yy,z);
          colorbar % Add Colorbar
          xlabel('X')
          ylabel('Y')
          box on
          title(s(1),'Contour Plot')
          title(s(2),'Filled Contour Plot')



          Update: Added example of surfc



          3D Surface with contours below



          h = surfc(xx,yy,z)





          share|improve this answer














          In addition to the excellent answers from @Toni and @esskov, for future plotters of functions with two variables, the contour and contourf functions are useful for some applications.



          Contour & Filled Contour Plots



          MATLAB Code (2018b):



          x = [-10:.1:10]; 
          y = [-20:.1:20];
          [xx, yy] = meshgrid (x, y);
          z = xx.^2 + 3*yy; % Borrowed 4 lines from @Toni

          figure
          s(1) = subplot(1,2,1), hold on % Left Plot
          [M,c] = contour(xx,yy,z); % Contour Plot
          c.ShowText = 'on'; % Label Contours
          c.LineWidth = 1.2; % Contour Line Width
          xlabel('X')
          ylabel('Y')
          box on
          s(2) = subplot(1,2,2), hold on % Right Plot
          [M2,c2] = contourf(xx,yy,z);
          colorbar % Add Colorbar
          xlabel('X')
          ylabel('Y')
          box on
          title(s(1),'Contour Plot')
          title(s(2),'Filled Contour Plot')



          Update: Added example of surfc



          3D Surface with contours below



          h = surfc(xx,yy,z)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 '18 at 18:22

























          answered Nov 11 '18 at 16:06









          SecretAgentMan

          579213




          579213











          • I didn't realize you had added the contour plot when I edited my answer with a flat contour plot.
            – Toni
            Nov 11 '18 at 16:08











          • @Toni No worries. Your answer was masterful and I just thought this should be added too. I see you added contourf at same time as my post. Your answer is now just about canonical...
            – SecretAgentMan
            Nov 11 '18 at 16:09

















          • I didn't realize you had added the contour plot when I edited my answer with a flat contour plot.
            – Toni
            Nov 11 '18 at 16:08











          • @Toni No worries. Your answer was masterful and I just thought this should be added too. I see you added contourf at same time as my post. Your answer is now just about canonical...
            – SecretAgentMan
            Nov 11 '18 at 16:09
















          I didn't realize you had added the contour plot when I edited my answer with a flat contour plot.
          – Toni
          Nov 11 '18 at 16:08





          I didn't realize you had added the contour plot when I edited my answer with a flat contour plot.
          – Toni
          Nov 11 '18 at 16:08













          @Toni No worries. Your answer was masterful and I just thought this should be added too. I see you added contourf at same time as my post. Your answer is now just about canonical...
          – SecretAgentMan
          Nov 11 '18 at 16:09





          @Toni No worries. Your answer was masterful and I just thought this should be added too. I see you added contourf at same time as my post. Your answer is now just about canonical...
          – SecretAgentMan
          Nov 11 '18 at 16:09


















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16868074%2fhow-can-i-plot-a-function-with-two-variables-in-octave-or-matlab%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kleinkühnau

          Makov (Slowakei)

          Deutsches Schauspielhaus