How to use a summation in a tikz plot












4















I need to plot this function using tikz, but I have no idea how to go about using the summation in tikz. If someone could help me I would really appreciate it.
Function
The code I have is this:



documentclass{article}

usepackage{pgfplots}
usepackage{tikz}

usepackage[paperwidth=20cm, paperheight=10cm, margin=0.0cm]{geometry}

begin{document}
hspace{-0.4cm}
begin{tikzpicture}[
declare function={p(k,n) =((n!*(k*(k-1)))/((n^(k))*((n-(k-
1))!)));}
]
begin{axis}[axis lines=middle, grid=both, width=20cm, height=10cm, grid
style={line width=.2pt, draw=gray!10},major grid style={line
width=.3pt,draw=gray!50},
xlabel=$k$, ylabel=$y$,
ylabel style={anchor=south},
xlabel style={anchor=west},
ymax=1, xmax=21.5,
domain=0:21.5, samples at={0,...,21},
]
addplot+ [blue, thick, mark size={1.6pt}, mark options={draw=blue,
fill=white!75!cyan}] {p(x,20)};
end{axis}
end{tikzpicture}

end{document}


This plots the function of x, but I don't know how to sum the function from k=2 to x+1.










share|improve this question


















  • 1





    possibly helpful? tex.stackexchange.com/questions/291404/…

    – cmhughes
    Nov 23 '17 at 15:01











  • Thanks, I saw that earlier but couldn't figure it out. I've only been using latex for a short time and Im struggling to work out what to do.

    – John 545
    Nov 23 '17 at 15:08











  • Is this not possible in tikz?

    – John 545
    Nov 23 '17 at 20:03
















4















I need to plot this function using tikz, but I have no idea how to go about using the summation in tikz. If someone could help me I would really appreciate it.
Function
The code I have is this:



documentclass{article}

usepackage{pgfplots}
usepackage{tikz}

usepackage[paperwidth=20cm, paperheight=10cm, margin=0.0cm]{geometry}

begin{document}
hspace{-0.4cm}
begin{tikzpicture}[
declare function={p(k,n) =((n!*(k*(k-1)))/((n^(k))*((n-(k-
1))!)));}
]
begin{axis}[axis lines=middle, grid=both, width=20cm, height=10cm, grid
style={line width=.2pt, draw=gray!10},major grid style={line
width=.3pt,draw=gray!50},
xlabel=$k$, ylabel=$y$,
ylabel style={anchor=south},
xlabel style={anchor=west},
ymax=1, xmax=21.5,
domain=0:21.5, samples at={0,...,21},
]
addplot+ [blue, thick, mark size={1.6pt}, mark options={draw=blue,
fill=white!75!cyan}] {p(x,20)};
end{axis}
end{tikzpicture}

end{document}


This plots the function of x, but I don't know how to sum the function from k=2 to x+1.










share|improve this question


















  • 1





    possibly helpful? tex.stackexchange.com/questions/291404/…

    – cmhughes
    Nov 23 '17 at 15:01











  • Thanks, I saw that earlier but couldn't figure it out. I've only been using latex for a short time and Im struggling to work out what to do.

    – John 545
    Nov 23 '17 at 15:08











  • Is this not possible in tikz?

    – John 545
    Nov 23 '17 at 20:03














4












4








4


2






I need to plot this function using tikz, but I have no idea how to go about using the summation in tikz. If someone could help me I would really appreciate it.
Function
The code I have is this:



documentclass{article}

usepackage{pgfplots}
usepackage{tikz}

usepackage[paperwidth=20cm, paperheight=10cm, margin=0.0cm]{geometry}

begin{document}
hspace{-0.4cm}
begin{tikzpicture}[
declare function={p(k,n) =((n!*(k*(k-1)))/((n^(k))*((n-(k-
1))!)));}
]
begin{axis}[axis lines=middle, grid=both, width=20cm, height=10cm, grid
style={line width=.2pt, draw=gray!10},major grid style={line
width=.3pt,draw=gray!50},
xlabel=$k$, ylabel=$y$,
ylabel style={anchor=south},
xlabel style={anchor=west},
ymax=1, xmax=21.5,
domain=0:21.5, samples at={0,...,21},
]
addplot+ [blue, thick, mark size={1.6pt}, mark options={draw=blue,
fill=white!75!cyan}] {p(x,20)};
end{axis}
end{tikzpicture}

end{document}


This plots the function of x, but I don't know how to sum the function from k=2 to x+1.










share|improve this question














I need to plot this function using tikz, but I have no idea how to go about using the summation in tikz. If someone could help me I would really appreciate it.
Function
The code I have is this:



documentclass{article}

usepackage{pgfplots}
usepackage{tikz}

usepackage[paperwidth=20cm, paperheight=10cm, margin=0.0cm]{geometry}

begin{document}
hspace{-0.4cm}
begin{tikzpicture}[
declare function={p(k,n) =((n!*(k*(k-1)))/((n^(k))*((n-(k-
1))!)));}
]
begin{axis}[axis lines=middle, grid=both, width=20cm, height=10cm, grid
style={line width=.2pt, draw=gray!10},major grid style={line
width=.3pt,draw=gray!50},
xlabel=$k$, ylabel=$y$,
ylabel style={anchor=south},
xlabel style={anchor=west},
ymax=1, xmax=21.5,
domain=0:21.5, samples at={0,...,21},
]
addplot+ [blue, thick, mark size={1.6pt}, mark options={draw=blue,
fill=white!75!cyan}] {p(x,20)};
end{axis}
end{tikzpicture}

end{document}


This plots the function of x, but I don't know how to sum the function from k=2 to x+1.







tikz-pgf plot






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '17 at 14:01









John 545John 545

545




545








  • 1





    possibly helpful? tex.stackexchange.com/questions/291404/…

    – cmhughes
    Nov 23 '17 at 15:01











  • Thanks, I saw that earlier but couldn't figure it out. I've only been using latex for a short time and Im struggling to work out what to do.

    – John 545
    Nov 23 '17 at 15:08











  • Is this not possible in tikz?

    – John 545
    Nov 23 '17 at 20:03














  • 1





    possibly helpful? tex.stackexchange.com/questions/291404/…

    – cmhughes
    Nov 23 '17 at 15:01











  • Thanks, I saw that earlier but couldn't figure it out. I've only been using latex for a short time and Im struggling to work out what to do.

    – John 545
    Nov 23 '17 at 15:08











  • Is this not possible in tikz?

    – John 545
    Nov 23 '17 at 20:03








1




1





possibly helpful? tex.stackexchange.com/questions/291404/…

– cmhughes
Nov 23 '17 at 15:01





possibly helpful? tex.stackexchange.com/questions/291404/…

– cmhughes
Nov 23 '17 at 15:01













Thanks, I saw that earlier but couldn't figure it out. I've only been using latex for a short time and Im struggling to work out what to do.

– John 545
Nov 23 '17 at 15:08





Thanks, I saw that earlier but couldn't figure it out. I've only been using latex for a short time and Im struggling to work out what to do.

– John 545
Nov 23 '17 at 15:08













Is this not possible in tikz?

– John 545
Nov 23 '17 at 20:03





Is this not possible in tikz?

– John 545
Nov 23 '17 at 20:03










1 Answer
1






active

oldest

votes


















5














You can use Lua to compute the sum. Then you have to typeset using LuaLaTeX of course.



documentclass{article}

usepackage{pgfplots}
pgfplotsset{compat=newest}

usepackage{luacode}
begin{luacode*}
function factorial(n)
assert(n >= 0, "Factorial is only valid for positive integers")
if n == 0 then
return 1
end
return n*factorial(n-1)
end

function p(x)
assert(x == math.floor(x), "x must be an integer")
res = 0
for k = 2, x+1 do
res = res + factorial(x)/factorial(x-(k-1)) * k*(k-1)/(x^k)
end
tex.sprint(res)
end
end{luacode*}

begin{document}

begin{tikzpicture}[
declare function={p(n) = directlua{p(n)};}
]
begin{axis}[
use fpu=false, % very important!
xlabel=$x$, ylabel=$p(x)$,
samples at={0,...,21},
only marks,
]
addplot {p(x)};
end{axis}
end{tikzpicture}

end{document}


enter image description here






share|improve this answer























    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%2f402730%2fhow-to-use-a-summation-in-a-tikz-plot%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    You can use Lua to compute the sum. Then you have to typeset using LuaLaTeX of course.



    documentclass{article}

    usepackage{pgfplots}
    pgfplotsset{compat=newest}

    usepackage{luacode}
    begin{luacode*}
    function factorial(n)
    assert(n >= 0, "Factorial is only valid for positive integers")
    if n == 0 then
    return 1
    end
    return n*factorial(n-1)
    end

    function p(x)
    assert(x == math.floor(x), "x must be an integer")
    res = 0
    for k = 2, x+1 do
    res = res + factorial(x)/factorial(x-(k-1)) * k*(k-1)/(x^k)
    end
    tex.sprint(res)
    end
    end{luacode*}

    begin{document}

    begin{tikzpicture}[
    declare function={p(n) = directlua{p(n)};}
    ]
    begin{axis}[
    use fpu=false, % very important!
    xlabel=$x$, ylabel=$p(x)$,
    samples at={0,...,21},
    only marks,
    ]
    addplot {p(x)};
    end{axis}
    end{tikzpicture}

    end{document}


    enter image description here






    share|improve this answer




























      5














      You can use Lua to compute the sum. Then you have to typeset using LuaLaTeX of course.



      documentclass{article}

      usepackage{pgfplots}
      pgfplotsset{compat=newest}

      usepackage{luacode}
      begin{luacode*}
      function factorial(n)
      assert(n >= 0, "Factorial is only valid for positive integers")
      if n == 0 then
      return 1
      end
      return n*factorial(n-1)
      end

      function p(x)
      assert(x == math.floor(x), "x must be an integer")
      res = 0
      for k = 2, x+1 do
      res = res + factorial(x)/factorial(x-(k-1)) * k*(k-1)/(x^k)
      end
      tex.sprint(res)
      end
      end{luacode*}

      begin{document}

      begin{tikzpicture}[
      declare function={p(n) = directlua{p(n)};}
      ]
      begin{axis}[
      use fpu=false, % very important!
      xlabel=$x$, ylabel=$p(x)$,
      samples at={0,...,21},
      only marks,
      ]
      addplot {p(x)};
      end{axis}
      end{tikzpicture}

      end{document}


      enter image description here






      share|improve this answer


























        5












        5








        5







        You can use Lua to compute the sum. Then you have to typeset using LuaLaTeX of course.



        documentclass{article}

        usepackage{pgfplots}
        pgfplotsset{compat=newest}

        usepackage{luacode}
        begin{luacode*}
        function factorial(n)
        assert(n >= 0, "Factorial is only valid for positive integers")
        if n == 0 then
        return 1
        end
        return n*factorial(n-1)
        end

        function p(x)
        assert(x == math.floor(x), "x must be an integer")
        res = 0
        for k = 2, x+1 do
        res = res + factorial(x)/factorial(x-(k-1)) * k*(k-1)/(x^k)
        end
        tex.sprint(res)
        end
        end{luacode*}

        begin{document}

        begin{tikzpicture}[
        declare function={p(n) = directlua{p(n)};}
        ]
        begin{axis}[
        use fpu=false, % very important!
        xlabel=$x$, ylabel=$p(x)$,
        samples at={0,...,21},
        only marks,
        ]
        addplot {p(x)};
        end{axis}
        end{tikzpicture}

        end{document}


        enter image description here






        share|improve this answer













        You can use Lua to compute the sum. Then you have to typeset using LuaLaTeX of course.



        documentclass{article}

        usepackage{pgfplots}
        pgfplotsset{compat=newest}

        usepackage{luacode}
        begin{luacode*}
        function factorial(n)
        assert(n >= 0, "Factorial is only valid for positive integers")
        if n == 0 then
        return 1
        end
        return n*factorial(n-1)
        end

        function p(x)
        assert(x == math.floor(x), "x must be an integer")
        res = 0
        for k = 2, x+1 do
        res = res + factorial(x)/factorial(x-(k-1)) * k*(k-1)/(x^k)
        end
        tex.sprint(res)
        end
        end{luacode*}

        begin{document}

        begin{tikzpicture}[
        declare function={p(n) = directlua{p(n)};}
        ]
        begin{axis}[
        use fpu=false, % very important!
        xlabel=$x$, ylabel=$p(x)$,
        samples at={0,...,21},
        only marks,
        ]
        addplot {p(x)};
        end{axis}
        end{tikzpicture}

        end{document}


        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '17 at 23:40









        Henri MenkeHenri Menke

        73.9k8162274




        73.9k8162274






























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f402730%2fhow-to-use-a-summation-in-a-tikz-plot%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

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?