TeX - LaTeX Asked on September 1, 2021
I am trying to plot $x^{-2},x^{-4}$
and $x^{-4}$
simulteneously using the code.
documentclass[10pt]{article}
usepackage{pgf,tikz,pgfplots}
pgfplotsset{compat=1.15}
usepackage{mathrsfs}
usetikzlibrary{arrows}
pagestyle{empty}
begin{document}
begin{center}
begin{tikzpicture}
begin{axis}
[width=2.5in,
axis line style = thick,
ymin=-1.5,ymax=3.5,
xmin=-3.5,xmax=5,
%clip=false,
xtick=empty,
ytick={-1,0,1,2,3},
extra x ticks={3.14, 6.28},
every extra x tick/.style={
xticklabel style={anchor=north west},
axis lines = center,
xlabel=$x$,ylabel=$y$,
domain=-3.5:3.5,
samples=200,
]
addplot [domain=-3.5:3.5, thick] {(x^(-2))};
addplot [domain=-3.5:3.5, black, thick] {(x^(-4))};
addplot [domain=-3.5:3.5, black, thick] {(x^(-6))};
node at (axis cs:0.2, -0.22) {$O$} ;
end{axis}
end{tikzpicture}
end{document}
When I try to run only with $x^{-2},$
I am getting the error, dimension too large. When I try to run all the three $x^{-2}, x^{-4}$
and $x^{-6}.$
Then I am getting the error File ended while scanning use of pgfplots@@environment@axis
.
I do not know how to fix the problem because every thing seems to me correct.
You have several problems in your code.
Indeed, you open environments or braces and do not close them.
For example, here, you begin center
environment without ending it.
But the error you get from pgfplots
comes from the fact that you open a brace after every extra x tick/.style
without closing it.
Then, the dimension too large comes from the fact that the functions you are trying to compute are indeed becoming too large for pgf
to compute when x
is close to 0 since they tend to infinity, and you even try to make pgf
compute 1/0 with the domain you define.
A way to avoid that would be to split your graphs in two parts, one stopping somewhere before 0 and the other starting after 0.
Another solution is to use the key restrict y to domain
in the axis environment to filter out the y
values when they become too large.
A functioning example with the restriction key is:
documentclass{standalone}
usepackage{pgf,tikz,pgfplots}
usetikzlibrary{arrows}
pgfplotsset{compat=1.15}
usepackage{mathrsfs}
begin{document}
begin{tikzpicture}
begin{axis}[%
width = 2.5in,
ymin = -1.5,
xmin = -3.5,
ymax = 3.5,
xmax = 5,
xtick = empty,
extra x ticks = {3.14, 6.28},
xticklabel style = {%
anchor = north east
},
ytick = {-1,0,1,2,3},
axis lines = center,
xlabel = $x$,
ylabel = $y$,
domain = -3.5:4.8,
restrict y to domain = -1.5:5,
samples = 200,
axis line style = thick,
]
addplot [thick] {1/(x^2)};
addplot [black, thick] {1/(x^4)};
addplot [black, thick] {1/(x^6)};
node[below right] at (axis cs: 0, 0) {$O$};
end{axis}
end{tikzpicture}
end{document}
which yields:
As a side note, if you declare a domain
key in the axis
environment options, it's no use declaring it again in the addplot
options unless it is different.
Answered by KersouMan on September 1, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP