Numerical contour plot of compiled function












2












$begingroup$


I have come across a problem in Mathematica when I have tried to plot the contours of a compiled function.



f  = Compile[{{x}, {y}}, x^2 + y^2];
ContourPlot[f[x, y] == 0.1, {x, y} [Element] Disk[{0, 0}, 1]]


this works perfectly but suppose I want to only plot a single contour:



ContourPlot[f[x, y] == 0.1, {x, y} [Element] Disk[{0, 0}, 1]]


this then returns the error



compiled function error



but still plots the contour correctly. Evaluating f[x, y] == 0.1 gives x^2 + y^2 == 0.1 along with the same error message as the output so it's clear that the problem is that in order to plot the contour specified Mathematica requires the equation for the contour to be in symbolic form suggesting that ContourPlot is using a symbolic method to solve the problem.



However this is no good for anything difficult. I have a function longFunc[x,y] which is an extremely complicated expression and hence must be compiled and evaluated numerically. When I do a simple ContourPlot it works perfectly but if I specify a contour longFunc[x,y]==0.5 it falls back to a symbolic method and doesn't return an answer for a very long time.



So the question is: Is there an option within ContourPlot to force Mathematica to search for a solution to the equality using a numerical method?



Thank you!



Related:



ContourPlot is slow and unwieldy and generates a large-data graphic



Plot compiled function with LogLinearPlot










share|improve this question









$endgroup$

















    2












    $begingroup$


    I have come across a problem in Mathematica when I have tried to plot the contours of a compiled function.



    f  = Compile[{{x}, {y}}, x^2 + y^2];
    ContourPlot[f[x, y] == 0.1, {x, y} [Element] Disk[{0, 0}, 1]]


    this works perfectly but suppose I want to only plot a single contour:



    ContourPlot[f[x, y] == 0.1, {x, y} [Element] Disk[{0, 0}, 1]]


    this then returns the error



    compiled function error



    but still plots the contour correctly. Evaluating f[x, y] == 0.1 gives x^2 + y^2 == 0.1 along with the same error message as the output so it's clear that the problem is that in order to plot the contour specified Mathematica requires the equation for the contour to be in symbolic form suggesting that ContourPlot is using a symbolic method to solve the problem.



    However this is no good for anything difficult. I have a function longFunc[x,y] which is an extremely complicated expression and hence must be compiled and evaluated numerically. When I do a simple ContourPlot it works perfectly but if I specify a contour longFunc[x,y]==0.5 it falls back to a symbolic method and doesn't return an answer for a very long time.



    So the question is: Is there an option within ContourPlot to force Mathematica to search for a solution to the equality using a numerical method?



    Thank you!



    Related:



    ContourPlot is slow and unwieldy and generates a large-data graphic



    Plot compiled function with LogLinearPlot










    share|improve this question









    $endgroup$















      2












      2








      2





      $begingroup$


      I have come across a problem in Mathematica when I have tried to plot the contours of a compiled function.



      f  = Compile[{{x}, {y}}, x^2 + y^2];
      ContourPlot[f[x, y] == 0.1, {x, y} [Element] Disk[{0, 0}, 1]]


      this works perfectly but suppose I want to only plot a single contour:



      ContourPlot[f[x, y] == 0.1, {x, y} [Element] Disk[{0, 0}, 1]]


      this then returns the error



      compiled function error



      but still plots the contour correctly. Evaluating f[x, y] == 0.1 gives x^2 + y^2 == 0.1 along with the same error message as the output so it's clear that the problem is that in order to plot the contour specified Mathematica requires the equation for the contour to be in symbolic form suggesting that ContourPlot is using a symbolic method to solve the problem.



      However this is no good for anything difficult. I have a function longFunc[x,y] which is an extremely complicated expression and hence must be compiled and evaluated numerically. When I do a simple ContourPlot it works perfectly but if I specify a contour longFunc[x,y]==0.5 it falls back to a symbolic method and doesn't return an answer for a very long time.



      So the question is: Is there an option within ContourPlot to force Mathematica to search for a solution to the equality using a numerical method?



      Thank you!



      Related:



      ContourPlot is slow and unwieldy and generates a large-data graphic



      Plot compiled function with LogLinearPlot










      share|improve this question









      $endgroup$




      I have come across a problem in Mathematica when I have tried to plot the contours of a compiled function.



      f  = Compile[{{x}, {y}}, x^2 + y^2];
      ContourPlot[f[x, y] == 0.1, {x, y} [Element] Disk[{0, 0}, 1]]


      this works perfectly but suppose I want to only plot a single contour:



      ContourPlot[f[x, y] == 0.1, {x, y} [Element] Disk[{0, 0}, 1]]


      this then returns the error



      compiled function error



      but still plots the contour correctly. Evaluating f[x, y] == 0.1 gives x^2 + y^2 == 0.1 along with the same error message as the output so it's clear that the problem is that in order to plot the contour specified Mathematica requires the equation for the contour to be in symbolic form suggesting that ContourPlot is using a symbolic method to solve the problem.



      However this is no good for anything difficult. I have a function longFunc[x,y] which is an extremely complicated expression and hence must be compiled and evaluated numerically. When I do a simple ContourPlot it works perfectly but if I specify a contour longFunc[x,y]==0.5 it falls back to a symbolic method and doesn't return an answer for a very long time.



      So the question is: Is there an option within ContourPlot to force Mathematica to search for a solution to the equality using a numerical method?



      Thank you!



      Related:



      ContourPlot is slow and unwieldy and generates a large-data graphic



      Plot compiled function with LogLinearPlot







      plotting compile






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 3 hours ago









      TakodaTakoda

      1055




      1055






















          3 Answers
          3






          active

          oldest

          votes


















          3












          $begingroup$

          Ok I figured it out. You specify the contours you want through the Contours option of the function, surprisingly.



          f = Compile[{{x}, {y}}, x^2 + y^2];
          ContourPlot[f[x, y], {x, y} [Element] Disk[{0, 0}, 1],
          Contours -> {0.1}, ContourShading -> False]


          Works beautifully. If you specify an equality in the argument ContourPlot defaults to symbolic solving which is usually way too slow.






          share|improve this answer









          $endgroup$





















            1












            $begingroup$

            Try



            f = Compile[{{x, _Real}, {y, _Real}}, x^2 + y^2];
            ContourPlot[f[x, y] == 0.1, {x, y} [Element]Disk[{0, 0}, 1]]


            enter image description here






            share|improve this answer











            $endgroup$













            • $begingroup$
              Doesn't change anything. The problem is that when you specify a contour to plot, Mathematica reverts back to trying to symbolically solve the equality.
              $endgroup$
              – Takoda
              3 hours ago










            • $begingroup$
              Contour is evaluated without error message, so obviously somthing changed.
              $endgroup$
              – Ulrich Neumann
              2 hours ago










            • $begingroup$
              Not for me. I am using Mathematica 11.2 so that might be why.
              $endgroup$
              – Takoda
              2 hours ago



















            0












            $begingroup$

            Here are a couple more ideas for displaying the contour at $z=1/10$



            You could also define a helper function g that forces Plot3D to work with numeric values. Note: this will give a symbolic rather than a numeric tooltip for the contour.



            g[x_?NumericQ, y_?NumericQ] := f[x, y];
            ContourPlot[g[x, y] == 1/10, {x, -1, 1}, {y, -1, 1}, ContourShading -> False]


            contour



            You could also show the contour on the 3D surface defined by f:



            Plot3D[f[x, y], {x, y} ∈ Disk,
            MeshFunctions -> {#3 &},
            Mesh -> {{{1/10, Directive[Black, AbsoluteThickness[3.5]]}}},
            BoxRatios -> {1, 1, 1/3},
            ColorFunction -> (White &),
            Lighting -> "Neutral"]


            surface






            share|improve this answer











            $endgroup$













              Your Answer





              StackExchange.ifUsing("editor", function () {
              return StackExchange.using("mathjaxEditing", function () {
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
              });
              });
              }, "mathjax-editing");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "387"
              };
              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: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              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%2fmathematica.stackexchange.com%2fquestions%2f190057%2fnumerical-contour-plot-of-compiled-function%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









              3












              $begingroup$

              Ok I figured it out. You specify the contours you want through the Contours option of the function, surprisingly.



              f = Compile[{{x}, {y}}, x^2 + y^2];
              ContourPlot[f[x, y], {x, y} [Element] Disk[{0, 0}, 1],
              Contours -> {0.1}, ContourShading -> False]


              Works beautifully. If you specify an equality in the argument ContourPlot defaults to symbolic solving which is usually way too slow.






              share|improve this answer









              $endgroup$


















                3












                $begingroup$

                Ok I figured it out. You specify the contours you want through the Contours option of the function, surprisingly.



                f = Compile[{{x}, {y}}, x^2 + y^2];
                ContourPlot[f[x, y], {x, y} [Element] Disk[{0, 0}, 1],
                Contours -> {0.1}, ContourShading -> False]


                Works beautifully. If you specify an equality in the argument ContourPlot defaults to symbolic solving which is usually way too slow.






                share|improve this answer









                $endgroup$
















                  3












                  3








                  3





                  $begingroup$

                  Ok I figured it out. You specify the contours you want through the Contours option of the function, surprisingly.



                  f = Compile[{{x}, {y}}, x^2 + y^2];
                  ContourPlot[f[x, y], {x, y} [Element] Disk[{0, 0}, 1],
                  Contours -> {0.1}, ContourShading -> False]


                  Works beautifully. If you specify an equality in the argument ContourPlot defaults to symbolic solving which is usually way too slow.






                  share|improve this answer









                  $endgroup$



                  Ok I figured it out. You specify the contours you want through the Contours option of the function, surprisingly.



                  f = Compile[{{x}, {y}}, x^2 + y^2];
                  ContourPlot[f[x, y], {x, y} [Element] Disk[{0, 0}, 1],
                  Contours -> {0.1}, ContourShading -> False]


                  Works beautifully. If you specify an equality in the argument ContourPlot defaults to symbolic solving which is usually way too slow.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 hours ago









                  TakodaTakoda

                  1055




                  1055























                      1












                      $begingroup$

                      Try



                      f = Compile[{{x, _Real}, {y, _Real}}, x^2 + y^2];
                      ContourPlot[f[x, y] == 0.1, {x, y} [Element]Disk[{0, 0}, 1]]


                      enter image description here






                      share|improve this answer











                      $endgroup$













                      • $begingroup$
                        Doesn't change anything. The problem is that when you specify a contour to plot, Mathematica reverts back to trying to symbolically solve the equality.
                        $endgroup$
                        – Takoda
                        3 hours ago










                      • $begingroup$
                        Contour is evaluated without error message, so obviously somthing changed.
                        $endgroup$
                        – Ulrich Neumann
                        2 hours ago










                      • $begingroup$
                        Not for me. I am using Mathematica 11.2 so that might be why.
                        $endgroup$
                        – Takoda
                        2 hours ago
















                      1












                      $begingroup$

                      Try



                      f = Compile[{{x, _Real}, {y, _Real}}, x^2 + y^2];
                      ContourPlot[f[x, y] == 0.1, {x, y} [Element]Disk[{0, 0}, 1]]


                      enter image description here






                      share|improve this answer











                      $endgroup$













                      • $begingroup$
                        Doesn't change anything. The problem is that when you specify a contour to plot, Mathematica reverts back to trying to symbolically solve the equality.
                        $endgroup$
                        – Takoda
                        3 hours ago










                      • $begingroup$
                        Contour is evaluated without error message, so obviously somthing changed.
                        $endgroup$
                        – Ulrich Neumann
                        2 hours ago










                      • $begingroup$
                        Not for me. I am using Mathematica 11.2 so that might be why.
                        $endgroup$
                        – Takoda
                        2 hours ago














                      1












                      1








                      1





                      $begingroup$

                      Try



                      f = Compile[{{x, _Real}, {y, _Real}}, x^2 + y^2];
                      ContourPlot[f[x, y] == 0.1, {x, y} [Element]Disk[{0, 0}, 1]]


                      enter image description here






                      share|improve this answer











                      $endgroup$



                      Try



                      f = Compile[{{x, _Real}, {y, _Real}}, x^2 + y^2];
                      ContourPlot[f[x, y] == 0.1, {x, y} [Element]Disk[{0, 0}, 1]]


                      enter image description here







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 2 hours ago

























                      answered 3 hours ago









                      Ulrich NeumannUlrich Neumann

                      8,141516




                      8,141516












                      • $begingroup$
                        Doesn't change anything. The problem is that when you specify a contour to plot, Mathematica reverts back to trying to symbolically solve the equality.
                        $endgroup$
                        – Takoda
                        3 hours ago










                      • $begingroup$
                        Contour is evaluated without error message, so obviously somthing changed.
                        $endgroup$
                        – Ulrich Neumann
                        2 hours ago










                      • $begingroup$
                        Not for me. I am using Mathematica 11.2 so that might be why.
                        $endgroup$
                        – Takoda
                        2 hours ago


















                      • $begingroup$
                        Doesn't change anything. The problem is that when you specify a contour to plot, Mathematica reverts back to trying to symbolically solve the equality.
                        $endgroup$
                        – Takoda
                        3 hours ago










                      • $begingroup$
                        Contour is evaluated without error message, so obviously somthing changed.
                        $endgroup$
                        – Ulrich Neumann
                        2 hours ago










                      • $begingroup$
                        Not for me. I am using Mathematica 11.2 so that might be why.
                        $endgroup$
                        – Takoda
                        2 hours ago
















                      $begingroup$
                      Doesn't change anything. The problem is that when you specify a contour to plot, Mathematica reverts back to trying to symbolically solve the equality.
                      $endgroup$
                      – Takoda
                      3 hours ago




                      $begingroup$
                      Doesn't change anything. The problem is that when you specify a contour to plot, Mathematica reverts back to trying to symbolically solve the equality.
                      $endgroup$
                      – Takoda
                      3 hours ago












                      $begingroup$
                      Contour is evaluated without error message, so obviously somthing changed.
                      $endgroup$
                      – Ulrich Neumann
                      2 hours ago




                      $begingroup$
                      Contour is evaluated without error message, so obviously somthing changed.
                      $endgroup$
                      – Ulrich Neumann
                      2 hours ago












                      $begingroup$
                      Not for me. I am using Mathematica 11.2 so that might be why.
                      $endgroup$
                      – Takoda
                      2 hours ago




                      $begingroup$
                      Not for me. I am using Mathematica 11.2 so that might be why.
                      $endgroup$
                      – Takoda
                      2 hours ago











                      0












                      $begingroup$

                      Here are a couple more ideas for displaying the contour at $z=1/10$



                      You could also define a helper function g that forces Plot3D to work with numeric values. Note: this will give a symbolic rather than a numeric tooltip for the contour.



                      g[x_?NumericQ, y_?NumericQ] := f[x, y];
                      ContourPlot[g[x, y] == 1/10, {x, -1, 1}, {y, -1, 1}, ContourShading -> False]


                      contour



                      You could also show the contour on the 3D surface defined by f:



                      Plot3D[f[x, y], {x, y} ∈ Disk,
                      MeshFunctions -> {#3 &},
                      Mesh -> {{{1/10, Directive[Black, AbsoluteThickness[3.5]]}}},
                      BoxRatios -> {1, 1, 1/3},
                      ColorFunction -> (White &),
                      Lighting -> "Neutral"]


                      surface






                      share|improve this answer











                      $endgroup$


















                        0












                        $begingroup$

                        Here are a couple more ideas for displaying the contour at $z=1/10$



                        You could also define a helper function g that forces Plot3D to work with numeric values. Note: this will give a symbolic rather than a numeric tooltip for the contour.



                        g[x_?NumericQ, y_?NumericQ] := f[x, y];
                        ContourPlot[g[x, y] == 1/10, {x, -1, 1}, {y, -1, 1}, ContourShading -> False]


                        contour



                        You could also show the contour on the 3D surface defined by f:



                        Plot3D[f[x, y], {x, y} ∈ Disk,
                        MeshFunctions -> {#3 &},
                        Mesh -> {{{1/10, Directive[Black, AbsoluteThickness[3.5]]}}},
                        BoxRatios -> {1, 1, 1/3},
                        ColorFunction -> (White &),
                        Lighting -> "Neutral"]


                        surface






                        share|improve this answer











                        $endgroup$
















                          0












                          0








                          0





                          $begingroup$

                          Here are a couple more ideas for displaying the contour at $z=1/10$



                          You could also define a helper function g that forces Plot3D to work with numeric values. Note: this will give a symbolic rather than a numeric tooltip for the contour.



                          g[x_?NumericQ, y_?NumericQ] := f[x, y];
                          ContourPlot[g[x, y] == 1/10, {x, -1, 1}, {y, -1, 1}, ContourShading -> False]


                          contour



                          You could also show the contour on the 3D surface defined by f:



                          Plot3D[f[x, y], {x, y} ∈ Disk,
                          MeshFunctions -> {#3 &},
                          Mesh -> {{{1/10, Directive[Black, AbsoluteThickness[3.5]]}}},
                          BoxRatios -> {1, 1, 1/3},
                          ColorFunction -> (White &),
                          Lighting -> "Neutral"]


                          surface






                          share|improve this answer











                          $endgroup$



                          Here are a couple more ideas for displaying the contour at $z=1/10$



                          You could also define a helper function g that forces Plot3D to work with numeric values. Note: this will give a symbolic rather than a numeric tooltip for the contour.



                          g[x_?NumericQ, y_?NumericQ] := f[x, y];
                          ContourPlot[g[x, y] == 1/10, {x, -1, 1}, {y, -1, 1}, ContourShading -> False]


                          contour



                          You could also show the contour on the 3D surface defined by f:



                          Plot3D[f[x, y], {x, y} ∈ Disk,
                          MeshFunctions -> {#3 &},
                          Mesh -> {{{1/10, Directive[Black, AbsoluteThickness[3.5]]}}},
                          BoxRatios -> {1, 1, 1/3},
                          ColorFunction -> (White &),
                          Lighting -> "Neutral"]


                          surface







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 14 mins ago

























                          answered 19 mins ago









                          m_goldbergm_goldberg

                          85k872196




                          85k872196






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Mathematica Stack Exchange!


                              • 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.


                              Use MathJax to format equations. MathJax reference.


                              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%2fmathematica.stackexchange.com%2fquestions%2f190057%2fnumerical-contour-plot-of-compiled-function%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

                              Callistus I

                              Tabula Rosettana

                              How to label and detect the document text images