I would like to plot the graph of a simple equation like x^2+xy+2y^2=1 using TikZ. There are some posts on implicit plots here, but nothing worked in my case.
I am using TeXShop Version 3.92 and \documentclass{book}.
I would like to plot the graph of a simple equation like x^2+xy+2y^2=1 using TikZ. There are some posts on implicit plots here, but nothing worked in my case.
I am using TeXShop Version 3.92 and \documentclass{book}.
The answer of question 18359 works for me. Changed line:
splot x^2 + x*y + 2*y^2 - 1;
And I added the line
set samples 500;
for a smoother curve.
Full example:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot +[no markers,
raw gnuplot,
thick,
empty line = jump % not strictly necessary, as this is the default behaviour in the development version of PGFPlots
] gnuplot {
set contour base;
set cntrparam levels discrete 0.003;
unset surface;
set view map;
set isosamples 500;
set samples 500;
splot x^2 + x*y + 2*y^2 - 1;
};
\end{axis}
\end{tikzpicture}
\end{document}
This solution requires that gnuplot is installed and available on the command line. Depending on the TeX configuration (texmf.cnf in TeX Live), usually option -shell-escape (TeX Live)/-enable-write18 (MiKTeX) for the TeX compiler is needed to run the example, see the comment of Sari.
The example of question 285246 also works with the following function:
f(x,y) = x**2 + x*y + 2*y**2 - 1;
(x2 is expressed as x**2.)
Full example:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw plot[id=question1, raw gnuplot] function{
f(x,y) = x**2 + x*y + 2*y**2 - 1;
set xrange [-4:4];
set yrange [-4:4];
set view 0,0;
set isosample 1000, 1000;
set size square;
set contour base;
set cntrparam levels incre 0,0.1,0;
unset surface;
splot f(x,y);
};
\end{tikzpicture}
\end{document}
.log file, it should contain something like runsystem(gnuplot test.pgf-plot.gnuplot)...executed..
– Heiko Oberdiek
Dec 09 '17 at 15:57
runsystem(gnuplot "DAPM-13-SA-Quadratic forms.question1.gnuplot")...executed.
– Piotr Mikusinski Dec 09 '17 at 16:12system returned with code 256. Unhappily, this is not written to the .log file. I do not know, whether TeXShop is able to catch it. Therefore, it is better to run on the command line, e.g. pdflatex -shell-escape test and check the messages on the screen.
– Heiko Oberdiek
Dec 09 '17 at 16:28
Ellipse.question1.table' not found. on input line 20. This is what I got when I run "pdflatex -shell-escape test": This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015) (preloaded format=pdflatex) \write18 enabled. entering extended mode ! I can't find filetest'.
<*> test
– Piotr Mikusinski
Dec 09 '17 at 16:45
gnuplot command did not succeed (the output file was not generated). Before the warning there can be a message, warning, error with the reason. Further tests: gnuplot --version should print the version number of gnuplot. Also, there should be a file Ellipse.question1.gnuplot, the source code for gnuplot. Test: gnuplot Ellipse.question1.gnuplot on the command line.
– Heiko Oberdiek
Dec 09 '17 at 17:00
What about this?
\documentclass{article}
\usepackage{pgfplots} % It is based on tikz!
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot3[
surf,
samples=20,
domain=-100:100,
y domain=-100:100
]
{x^2+x*y+2*y^2-1};
\end{axis}
\end{tikzpicture}
\end{document}
hide axis option of axis environment.
–
Dec 09 '17 at 17:42
\begin{tikzpicture}
\begin{axis}[hide axis,xmin=-2,xmax=2,ymin=-2,ymax=2,]
\addplot [blue,thick,domain=0:360,samples=200]({(cos(x)+sin(x)},{sin(x)});
\end{axis}
\end{tikzpicture}
If you don't mind a not-tikz solution, the mfpic package, interface to the MetaPost language, seems to work quite well in this kind of problem with its \levelcurve command.
\documentclass[border=3bp]{standalone}
\usepackage[metapost, mplabels, truebbox]{mfpic}
\setlength{\mfpicunit}{1cm}
\opengraphsfile{\jobname}
\begin{document}
\begin{mfpic}[2]{-1.5}{1.5}{-1.5}{1.5}
\drawcolor[gray]{0.7}
\gridlines{.5,.5}
\drawcolor{black}
\levelcurve{origin, .01}{x**2 + x*y + 2(y**2) < 1}
\doaxes{xy}
\tlpointsep{3bp}
\tlabels{[tc]{(-1, 0)}{$-1$} [tc]{(1, 0)}{$1$} [tc]{(\xmax, 0)}{$x$}}
\tlabels{[cr]{(0, -1)}{$-1$} [cr]{(0, 1)}{$1$} [cr]{(0, \ymax)}{$y$}}
\end{mfpic}
\closegraphsfile
\end{document}
With TeXShop, process this TeX file with (PDF)LaTeX, then with MetaPost, and finally with LaTeX again.
See the mfpic documentation p.44-45 for more information on this most useful command.