5

I have

succeeded in making a simple contour plot by using the function

\newcommand\expr[2]{ (-#2^4+#2^2)*e^-(#2^2+#1^2) + (cos((-#1/1.85*pi-0.45) r)/2.0-#1/3.0)/1.5 }

MWE:

\documentclass{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

\usepackage[dvipsnames]{xcolor}

\usepackage{tikz,pgfplots,pgfplotstable,pgffor,pgfmath,tikz-3dplot}

%===================================================
\usetikzlibrary{positioning,plotmarks,pgfplots.colormaps,external,3d,calc,arrows}
\usetikzlibrary{decorations,decorations.pathmorphing,decorations.pathreplacing}
\usetikzlibrary{shapes,arrows}
\tikzexternalize[mode=convert with system call,shell escape=-enable-write18]
%===================================================

%==== Tikz & PGF Camera alignment macro ============
% Style to set TikZ camera angle, like PGFPlots `view`
\tikzset{viewport/.style 2 args={
    x={({cos(-#1)*1cm},{sin(-#1)*sin(#2)*1cm})},
    y={({-sin(-#1)*1cm},{cos(-#1)*sin(#2)*1cm})},
    z={(0,{cos(#2)*1cm})}
}}
%===================================================

%Define standard arrow tip
\tikzset{>=stealth',}

% Color Map
\pgfplotsset{
    colormap={colormap2}{
        color(0cm)=(blue!70!black);
        color(1cm)=(cyan!60!black)
    }
}

%==== Externalisation ==============================
\makeatletter
\newcommand{\useexternalfile}[2][Tikz] % \useexternalfile[Folder]{Filename}
{
    \tikzsetnextfilename{#2-output}
    \input{#1/#2.tikz}
}
\makeatother
%===================================================


\begin{document}

\begin{tikzpicture}

    \newcommand\expr[2]{ (-#2^4+#2^2)*e^-(#2^2+#1^2) + (cos((-#1/1.85*pi-0.45) r)/2.0-#1/3.0)/1.5 }

    %==== 2D Contour Plot =============
    \begin{axis}
    [
        view={0}{90},
        scale only axis,
        colormap name={colormap2},
        ticks=none
    ]

    \addplot3   % contour
    [
        very thick,
        domain=-2*pi:2*pi,
        domain y=-2*pi:2*pi,
        contour gnuplot={ number=24, output point meta=rawz, labels=false, },
        samples=70,
        samples y=70,
    ]
    (
        {x},                      % x
        {y},              % y
        {\expr{x}{y}} %z
    );
    \end{axis}
    %==========================


\end{tikzpicture}

\end{document}

producing the following image

enter image description here

My Question

How do I do such a plot with the more complicated function

\newcommand\fun[2]
{
    (atan(sqrt(#1*#1 + #2*#2)))^2
}
\newcommand\expr[2]
{
    1.2*\fun{(#1+2)/2}{(#2-2)/2}
    +\fun{(#1-2)/2}{(#2+2)/2}
    -0.5*\fun{(#1+4)/2}{(#2+4)/1.5}
    -0.6*\fun{(#1-4)/3}{(#2-4)/2}
    -0.75*\fun{(#1+0.25)/1.25}{#2/1.25}
}

I get multiple Illegal parameter number in definition of \pgfmathfloat@test and the same with \pgfmathfloat@expression, \pgfmathfloat@parse, \pgfmathfloat@next, \pgfmathfloat@token and \pgfmathfloat@bgroup.

The relevant error probably is the following

! Package PGF Math Error: Could not parse input '' as a floating point number, sorry.
The unreadable part was near ''.
(in ' { 1.2*{((atan((sqrt(##1^2 + ##2^2)) r))^2)} +{((atan((sqrt(##1^2 + ##2^2)) r))^2)} -0.5*{((atan((sqrt(##1^2 + ##2^2)) r))^2)} -0.6*{((atan((sqrt(##1^2 + ##2^2)) r))^2)} -0.75*{((atan((sqrt(##1^2 + ##2^2)) r))^2)} } ').

Any ideas how to correctly write this function? Are there general rules to follow when functions get as complex as this? I assume it has something to do with the nested commands, producing ##1 and ##2?

It is supposed to

reproduce the following Mathematica code

Mathematica

2

I have succeeded in plotting this function by using gnuplot inside the PGF Plot: Surface

The image is produced by the following MWE:

\documentclass{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

\usepackage[dvipsnames]{xcolor}

\usepackage{tikz,pgfplots,pgfplotstable,pgffor,pgfmath,tikz-3dplot}

%===================================================
\usetikzlibrary{positioning,plotmarks,pgfplots.colormaps,external,3d,calc,arrows}
\usetikzlibrary{decorations,decorations.pathmorphing,decorations.pathreplacing}
\usetikzlibrary{shapes,arrows}
\tikzexternalize[mode=convert with system call,shell escape=-enable-write18]
%===================================================


%==== Tikz & PGF Camera alignment macro ============
% Style to set TikZ camera angle, like PGFPlots `view`
\tikzset{viewport/.style 2 args={
    x={({cos(-#1)*1cm},{sin(-#1)*sin(#2)*1cm})},
    y={({-sin(-#1)*1cm},{cos(-#1)*sin(#2)*1cm})},
    z={(0,{cos(#2)*1cm})}
}}
%===================================================

%Define standard arrow tip
\tikzset{>=stealth',}

% Color Map
\pgfplotsset{
colormap={colormap2}{
    color(0cm)=(blue!70!black);
    color(1cm)=(cyan!60!black)
}
}

%==== Externalisation ==============================
\makeatletter
\newcommand{\useexternalfile}[2][Tikz] % \useexternalfile[Folder]{Filename}
{
\tikzsetnextfilename{#2-output}
\input{#1/#2.tikz}
}
\makeatother
%===================================================

\begin{document}




\begin{tikzpicture}

%==================================
%==== 3D Surface Plot =============
\begin{axis}
[
view={25}{70},
scale only axis,
colormap name={colormap2},
ticks=none,
width=0.3\textwidth,
at={(-0.3\textwidth,0)}
]

\addplot3
[
raw gnuplot,
surf,
%           contour prepared,
%           contour/labels=false,
samples=20,
]
gnuplot
{%
    f(x,y) = (atan(sqrt(x**2 + y**2)))**2;
    fun(x,y) = 1.2*f((x+2)/2,(y-2)/2) + f((x-2)/2,(y+2)/2) - 0.5*(f((x+4)/2,(y+4)/1.5)) - 0.6*(f((x-4)/3,(y-4)/2)) - 0.75*(f((x+0.25)/1.25,y/1.25)) - 0.025*x;
    set samples 50, 50;
    set isosamples 51, 51;
    %           set contour base;
    %           set cntrparam levels 15;
    set xrange [-1.0*pi:1.0*pi];
    set yrange [-1.0*pi:1.0*pi];
    splot fun(x,y);
};

\end{axis}
%==================================
%==================================

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.