
Te comparto el código fuente de una figura que elaboré usando TikZ de LaTeX2e para graficar una función de dos variables usando TikZ (sin usar el paquete pgfplots
).
Te explico grosso modo el código fuente.
Empiezo cargando los paquetes que se requieren para generar la diapositiva (TikZ
y su librería math
al igual que el paquete tikz-3dplot
).
Indico información sobre el archivo en un comentario de varias líneas.
Primero indico los valores de los límites de la región donde se grafica la función: en el eje se grafica en el intervalo
[\xi, \yi]
, mientras que en el eje se grafica en el intervalo
[\xf, \yf]
. Defino el número de intervalos (\n
) siendo en este caso para ambos ejes el mismo valor. A partir de los valores anteriores calculo el tamaño del incremento \step
(para ambos ejes uso el mismo valor.)
Los valores \xsig, \ysig, \xant, \yant
me sirven para indicar en cada ciclo \foreach
cuál es el siguiente valor en la variable que controla el ciclo y cuál es el último, de manera que grafique hasta el punto (\xf, \yf)
.
A continuación se enlista el código fuente.
\documentclass{article} \usepackage{tikz} \usepackage{tikz-3dplot} \usetikzlibrary{math} \usepackage[active,tightpage]{preview} \PreviewEnvironment{tikzpicture} \setlength\PreviewBorder{1pt} % % Filename: SurfaceArea.tex % Description: % A surface, corresponding to the graph of a function of two variables ($z = f(x, y)$) is shown. % % Date of creation: March, 12th, 2023. % date of last modification: March, 12th, 2023. % Autor: Efraín Soto Apolinar. % https://www.aprendematematicas.org.mx/author/efrain-soto-apolinar/instructing-courses/ % Terms of use: % https://creativecommons.org/licenses/by-nc-sa/4.0/ % Your commitment to the terms of use is greatly appreciated. % \begin{document} % \tdplotsetmaincoords{70}{110} % \begin{tikzpicture}[tdplot_main_coords,scale=1.5] % Definition of the function to be plotted \tikzmath{function f(\x,\y) {return 0.5 + 0.75 * cos((\x * \x + \y * \y) r) / sqrt(1.0 + \x * \x + \y * \y) ;};} % Domain for the surface plot: from [\xi,\yi] to [\xf,\yf] \pgfmathsetmacro{\xi}{-3.0} \pgfmathsetmacro{\xf}{3.0} \pgfmathsetmacro{\yi}{-3.0} \pgfmathsetmacro{\yf}{3.0} \pgfmathsetmacro{\n}{60} % Number of subintervals \pgfmathsetmacro{\step}{(\xf - \xi) / \n} % size of each subinterval % Basically, \step is the length of the horizontal projection of the side of the surface element \pgfmathsetmacro{\xsig}{\xi + \step} % Auxiliary [next x] \pgfmathsetmacro{\ysig}{\yi + \step} % Auxiliary [next y] \pgfmathsetmacro{\xant}{\xf - \step} % Auxiliary [previous value of x (with respect to the final value)] \pgfmathsetmacro{\yant}{\yf - \step} % Auxiliary [previous value of y (with respect to the final value)] \pgfmathsetmacro{\ejez}{f(0,0) + 1.0} % Length of the z-axis % \draw[dashed,->] (0,0,0) -- (\xi,0,0); % Negative part of the x-axis \draw[dashed,->] (0,0,0) -- (0,\yi,0); % Negative part of the y-axis % % Region #1: from (\xi, \yi) to (0,\yf) % \foreach \x in {\xi,\xsig,...,-\step}{ \foreach \y in{\yi,\ysig,...,\yant}{ % Coordinates of the vertices of each surface element \pgfmathsetmacro{\Ax}{\x} \pgfmathsetmacro{\Ay}{\y} \pgfmathsetmacro{\Az}{f(\Ax,\Ay)} \pgfmathsetmacro{\Bx}{\x + \step} \pgfmathsetmacro{\By}{\y} \pgfmathsetmacro{\Bz}{f(\Bx,\By)} \pgfmathsetmacro{\Cx}{\x + \step} \pgfmathsetmacro{\Cy}{\y + \step} \pgfmathsetmacro{\Cz}{f(\Cx,\Cy)} \pgfmathsetmacro{\Dx}{\x} \pgfmathsetmacro{\Dy}{\y + \step} \pgfmathsetmacro{\Dz}{f(\Dx,\Dy)} \fill[cyan!50,opacity=0.75] (\Ax,\Ay,\Az) -- (\Bx,\By,\Bz) -- (\Cx,\Cy,\Cz) -- (\Dx,\Dy,\Dz) -- cycle; \draw[blue,ultra thin] (\Ax,\Ay,\Az) -- (\Bx,\By,\Bz) -- (\Cx,\Cy,\Cz) -- (\Dx,\Dy,\Dz) -- cycle; } } % Draw the coordinate axis \draw[thick,->] (0,0,0) -- (\xf+0.5,0,0) node [below left] {\footnotesize$x$}; \draw[thick,->] (0,0,0) -- (0,\yf+0.5,0) node [right] {\footnotesize$y$}; \draw[thick,->] (0,0,0) -- (0,0,\ejez) node[above] {$z$}; % Eje z % % Region #2: from (0, \yi) to (\xf,\yf) % \foreach \x in {0.0,\step,...,\xant}{ \foreach \y in{\yi,\ysig,...,\yant}{ % Coordinates of the vertices of each surface element \pgfmathsetmacro{\Ax}{\x} \pgfmathsetmacro{\Ay}{\y} \pgfmathsetmacro{\Az}{f(\Ax,\Ay)} \pgfmathsetmacro{\Bx}{\x + \step} \pgfmathsetmacro{\By}{\y} \pgfmathsetmacro{\Bz}{f(\Bx,\By)} \pgfmathsetmacro{\Cx}{\x + \step} \pgfmathsetmacro{\Cy}{\y + \step} \pgfmathsetmacro{\Cz}{f(\Cx,\Cy)} \pgfmathsetmacro{\Dx}{\x} \pgfmathsetmacro{\Dy}{\y + \step} \pgfmathsetmacro{\Dz}{f(\Dx,\Dy)} \fill[cyan!50,opacity=0.75] (\Ax,\Ay,\Az) -- (\Bx,\By,\Bz) -- (\Cx,\Cy,\Cz) -- (\Dx,\Dy,\Dz) -- cycle; \draw[blue,ultra thin] (\Ax,\Ay,\Az) -- (\Bx,\By,\Bz) -- (\Cx,\Cy,\Cz) -- (\Dx,\Dy,\Dz) -- cycle; } } % \end{tikzpicture} % \end{document} |
Si aún no conoces LaTeX2e, te invito a aprenderlo con nuestro curso en línea gratuito, titulado LaTeX2e para principiantes.
A continuación te muestro la figura.

0 responses on "Gráfica de una superficie usando TikZ"