关于空间曲面上的一点,如何绘制出空间向量?

发布于 2024-04-03 22:00:48

vector_quest2.png

老师,如图,曲面有一个已知点,实际上此曲面上任意点的法向量都同时受到正弦函数与对数函数的双重影响,如何准确(或近似)绘制出这点的法向量呢?

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepgfplotslibrary{colormaps,fillbetween}
\begin{document}
\begin{tikzpicture}
[declare function={f(\x,\y)=0.8*sin(deg(\x))+1.2*ln(\y);},]    
\begin{axis}[axis equal image,view={-45}{15},domain=-2:5,y domain=0:5,samples=20,axis lines =center,
axis on top,
xmax=6,ymax=8,zmax=4,xmin=-2,ymin=-1,zmin=-2.5,xlabel=\tiny{$X$},ylabel=\tiny{$Y$},zlabel=\tiny{$Z$}]
\addplot3[colormap/bone,surf,                       
z buffer=sort,] (x,y,{f(x,y)});
%%%define P
\addplot3+ [mark=ball,mark size=2pt,scatter src=rand,ball color=yellow!80!black!60] 
coordinates {({0.25*pi},{3},{f(x,y)})};
\end{axis}
\end{tikzpicture}
\end{document}

查看更多

关注者
0
被浏览
368
1 个回答
雾月
雾月 2024-04-04
这家伙很懒,什么也没写!

需要自己计算在该点的法向量,但是 pgf 的计算精度不是很高,而且比较准确的数值算法会很慢。
image.png

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{calc}
\usepgfplotslibrary{colormaps,fillbetween}

\makeatletter
\newcommand\computegrad[4][0.00025]{% [delta], function, x, y
  \begingroup\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
    \def\@tempdelta{#1}%
    \pgfmathparse{#2(#3,#4)}\let\@tempz\pgfmathresult
    \pgfmathparse{#2(#3+\@tempdelta,#4)}\let\@temppxa\pgfmathresult
    \pgfmathparse{#2(#3-\@tempdelta,#4)}\let\@temppxb\pgfmathresult
    \edef\@temppx{\fpeval{(\@temppxa-\@temppxb)/(2*\@tempdelta)}}%
    \pgfmathparse{#2(#3,#4+\@tempdelta)}\let\@temppya\pgfmathresult
    \pgfmathparse{#2(#3,#4-\@tempdelta)}\let\@temppyb\pgfmathresult
    \edef\@temppy{\fpeval{(\@temppya-\@temppyb)/(2*\@tempdelta)}}%
    \edef\@tempu{\fpeval{(\@temppx,\@temppy,-1)/sqrt(\@temppx^2+\@temppy^2+1)}}%
    \edef\?{\endgroup\def\noexpand\pgfmathresult{\@tempu}%
      \def\noexpand\pgfmathresulta{\fpeval{(#3,#4,\@tempz)+\@tempu}}}%
  \?}
\makeatother

\begin{document}
\begin{tikzpicture}[declare function={f(\x,\y)=0.8*sin(deg(\x))+1.2*ln(\y);}]
\begin{axis}[axis equal image,view={-45}{15},
  domain=-2:5,y domain=0:5,samples=20,axis lines=center,axis on top,
  xmax=6,ymax=8,zmax=4,xmin=-2,ymin=-1,zmin=-2.5,
  xlabel=\tiny $X$,ylabel=\tiny $Y$,zlabel=\tiny $Z$]
\addplot3[colormap/bone,surf,z buffer=sort,] (x,y,{f(x,y)});
%%define P
\addplot3+ [mark=ball,mark size=2pt,scatter src=rand,ball color=yellow!80!black!60]
  coordinates {({0.25*pi},{3},{f(x,y)})};
\draw[->,shorten <=-1cm] ({0.25*pi},{3},{f(0.25*pi,3)}) 
  \pgfextra{\computegrad{f}{0.25*pi}{3}} -- \pgfmathresulta;

\draw[red,->,shorten <=-1cm] (0.785398, 3., 1.88402)--(1.35108, 3.4, 0.88402);
\end{axis}
\end{tikzpicture}
\end{document}

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览