[Tex/LaTex] Declare function for tikzpicture

diagramspgfplotstikz-pgf

I'd like to declare a function that will be part of the equation that I then want to plot. The equation that I want to plot is defined as

\frac{e^{-rT}N'(d_{2})}{S\sigma\sqrt{T}}

The function to be declared is the probability density function

N'\left (x  \right )=\frac{1}{\sqrt{2\pi}}e^{-x^2/2}

where x should equal to d_2, and d_2 must also be declared by

d_{2}=\frac{ln(S/K)+(r-\frac{\sigma^2}{2})T}{\sigma\sqrt{T}})

If possible I'd like to be able to define K, r and sigma in advance, so that they can flow into above calculation as fixed values. For the values of S (x axis) and T (y axis) I want to take a different domains, e.g. for S a range 95 to 105, and for T a range 0.01 to 0.3. The overall result should be a surfaceplot where the results of the intial equation are on the z axis. Thanks a million, I hope so much that somebody can help me!

ADDITION: the code that was provided by @percusse works very well but when I try to modify it following the logic of its structure I get an error. Here is the modified code:

\documentclass{article}
\usepackage{pgfplots}
\begin{document} 

\begin{tikzpicture}[
declare function={ Nprime(\x)                 = 1/(sqrt(2*pi))*exp(-0.5*(pow(\x,2))); 
                   d1(\x,\y,\KK,\RR,\SIG)     = (ln(\x/\KK)+(\RR+(pow(\SIG,2)/2)*\y))/(\SIG*(sqrt(\y)));
                   d2(\x,\y,\KK,\RR,\SIG)     = (ln(\x/\KK)+(\RR-(pow(\SIG,2)/2)*\y))/(\SIG*(sqrt(\y)));
                   myfun(\x,\y,\KK,\RR,\SIG)  = - (exp(-\RR*\y)*d1*Nprime(d2(\x,\y,\KK,\RR,\SIG))/(\y*(pow(\x,2))*(pow(\SIG,2))));
                 },
]
\begin{axis}[ y domain=0.01:0.3, domain=95:105,view={25}{20}, 
]
\addplot3[surf] {myfun(x,y,100,0,0.09)};
\end{axis}
\end{tikzpicture}
\end{document} 

What am I doing wrong?

ONE MORE ADDITION: I have tried to swap the \y and \SIG values. Here is the code that does not work

\documentclass{article}
\usepackage{pgfplots}
\begin{document} 
\begin{tikzpicture}[
declare function={ Nprime(\x)                 = 1/(sqrt(2*pi))*exp(-0.5*(pow(\x,2))); 
                   d2(\x,\SIG,\KK,\RR,\y)     = (ln(\x/\KK)+(\RR-(pow(\SIG,2)/2)*\y))/(\SIG*(sqrt(\y)));
                   myfun(\x,\SIG,\KK,\RR,\y)  = exp(-\RR*\y)*Nprime(d2(\x,\SIG,\KK,\RR,\y))/(\x*\SIG*sqrt(\y));
                 },
]
\begin{axis}[
y domain=0.01:0.09, domain=95:105, view={25}{20}, 
]
\addplot3[surf] {myfun(x,SIG,100,0,0.04)};
\end{axis}
\end{tikzpicture}
\end{document} 

What's wrong with it? Thanks a lot!

Best Answer

I don't know what it should look like but from the constants you have provided I get this

\documentclass{article}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}[
declare function={ Nprime(\x)                 = 1/(sqrt(2*pi))*exp(-0.5*(pow(\x,2))); 
                   d2(\x,\y,\KK,\RR,\SIG)     = (ln(\x/\KK)+(\RR-(pow(\SIG,2)/2)*\y))/(\SIG*(sqrt(\y)));
                   myfun(\x,\y,\KK,\RR,\SIG)  = exp(-\RR*\y)*Nprime(d2(\x,\y,\KK,\RR,\SIG))/(\x*\SIG*sqrt(\y));
                 },
]
\begin{axis}[y domain=0.01:0.3,domain=95:105,view={150}{20}]
\addplot3[surf] {myfun(x,y,100,0,0.09)};
\end{axis}
\end{tikzpicture}


\end{document}

enter image description here

Related Question