TIKZ: Evaluate declared functions inside `foreach`
Clash Royale CLAN TAG#URR8PPP
up vote
5
down vote
favorite
I'm trying to write a loop to plot a rectangle showing the Midpoint Rule
using pgfplots, but cannot evaluate the function to draw it. (around the end of code). Actual code isn't compiling.
documentclassarticle
usepackagepgfplots
begindocument
pgfplotsset
integral axis/.style=
axis lines=middle,
enlarge y limits=upper,
axis equal image, width=12cm,
xlabel=$x$, ylabel=$y$,
ytick=empty,
xticklabel style=font=small, text height=1.5ex, anchor=north,
samples=100
,
integral/.style=
domain=2:8,
samples=7
,
integral fill/.style=
integral,
draw=none, fill=#1,
%on layer=axis background
,
integral fill/.default=cyan!10,
integral line/.style=
integral,
very thick,
draw=#1
,
integral line/.default=black
begintikzpicture[
% The function that is used for all the plots
declare function=f=x/5-cos(deg(x*1.85))/2+2;
]
beginaxis[
integral axis,
ymin=0,
xmin=0.75, xmax=9.25,
domain=1.5:8.5,
xtick=2,2.5,3,3.5,...,8,
xticklabels=$x_0$, $x_1/2$, $x_1$,$x_3/2$,$x_2$,$x_5/2$,$x_3$,,,,$x_N-1$,$x_N-frac12$,$x_N$,
axis on top
]
% The filled area under the approximate integral
addplot [integral fill=cyan!15] f closedcycle;
% The highlighted segment
%addplot [integral fill=cyan!35, domain=6:7, samples=2] f closedcycle;
% The function
addplot [very thick, cyan!75!blue] f node [anchor=south] $y=f(x)$;
% The approximate integral
addplot [integral line=black] f;
% The vertical lines between the segments
addplot [integral, ycomb] f;
% Issue is here.
foreach i in 2.5,3.5,...,7.5
pgfmathsetmacroyf i;
draw (i-.5,y) rectangle (i+.5,0);
endaxis
endtikzpicture
enddocument
tikz-pgf pgfplots
add a comment |Â
up vote
5
down vote
favorite
I'm trying to write a loop to plot a rectangle showing the Midpoint Rule
using pgfplots, but cannot evaluate the function to draw it. (around the end of code). Actual code isn't compiling.
documentclassarticle
usepackagepgfplots
begindocument
pgfplotsset
integral axis/.style=
axis lines=middle,
enlarge y limits=upper,
axis equal image, width=12cm,
xlabel=$x$, ylabel=$y$,
ytick=empty,
xticklabel style=font=small, text height=1.5ex, anchor=north,
samples=100
,
integral/.style=
domain=2:8,
samples=7
,
integral fill/.style=
integral,
draw=none, fill=#1,
%on layer=axis background
,
integral fill/.default=cyan!10,
integral line/.style=
integral,
very thick,
draw=#1
,
integral line/.default=black
begintikzpicture[
% The function that is used for all the plots
declare function=f=x/5-cos(deg(x*1.85))/2+2;
]
beginaxis[
integral axis,
ymin=0,
xmin=0.75, xmax=9.25,
domain=1.5:8.5,
xtick=2,2.5,3,3.5,...,8,
xticklabels=$x_0$, $x_1/2$, $x_1$,$x_3/2$,$x_2$,$x_5/2$,$x_3$,,,,$x_N-1$,$x_N-frac12$,$x_N$,
axis on top
]
% The filled area under the approximate integral
addplot [integral fill=cyan!15] f closedcycle;
% The highlighted segment
%addplot [integral fill=cyan!35, domain=6:7, samples=2] f closedcycle;
% The function
addplot [very thick, cyan!75!blue] f node [anchor=south] $y=f(x)$;
% The approximate integral
addplot [integral line=black] f;
% The vertical lines between the segments
addplot [integral, ycomb] f;
% Issue is here.
foreach i in 2.5,3.5,...,7.5
pgfmathsetmacroyf i;
draw (i-.5,y) rectangle (i+.5,0);
endaxis
endtikzpicture
enddocument
tikz-pgf pgfplots
add a comment |Â
up vote
5
down vote
favorite
up vote
5
down vote
favorite
I'm trying to write a loop to plot a rectangle showing the Midpoint Rule
using pgfplots, but cannot evaluate the function to draw it. (around the end of code). Actual code isn't compiling.
documentclassarticle
usepackagepgfplots
begindocument
pgfplotsset
integral axis/.style=
axis lines=middle,
enlarge y limits=upper,
axis equal image, width=12cm,
xlabel=$x$, ylabel=$y$,
ytick=empty,
xticklabel style=font=small, text height=1.5ex, anchor=north,
samples=100
,
integral/.style=
domain=2:8,
samples=7
,
integral fill/.style=
integral,
draw=none, fill=#1,
%on layer=axis background
,
integral fill/.default=cyan!10,
integral line/.style=
integral,
very thick,
draw=#1
,
integral line/.default=black
begintikzpicture[
% The function that is used for all the plots
declare function=f=x/5-cos(deg(x*1.85))/2+2;
]
beginaxis[
integral axis,
ymin=0,
xmin=0.75, xmax=9.25,
domain=1.5:8.5,
xtick=2,2.5,3,3.5,...,8,
xticklabels=$x_0$, $x_1/2$, $x_1$,$x_3/2$,$x_2$,$x_5/2$,$x_3$,,,,$x_N-1$,$x_N-frac12$,$x_N$,
axis on top
]
% The filled area under the approximate integral
addplot [integral fill=cyan!15] f closedcycle;
% The highlighted segment
%addplot [integral fill=cyan!35, domain=6:7, samples=2] f closedcycle;
% The function
addplot [very thick, cyan!75!blue] f node [anchor=south] $y=f(x)$;
% The approximate integral
addplot [integral line=black] f;
% The vertical lines between the segments
addplot [integral, ycomb] f;
% Issue is here.
foreach i in 2.5,3.5,...,7.5
pgfmathsetmacroyf i;
draw (i-.5,y) rectangle (i+.5,0);
endaxis
endtikzpicture
enddocument
tikz-pgf pgfplots
I'm trying to write a loop to plot a rectangle showing the Midpoint Rule
using pgfplots, but cannot evaluate the function to draw it. (around the end of code). Actual code isn't compiling.
documentclassarticle
usepackagepgfplots
begindocument
pgfplotsset
integral axis/.style=
axis lines=middle,
enlarge y limits=upper,
axis equal image, width=12cm,
xlabel=$x$, ylabel=$y$,
ytick=empty,
xticklabel style=font=small, text height=1.5ex, anchor=north,
samples=100
,
integral/.style=
domain=2:8,
samples=7
,
integral fill/.style=
integral,
draw=none, fill=#1,
%on layer=axis background
,
integral fill/.default=cyan!10,
integral line/.style=
integral,
very thick,
draw=#1
,
integral line/.default=black
begintikzpicture[
% The function that is used for all the plots
declare function=f=x/5-cos(deg(x*1.85))/2+2;
]
beginaxis[
integral axis,
ymin=0,
xmin=0.75, xmax=9.25,
domain=1.5:8.5,
xtick=2,2.5,3,3.5,...,8,
xticklabels=$x_0$, $x_1/2$, $x_1$,$x_3/2$,$x_2$,$x_5/2$,$x_3$,,,,$x_N-1$,$x_N-frac12$,$x_N$,
axis on top
]
% The filled area under the approximate integral
addplot [integral fill=cyan!15] f closedcycle;
% The highlighted segment
%addplot [integral fill=cyan!35, domain=6:7, samples=2] f closedcycle;
% The function
addplot [very thick, cyan!75!blue] f node [anchor=south] $y=f(x)$;
% The approximate integral
addplot [integral line=black] f;
% The vertical lines between the segments
addplot [integral, ycomb] f;
% Issue is here.
foreach i in 2.5,3.5,...,7.5
pgfmathsetmacroyf i;
draw (i-.5,y) rectangle (i+.5,0);
endaxis
endtikzpicture
enddocument
tikz-pgf pgfplots
tikz-pgf pgfplots
asked 2 days ago
Lin
2337
2337
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
9
down vote
accepted
Pgfplots comes with its own foreach version, which you may want to use here. Notice that you did not specify a version. In older versions of pgfplots you need to add axis cs:
to the coordinates in the loop. I adjusted your function such that it is really a function of a variable, otherwise you cannot use it in the draw
commands
documentclassarticle
usepackagepgfplots
begindocument
pgfplotssetcompat=1.16,
integral axis/.style=
axis lines=middle,
enlarge y limits=upper,
axis equal image, width=12cm,
xlabel=$x$, ylabel=$y$,
ytick=empty,
xticklabel style=font=small, text height=1.5ex, anchor=north,
samples=100
,
integral/.style=
domain=2:8,
samples=7
,
integral fill/.style=
integral,
draw=none, fill=#1,
%on layer=axis background
,
integral fill/.default=cyan!10,
integral line/.style=
integral,
very thick,
draw=#1
,
integral line/.default=black
begintikzpicture[
% The function that is used for all the plots
declare function=f(x)=x/5-cos(deg(x*1.85))/2+2;
]
beginaxis[
integral axis,
ymin=0,
xmin=0.75, xmax=9.25,
domain=1.5:8.5,
xtick=2,2.5,3,3.5,...,8,
xticklabels=$x_0$, $x_1/2$, $x_1$,$x_3/2$,$x_2$,$x_5/2$,$x_3$,,,,$x_N-1$,$x_N-frac12$,$x_N$,
axis on top
]
% The filled area under the approximate integral
addplot [integral fill=cyan!15] f(x) closedcycle;
% The highlighted segment
%addplot [integral fill=cyan!35, domain=6:7, samples=2] f closedcycle;
% The function
addplot [very thick, cyan!75!blue] f(x) node [anchor=south] $y=f(x)$;
% The approximate integral
addplot [integral line=black] f(x);
% The vertical lines between the segments
addplot [integral, ycomb] f(x);
% Issue is here.
pgfplotsinvokeforeach2.5,3.5,...,7.5
draw[thick,blue] (#1-0.5,f(#1)) rectangle(#1+.5,0);
endaxis
endtikzpicture
enddocument
Of course, you could plot these bars also with a bar
plot. That is, you could replace the loop by
addplot [blue,thick,ybar,bar width=1,samples at=2.5,3.5,...,7.5] f(x);
Mr @marmot, thank you one more time. Just for curiosity, is possible to add some discrete evaluated points, lets say inf(x)
, at some sample positions without to add a line that connects each sampled value?
– Lin
2 days ago
1
found:only marks
.
– Lin
2 days ago
@Lin Yes, that's how this can be done. (I'm already in hibernation mode. ;-)
– marmot
2 days ago
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
accepted
Pgfplots comes with its own foreach version, which you may want to use here. Notice that you did not specify a version. In older versions of pgfplots you need to add axis cs:
to the coordinates in the loop. I adjusted your function such that it is really a function of a variable, otherwise you cannot use it in the draw
commands
documentclassarticle
usepackagepgfplots
begindocument
pgfplotssetcompat=1.16,
integral axis/.style=
axis lines=middle,
enlarge y limits=upper,
axis equal image, width=12cm,
xlabel=$x$, ylabel=$y$,
ytick=empty,
xticklabel style=font=small, text height=1.5ex, anchor=north,
samples=100
,
integral/.style=
domain=2:8,
samples=7
,
integral fill/.style=
integral,
draw=none, fill=#1,
%on layer=axis background
,
integral fill/.default=cyan!10,
integral line/.style=
integral,
very thick,
draw=#1
,
integral line/.default=black
begintikzpicture[
% The function that is used for all the plots
declare function=f(x)=x/5-cos(deg(x*1.85))/2+2;
]
beginaxis[
integral axis,
ymin=0,
xmin=0.75, xmax=9.25,
domain=1.5:8.5,
xtick=2,2.5,3,3.5,...,8,
xticklabels=$x_0$, $x_1/2$, $x_1$,$x_3/2$,$x_2$,$x_5/2$,$x_3$,,,,$x_N-1$,$x_N-frac12$,$x_N$,
axis on top
]
% The filled area under the approximate integral
addplot [integral fill=cyan!15] f(x) closedcycle;
% The highlighted segment
%addplot [integral fill=cyan!35, domain=6:7, samples=2] f closedcycle;
% The function
addplot [very thick, cyan!75!blue] f(x) node [anchor=south] $y=f(x)$;
% The approximate integral
addplot [integral line=black] f(x);
% The vertical lines between the segments
addplot [integral, ycomb] f(x);
% Issue is here.
pgfplotsinvokeforeach2.5,3.5,...,7.5
draw[thick,blue] (#1-0.5,f(#1)) rectangle(#1+.5,0);
endaxis
endtikzpicture
enddocument
Of course, you could plot these bars also with a bar
plot. That is, you could replace the loop by
addplot [blue,thick,ybar,bar width=1,samples at=2.5,3.5,...,7.5] f(x);
Mr @marmot, thank you one more time. Just for curiosity, is possible to add some discrete evaluated points, lets say inf(x)
, at some sample positions without to add a line that connects each sampled value?
– Lin
2 days ago
1
found:only marks
.
– Lin
2 days ago
@Lin Yes, that's how this can be done. (I'm already in hibernation mode. ;-)
– marmot
2 days ago
add a comment |Â
up vote
9
down vote
accepted
Pgfplots comes with its own foreach version, which you may want to use here. Notice that you did not specify a version. In older versions of pgfplots you need to add axis cs:
to the coordinates in the loop. I adjusted your function such that it is really a function of a variable, otherwise you cannot use it in the draw
commands
documentclassarticle
usepackagepgfplots
begindocument
pgfplotssetcompat=1.16,
integral axis/.style=
axis lines=middle,
enlarge y limits=upper,
axis equal image, width=12cm,
xlabel=$x$, ylabel=$y$,
ytick=empty,
xticklabel style=font=small, text height=1.5ex, anchor=north,
samples=100
,
integral/.style=
domain=2:8,
samples=7
,
integral fill/.style=
integral,
draw=none, fill=#1,
%on layer=axis background
,
integral fill/.default=cyan!10,
integral line/.style=
integral,
very thick,
draw=#1
,
integral line/.default=black
begintikzpicture[
% The function that is used for all the plots
declare function=f(x)=x/5-cos(deg(x*1.85))/2+2;
]
beginaxis[
integral axis,
ymin=0,
xmin=0.75, xmax=9.25,
domain=1.5:8.5,
xtick=2,2.5,3,3.5,...,8,
xticklabels=$x_0$, $x_1/2$, $x_1$,$x_3/2$,$x_2$,$x_5/2$,$x_3$,,,,$x_N-1$,$x_N-frac12$,$x_N$,
axis on top
]
% The filled area under the approximate integral
addplot [integral fill=cyan!15] f(x) closedcycle;
% The highlighted segment
%addplot [integral fill=cyan!35, domain=6:7, samples=2] f closedcycle;
% The function
addplot [very thick, cyan!75!blue] f(x) node [anchor=south] $y=f(x)$;
% The approximate integral
addplot [integral line=black] f(x);
% The vertical lines between the segments
addplot [integral, ycomb] f(x);
% Issue is here.
pgfplotsinvokeforeach2.5,3.5,...,7.5
draw[thick,blue] (#1-0.5,f(#1)) rectangle(#1+.5,0);
endaxis
endtikzpicture
enddocument
Of course, you could plot these bars also with a bar
plot. That is, you could replace the loop by
addplot [blue,thick,ybar,bar width=1,samples at=2.5,3.5,...,7.5] f(x);
Mr @marmot, thank you one more time. Just for curiosity, is possible to add some discrete evaluated points, lets say inf(x)
, at some sample positions without to add a line that connects each sampled value?
– Lin
2 days ago
1
found:only marks
.
– Lin
2 days ago
@Lin Yes, that's how this can be done. (I'm already in hibernation mode. ;-)
– marmot
2 days ago
add a comment |Â
up vote
9
down vote
accepted
up vote
9
down vote
accepted
Pgfplots comes with its own foreach version, which you may want to use here. Notice that you did not specify a version. In older versions of pgfplots you need to add axis cs:
to the coordinates in the loop. I adjusted your function such that it is really a function of a variable, otherwise you cannot use it in the draw
commands
documentclassarticle
usepackagepgfplots
begindocument
pgfplotssetcompat=1.16,
integral axis/.style=
axis lines=middle,
enlarge y limits=upper,
axis equal image, width=12cm,
xlabel=$x$, ylabel=$y$,
ytick=empty,
xticklabel style=font=small, text height=1.5ex, anchor=north,
samples=100
,
integral/.style=
domain=2:8,
samples=7
,
integral fill/.style=
integral,
draw=none, fill=#1,
%on layer=axis background
,
integral fill/.default=cyan!10,
integral line/.style=
integral,
very thick,
draw=#1
,
integral line/.default=black
begintikzpicture[
% The function that is used for all the plots
declare function=f(x)=x/5-cos(deg(x*1.85))/2+2;
]
beginaxis[
integral axis,
ymin=0,
xmin=0.75, xmax=9.25,
domain=1.5:8.5,
xtick=2,2.5,3,3.5,...,8,
xticklabels=$x_0$, $x_1/2$, $x_1$,$x_3/2$,$x_2$,$x_5/2$,$x_3$,,,,$x_N-1$,$x_N-frac12$,$x_N$,
axis on top
]
% The filled area under the approximate integral
addplot [integral fill=cyan!15] f(x) closedcycle;
% The highlighted segment
%addplot [integral fill=cyan!35, domain=6:7, samples=2] f closedcycle;
% The function
addplot [very thick, cyan!75!blue] f(x) node [anchor=south] $y=f(x)$;
% The approximate integral
addplot [integral line=black] f(x);
% The vertical lines between the segments
addplot [integral, ycomb] f(x);
% Issue is here.
pgfplotsinvokeforeach2.5,3.5,...,7.5
draw[thick,blue] (#1-0.5,f(#1)) rectangle(#1+.5,0);
endaxis
endtikzpicture
enddocument
Of course, you could plot these bars also with a bar
plot. That is, you could replace the loop by
addplot [blue,thick,ybar,bar width=1,samples at=2.5,3.5,...,7.5] f(x);
Pgfplots comes with its own foreach version, which you may want to use here. Notice that you did not specify a version. In older versions of pgfplots you need to add axis cs:
to the coordinates in the loop. I adjusted your function such that it is really a function of a variable, otherwise you cannot use it in the draw
commands
documentclassarticle
usepackagepgfplots
begindocument
pgfplotssetcompat=1.16,
integral axis/.style=
axis lines=middle,
enlarge y limits=upper,
axis equal image, width=12cm,
xlabel=$x$, ylabel=$y$,
ytick=empty,
xticklabel style=font=small, text height=1.5ex, anchor=north,
samples=100
,
integral/.style=
domain=2:8,
samples=7
,
integral fill/.style=
integral,
draw=none, fill=#1,
%on layer=axis background
,
integral fill/.default=cyan!10,
integral line/.style=
integral,
very thick,
draw=#1
,
integral line/.default=black
begintikzpicture[
% The function that is used for all the plots
declare function=f(x)=x/5-cos(deg(x*1.85))/2+2;
]
beginaxis[
integral axis,
ymin=0,
xmin=0.75, xmax=9.25,
domain=1.5:8.5,
xtick=2,2.5,3,3.5,...,8,
xticklabels=$x_0$, $x_1/2$, $x_1$,$x_3/2$,$x_2$,$x_5/2$,$x_3$,,,,$x_N-1$,$x_N-frac12$,$x_N$,
axis on top
]
% The filled area under the approximate integral
addplot [integral fill=cyan!15] f(x) closedcycle;
% The highlighted segment
%addplot [integral fill=cyan!35, domain=6:7, samples=2] f closedcycle;
% The function
addplot [very thick, cyan!75!blue] f(x) node [anchor=south] $y=f(x)$;
% The approximate integral
addplot [integral line=black] f(x);
% The vertical lines between the segments
addplot [integral, ycomb] f(x);
% Issue is here.
pgfplotsinvokeforeach2.5,3.5,...,7.5
draw[thick,blue] (#1-0.5,f(#1)) rectangle(#1+.5,0);
endaxis
endtikzpicture
enddocument
Of course, you could plot these bars also with a bar
plot. That is, you could replace the loop by
addplot [blue,thick,ybar,bar width=1,samples at=2.5,3.5,...,7.5] f(x);
edited 2 days ago
answered 2 days ago


marmot
56.8k462124
56.8k462124
Mr @marmot, thank you one more time. Just for curiosity, is possible to add some discrete evaluated points, lets say inf(x)
, at some sample positions without to add a line that connects each sampled value?
– Lin
2 days ago
1
found:only marks
.
– Lin
2 days ago
@Lin Yes, that's how this can be done. (I'm already in hibernation mode. ;-)
– marmot
2 days ago
add a comment |Â
Mr @marmot, thank you one more time. Just for curiosity, is possible to add some discrete evaluated points, lets say inf(x)
, at some sample positions without to add a line that connects each sampled value?
– Lin
2 days ago
1
found:only marks
.
– Lin
2 days ago
@Lin Yes, that's how this can be done. (I'm already in hibernation mode. ;-)
– marmot
2 days ago
Mr @marmot, thank you one more time. Just for curiosity, is possible to add some discrete evaluated points, lets say in
f(x)
, at some sample positions without to add a line that connects each sampled value?– Lin
2 days ago
Mr @marmot, thank you one more time. Just for curiosity, is possible to add some discrete evaluated points, lets say in
f(x)
, at some sample positions without to add a line that connects each sampled value?– Lin
2 days ago
1
1
found:
only marks
.– Lin
2 days ago
found:
only marks
.– Lin
2 days ago
@Lin Yes, that's how this can be done. (I'm already in hibernation mode. ;-)
– marmot
2 days ago
@Lin Yes, that's how this can be done. (I'm already in hibernation mode. ;-)
– marmot
2 days ago
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f450199%2ftikz-evaluate-declared-functions-inside-foreach%23new-answer', 'question_page');
);
Post as a guest
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
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
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