How to use a summation in a tikz plot
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.
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
add a comment |
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.
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
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
add a comment |
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.
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
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.
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
tikz-pgf plot
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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}
add a comment |
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}
add a comment |
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}
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}
answered Nov 23 '17 at 23:40
Henri MenkeHenri Menke
73.9k8162274
73.9k8162274
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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