How to plot function sin(x)^2 / x^2












12














The above function looks like this:



enter image description here



However, using tikzpicture with addplot



documentclass[11pt]{article}
usepackage{pgfplots}
usepackage{tikz}
begin{document}
begin{tikzpicture}
begin{axis}
addplot[domain=-6:6,samples=50,smooth,red] { (sin(x)^2)/pow(x,2) };
end{axis}
end{tikzpicture}
end{document}


Gives the following disturbing result:



enter image description here



How should I plot sin(x)^2 / x^2 in tikz?










share|improve this question




















  • 3




    (1) welcome, (2) as always on this site you are much much more likely to get help if you provide a full minimal example that others can copy and test as is. Then they will not have to guess 80% of your setup.
    – daleif
    Nov 20 '18 at 10:53








  • 1




    Thanks for the tip, I've done that now!
    – MrMartin
    Nov 20 '18 at 10:57






  • 8




    Try more samples and sin(deg(x))
    – daleif
    Nov 20 '18 at 10:58










  • Yes, it seems sin(deg(x)) is required when plotting sin(x). Thanks!
    – MrMartin
    Nov 20 '18 at 11:23






  • 1




    @daleif Damn! I did not see your comment :) Also sampling is just as fine, as it is now. Only deg(...) is necessary with sin.
    – Raaja
    Nov 20 '18 at 11:41


















12














The above function looks like this:



enter image description here



However, using tikzpicture with addplot



documentclass[11pt]{article}
usepackage{pgfplots}
usepackage{tikz}
begin{document}
begin{tikzpicture}
begin{axis}
addplot[domain=-6:6,samples=50,smooth,red] { (sin(x)^2)/pow(x,2) };
end{axis}
end{tikzpicture}
end{document}


Gives the following disturbing result:



enter image description here



How should I plot sin(x)^2 / x^2 in tikz?










share|improve this question




















  • 3




    (1) welcome, (2) as always on this site you are much much more likely to get help if you provide a full minimal example that others can copy and test as is. Then they will not have to guess 80% of your setup.
    – daleif
    Nov 20 '18 at 10:53








  • 1




    Thanks for the tip, I've done that now!
    – MrMartin
    Nov 20 '18 at 10:57






  • 8




    Try more samples and sin(deg(x))
    – daleif
    Nov 20 '18 at 10:58










  • Yes, it seems sin(deg(x)) is required when plotting sin(x). Thanks!
    – MrMartin
    Nov 20 '18 at 11:23






  • 1




    @daleif Damn! I did not see your comment :) Also sampling is just as fine, as it is now. Only deg(...) is necessary with sin.
    – Raaja
    Nov 20 '18 at 11:41
















12












12








12


2





The above function looks like this:



enter image description here



However, using tikzpicture with addplot



documentclass[11pt]{article}
usepackage{pgfplots}
usepackage{tikz}
begin{document}
begin{tikzpicture}
begin{axis}
addplot[domain=-6:6,samples=50,smooth,red] { (sin(x)^2)/pow(x,2) };
end{axis}
end{tikzpicture}
end{document}


Gives the following disturbing result:



enter image description here



How should I plot sin(x)^2 / x^2 in tikz?










share|improve this question















The above function looks like this:



enter image description here



However, using tikzpicture with addplot



documentclass[11pt]{article}
usepackage{pgfplots}
usepackage{tikz}
begin{document}
begin{tikzpicture}
begin{axis}
addplot[domain=-6:6,samples=50,smooth,red] { (sin(x)^2)/pow(x,2) };
end{axis}
end{tikzpicture}
end{document}


Gives the following disturbing result:



enter image description here



How should I plot sin(x)^2 / x^2 in tikz?







tikz-pgf gnuplot






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 10:56

























asked Nov 20 '18 at 10:48









MrMartin

1038




1038








  • 3




    (1) welcome, (2) as always on this site you are much much more likely to get help if you provide a full minimal example that others can copy and test as is. Then they will not have to guess 80% of your setup.
    – daleif
    Nov 20 '18 at 10:53








  • 1




    Thanks for the tip, I've done that now!
    – MrMartin
    Nov 20 '18 at 10:57






  • 8




    Try more samples and sin(deg(x))
    – daleif
    Nov 20 '18 at 10:58










  • Yes, it seems sin(deg(x)) is required when plotting sin(x). Thanks!
    – MrMartin
    Nov 20 '18 at 11:23






  • 1




    @daleif Damn! I did not see your comment :) Also sampling is just as fine, as it is now. Only deg(...) is necessary with sin.
    – Raaja
    Nov 20 '18 at 11:41
















  • 3




    (1) welcome, (2) as always on this site you are much much more likely to get help if you provide a full minimal example that others can copy and test as is. Then they will not have to guess 80% of your setup.
    – daleif
    Nov 20 '18 at 10:53








  • 1




    Thanks for the tip, I've done that now!
    – MrMartin
    Nov 20 '18 at 10:57






  • 8




    Try more samples and sin(deg(x))
    – daleif
    Nov 20 '18 at 10:58










  • Yes, it seems sin(deg(x)) is required when plotting sin(x). Thanks!
    – MrMartin
    Nov 20 '18 at 11:23






  • 1




    @daleif Damn! I did not see your comment :) Also sampling is just as fine, as it is now. Only deg(...) is necessary with sin.
    – Raaja
    Nov 20 '18 at 11:41










3




3




(1) welcome, (2) as always on this site you are much much more likely to get help if you provide a full minimal example that others can copy and test as is. Then they will not have to guess 80% of your setup.
– daleif
Nov 20 '18 at 10:53






(1) welcome, (2) as always on this site you are much much more likely to get help if you provide a full minimal example that others can copy and test as is. Then they will not have to guess 80% of your setup.
– daleif
Nov 20 '18 at 10:53






1




1




Thanks for the tip, I've done that now!
– MrMartin
Nov 20 '18 at 10:57




Thanks for the tip, I've done that now!
– MrMartin
Nov 20 '18 at 10:57




8




8




Try more samples and sin(deg(x))
– daleif
Nov 20 '18 at 10:58




Try more samples and sin(deg(x))
– daleif
Nov 20 '18 at 10:58












Yes, it seems sin(deg(x)) is required when plotting sin(x). Thanks!
– MrMartin
Nov 20 '18 at 11:23




Yes, it seems sin(deg(x)) is required when plotting sin(x). Thanks!
– MrMartin
Nov 20 '18 at 11:23




1




1




@daleif Damn! I did not see your comment :) Also sampling is just as fine, as it is now. Only deg(...) is necessary with sin.
– Raaja
Nov 20 '18 at 11:41






@daleif Damn! I did not see your comment :) Also sampling is just as fine, as it is now. Only deg(...) is necessary with sin.
– Raaja
Nov 20 '18 at 11:41












3 Answers
3






active

oldest

votes


















8














As commented by @daleif, the problem is that the trigonometric functions in pgfplots are defined with the argument in degrees, not in radians. So you are calculating the function really near zero, and you have huge rounding errors (and wrong scale...).



You can use sin(deg(x)) to do the conversion, or globally switch with pgfplotsset{trig format=rad}.



Notice however that the manual states:




/pgf/trig format=deg|red [sic, should be rad]



Allows to reconfigure
the trigonometric format for all user arguments. This affects all user
arguments including view, Tik Z polar coordinates, pins of nodes,
start/end angles for edges, etc. At the time of this writing, this
feature is in experimental state: it can happen that it breaks Tik Z
internals. Please handle with care and report any bugs.




In this case it works:



documentclass[margin=10pt]{standalone}
usepackage{pgfplots}pgfplotsset{compat=newest}
usepackage{tikz}
begin{document}
pgfplotsset{trig format=rad}
begin{tikzpicture}
begin{axis}
addplot[domain=-6:6,samples=50,smooth,red] { (sin(x)^2)/pow(x,2) };
end{axis}
end{tikzpicture}
end{document}


enter image description here



Notice also that you have to avoid the point x=0 in your drawing, where the function is undefined... this is with:



addplot[domain=-6:6,samples=151,smooth,red] { (sin(x)^2)/pow(x,2) };


enter image description here






share|improve this answer





























    6














    A PSTricks solution for comparison purpose only.



    documentclass[pstricks,border=12pt]{standalone}
    usepackage{pst-plot}

    deff{(sin(x)/x)^2}
    psset
    {
    yunit=4cm,
    xunit=.5cm
    }

    begin{document}
    begin{pspicture}[algebraic,plotpoints=200](-12,-.1)(12.5,1.2)
    psaxes[Dx=5]{->}(0,0)(-12,-.1)(12,1.1)[$x$,0][$y$,90]
    psplot[linecolor=blue]{-12}{12}{f}
    uput[45](*.7 {f}){$displaystyle f(x)=frac{sin^2(x)}{x^2}$}
    end{pspicture}
    end{document}


    enter image description here






    share|improve this answer































      4














      It seems that the problem is with pgfplots, and it can be solved by switching to gnuplot:



      documentclass[11pt]{article}
      usepackage{pgfplots}
      usepackage{tikz}
      begin{document}
      begin{tikzpicture}
      begin{axis}
      addplot [no markers] gnuplot [domain=-6:6, samples=50] {sin(x)**2/x**2};
      end{axis}
      end{tikzpicture}
      end{document}





      share|improve this answer

















      • 1




        Nope, works just fine for me without gnuplot.
        – daleif
        Nov 20 '18 at 11:17











      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "85"
      };
      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%2ftex.stackexchange.com%2fquestions%2f460921%2fhow-to-plot-function-sinx2-x2%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









      8














      As commented by @daleif, the problem is that the trigonometric functions in pgfplots are defined with the argument in degrees, not in radians. So you are calculating the function really near zero, and you have huge rounding errors (and wrong scale...).



      You can use sin(deg(x)) to do the conversion, or globally switch with pgfplotsset{trig format=rad}.



      Notice however that the manual states:




      /pgf/trig format=deg|red [sic, should be rad]



      Allows to reconfigure
      the trigonometric format for all user arguments. This affects all user
      arguments including view, Tik Z polar coordinates, pins of nodes,
      start/end angles for edges, etc. At the time of this writing, this
      feature is in experimental state: it can happen that it breaks Tik Z
      internals. Please handle with care and report any bugs.




      In this case it works:



      documentclass[margin=10pt]{standalone}
      usepackage{pgfplots}pgfplotsset{compat=newest}
      usepackage{tikz}
      begin{document}
      pgfplotsset{trig format=rad}
      begin{tikzpicture}
      begin{axis}
      addplot[domain=-6:6,samples=50,smooth,red] { (sin(x)^2)/pow(x,2) };
      end{axis}
      end{tikzpicture}
      end{document}


      enter image description here



      Notice also that you have to avoid the point x=0 in your drawing, where the function is undefined... this is with:



      addplot[domain=-6:6,samples=151,smooth,red] { (sin(x)^2)/pow(x,2) };


      enter image description here






      share|improve this answer


























        8














        As commented by @daleif, the problem is that the trigonometric functions in pgfplots are defined with the argument in degrees, not in radians. So you are calculating the function really near zero, and you have huge rounding errors (and wrong scale...).



        You can use sin(deg(x)) to do the conversion, or globally switch with pgfplotsset{trig format=rad}.



        Notice however that the manual states:




        /pgf/trig format=deg|red [sic, should be rad]



        Allows to reconfigure
        the trigonometric format for all user arguments. This affects all user
        arguments including view, Tik Z polar coordinates, pins of nodes,
        start/end angles for edges, etc. At the time of this writing, this
        feature is in experimental state: it can happen that it breaks Tik Z
        internals. Please handle with care and report any bugs.




        In this case it works:



        documentclass[margin=10pt]{standalone}
        usepackage{pgfplots}pgfplotsset{compat=newest}
        usepackage{tikz}
        begin{document}
        pgfplotsset{trig format=rad}
        begin{tikzpicture}
        begin{axis}
        addplot[domain=-6:6,samples=50,smooth,red] { (sin(x)^2)/pow(x,2) };
        end{axis}
        end{tikzpicture}
        end{document}


        enter image description here



        Notice also that you have to avoid the point x=0 in your drawing, where the function is undefined... this is with:



        addplot[domain=-6:6,samples=151,smooth,red] { (sin(x)^2)/pow(x,2) };


        enter image description here






        share|improve this answer
























          8












          8








          8






          As commented by @daleif, the problem is that the trigonometric functions in pgfplots are defined with the argument in degrees, not in radians. So you are calculating the function really near zero, and you have huge rounding errors (and wrong scale...).



          You can use sin(deg(x)) to do the conversion, or globally switch with pgfplotsset{trig format=rad}.



          Notice however that the manual states:




          /pgf/trig format=deg|red [sic, should be rad]



          Allows to reconfigure
          the trigonometric format for all user arguments. This affects all user
          arguments including view, Tik Z polar coordinates, pins of nodes,
          start/end angles for edges, etc. At the time of this writing, this
          feature is in experimental state: it can happen that it breaks Tik Z
          internals. Please handle with care and report any bugs.




          In this case it works:



          documentclass[margin=10pt]{standalone}
          usepackage{pgfplots}pgfplotsset{compat=newest}
          usepackage{tikz}
          begin{document}
          pgfplotsset{trig format=rad}
          begin{tikzpicture}
          begin{axis}
          addplot[domain=-6:6,samples=50,smooth,red] { (sin(x)^2)/pow(x,2) };
          end{axis}
          end{tikzpicture}
          end{document}


          enter image description here



          Notice also that you have to avoid the point x=0 in your drawing, where the function is undefined... this is with:



          addplot[domain=-6:6,samples=151,smooth,red] { (sin(x)^2)/pow(x,2) };


          enter image description here






          share|improve this answer












          As commented by @daleif, the problem is that the trigonometric functions in pgfplots are defined with the argument in degrees, not in radians. So you are calculating the function really near zero, and you have huge rounding errors (and wrong scale...).



          You can use sin(deg(x)) to do the conversion, or globally switch with pgfplotsset{trig format=rad}.



          Notice however that the manual states:




          /pgf/trig format=deg|red [sic, should be rad]



          Allows to reconfigure
          the trigonometric format for all user arguments. This affects all user
          arguments including view, Tik Z polar coordinates, pins of nodes,
          start/end angles for edges, etc. At the time of this writing, this
          feature is in experimental state: it can happen that it breaks Tik Z
          internals. Please handle with care and report any bugs.




          In this case it works:



          documentclass[margin=10pt]{standalone}
          usepackage{pgfplots}pgfplotsset{compat=newest}
          usepackage{tikz}
          begin{document}
          pgfplotsset{trig format=rad}
          begin{tikzpicture}
          begin{axis}
          addplot[domain=-6:6,samples=50,smooth,red] { (sin(x)^2)/pow(x,2) };
          end{axis}
          end{tikzpicture}
          end{document}


          enter image description here



          Notice also that you have to avoid the point x=0 in your drawing, where the function is undefined... this is with:



          addplot[domain=-6:6,samples=151,smooth,red] { (sin(x)^2)/pow(x,2) };


          enter image description here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 16:31









          Rmano

          7,75221647




          7,75221647























              6














              A PSTricks solution for comparison purpose only.



              documentclass[pstricks,border=12pt]{standalone}
              usepackage{pst-plot}

              deff{(sin(x)/x)^2}
              psset
              {
              yunit=4cm,
              xunit=.5cm
              }

              begin{document}
              begin{pspicture}[algebraic,plotpoints=200](-12,-.1)(12.5,1.2)
              psaxes[Dx=5]{->}(0,0)(-12,-.1)(12,1.1)[$x$,0][$y$,90]
              psplot[linecolor=blue]{-12}{12}{f}
              uput[45](*.7 {f}){$displaystyle f(x)=frac{sin^2(x)}{x^2}$}
              end{pspicture}
              end{document}


              enter image description here






              share|improve this answer




























                6














                A PSTricks solution for comparison purpose only.



                documentclass[pstricks,border=12pt]{standalone}
                usepackage{pst-plot}

                deff{(sin(x)/x)^2}
                psset
                {
                yunit=4cm,
                xunit=.5cm
                }

                begin{document}
                begin{pspicture}[algebraic,plotpoints=200](-12,-.1)(12.5,1.2)
                psaxes[Dx=5]{->}(0,0)(-12,-.1)(12,1.1)[$x$,0][$y$,90]
                psplot[linecolor=blue]{-12}{12}{f}
                uput[45](*.7 {f}){$displaystyle f(x)=frac{sin^2(x)}{x^2}$}
                end{pspicture}
                end{document}


                enter image description here






                share|improve this answer


























                  6












                  6








                  6






                  A PSTricks solution for comparison purpose only.



                  documentclass[pstricks,border=12pt]{standalone}
                  usepackage{pst-plot}

                  deff{(sin(x)/x)^2}
                  psset
                  {
                  yunit=4cm,
                  xunit=.5cm
                  }

                  begin{document}
                  begin{pspicture}[algebraic,plotpoints=200](-12,-.1)(12.5,1.2)
                  psaxes[Dx=5]{->}(0,0)(-12,-.1)(12,1.1)[$x$,0][$y$,90]
                  psplot[linecolor=blue]{-12}{12}{f}
                  uput[45](*.7 {f}){$displaystyle f(x)=frac{sin^2(x)}{x^2}$}
                  end{pspicture}
                  end{document}


                  enter image description here






                  share|improve this answer














                  A PSTricks solution for comparison purpose only.



                  documentclass[pstricks,border=12pt]{standalone}
                  usepackage{pst-plot}

                  deff{(sin(x)/x)^2}
                  psset
                  {
                  yunit=4cm,
                  xunit=.5cm
                  }

                  begin{document}
                  begin{pspicture}[algebraic,plotpoints=200](-12,-.1)(12.5,1.2)
                  psaxes[Dx=5]{->}(0,0)(-12,-.1)(12,1.1)[$x$,0][$y$,90]
                  psplot[linecolor=blue]{-12}{12}{f}
                  uput[45](*.7 {f}){$displaystyle f(x)=frac{sin^2(x)}{x^2}$}
                  end{pspicture}
                  end{document}


                  enter image description here







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 20 '18 at 11:15

























                  answered Nov 20 '18 at 11:01









                  God Must Be Crazy

                  5,64011039




                  5,64011039























                      4














                      It seems that the problem is with pgfplots, and it can be solved by switching to gnuplot:



                      documentclass[11pt]{article}
                      usepackage{pgfplots}
                      usepackage{tikz}
                      begin{document}
                      begin{tikzpicture}
                      begin{axis}
                      addplot [no markers] gnuplot [domain=-6:6, samples=50] {sin(x)**2/x**2};
                      end{axis}
                      end{tikzpicture}
                      end{document}





                      share|improve this answer

















                      • 1




                        Nope, works just fine for me without gnuplot.
                        – daleif
                        Nov 20 '18 at 11:17
















                      4














                      It seems that the problem is with pgfplots, and it can be solved by switching to gnuplot:



                      documentclass[11pt]{article}
                      usepackage{pgfplots}
                      usepackage{tikz}
                      begin{document}
                      begin{tikzpicture}
                      begin{axis}
                      addplot [no markers] gnuplot [domain=-6:6, samples=50] {sin(x)**2/x**2};
                      end{axis}
                      end{tikzpicture}
                      end{document}





                      share|improve this answer

















                      • 1




                        Nope, works just fine for me without gnuplot.
                        – daleif
                        Nov 20 '18 at 11:17














                      4












                      4








                      4






                      It seems that the problem is with pgfplots, and it can be solved by switching to gnuplot:



                      documentclass[11pt]{article}
                      usepackage{pgfplots}
                      usepackage{tikz}
                      begin{document}
                      begin{tikzpicture}
                      begin{axis}
                      addplot [no markers] gnuplot [domain=-6:6, samples=50] {sin(x)**2/x**2};
                      end{axis}
                      end{tikzpicture}
                      end{document}





                      share|improve this answer












                      It seems that the problem is with pgfplots, and it can be solved by switching to gnuplot:



                      documentclass[11pt]{article}
                      usepackage{pgfplots}
                      usepackage{tikz}
                      begin{document}
                      begin{tikzpicture}
                      begin{axis}
                      addplot [no markers] gnuplot [domain=-6:6, samples=50] {sin(x)**2/x**2};
                      end{axis}
                      end{tikzpicture}
                      end{document}






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 20 '18 at 11:01









                      MrMartin

                      1038




                      1038








                      • 1




                        Nope, works just fine for me without gnuplot.
                        – daleif
                        Nov 20 '18 at 11:17














                      • 1




                        Nope, works just fine for me without gnuplot.
                        – daleif
                        Nov 20 '18 at 11:17








                      1




                      1




                      Nope, works just fine for me without gnuplot.
                      – daleif
                      Nov 20 '18 at 11:17




                      Nope, works just fine for me without gnuplot.
                      – daleif
                      Nov 20 '18 at 11:17


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to TeX - LaTeX 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.


                      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%2ftex.stackexchange.com%2fquestions%2f460921%2fhow-to-plot-function-sinx2-x2%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

                      android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                      SQL update select statement

                      'app-layout' is not a known element: how to share Component with different Modules