# Declare function for tikzpicture

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},
]
\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},
]
\end{axis}
\end{tikzpicture}
\end{document}


What's wrong with it? Thanks a lot!

• I fear that your aim is definitely not possible : in general the elaborate LaTeX formulas cannot be parsed by the pgf engine. Just as an example, there is not \frac and your denominator would be writen as \S*\sig*sqrt(\T) provided that you have \def-ined the values of \S,\sig and \T (note te absence of \ before sqrt to {evaluate} de square root !
– Jhor
Jul 21 '13 at 11:17
• can you provide some values of the constants? Jul 21 '13 at 11:28
• K = 100, r = 0, sigma = 0.09 Jul 21 '13 at 11:29
• the thing is, that I have that graph in matlab and I've tried to use matlab2tikz to transform the matlab-figure to latex. matlab2tikz does it with some distrotion, i.e. it simply chops off a part of the graph and places that piece arbitrarily somewhere else Jul 21 '13 at 11:35
• @ajafarov You are missing the arguments of d1 Jul 21 '13 at 14:36

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}]

• @ajafarov You can rotate the 3D view angles with view option added to the axis options. Replace it with view={25}{20}. Jul 21 '13 at 11:43