# Portfolio frontier with Pgfplots package

I'm currently trying to plot the portfolio frontier given a data set, but the hyperbola is not very pretty.. With the data I've calculated the following analytical expressions for the frontier:

Upper half: 0.665918 * (√(x2 - 7.00569) + 1.12816)

Lower half: −0.665918 * (√(x2 - 7.00569) - 1.12816)

My code for the plot of the frontier is:

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

\begin{tikzpicture}
\begin{axis}[
height=10cm, width=16cm,
axis x line=bottom, axis y line=left,
xlabel = Standard Deviation, ylabel = Expected Return,
ymin=-3, ymax=7, xmin=0, xmax=8,
enlargelimits=true,
]
\addplot[
domain = 2:10,
samples = 200,
smooth,
ultra thick,
blue,
] {0.665918 * (sqrt(x^2 - 7.00569) + 1.12816)};
\addplot[
domain = 2:10,
samples = 200,
smooth,
ultra thick,
green,
] {-0.665918 * (sqrt(x^2 - 7.00569) - 1.12816)};
\end{axis}
\end{tikzpicture}

\end{document}


Clearly, there's a huge gap in the middle - what is causing this and how do I fix it? I have another software that can plot the entire hyperbola without trouble - but it is not as clean and smooth as this plot.

## 1 Answer

The problem is that the function is very steep (infinitely so) around the cusp --- it is not defined for x<sqrt(7.00569) and then it jumps up abruptly.

Now, plotting between 2 and 10, even with 200 steps, you will find values for a point where the function does not exist and after that another one that have a value. But the probability to get exactly the cusp point is, well, 0. The slope is so high that you see the difference plotted.

One solution is to plot the function starting from the cusp.

\documentclass{standalone}
\usepackage{pgfplots}\pgfplotsset{compat=newest}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
height=10cm, width=16cm,
axis x line=bottom, axis y line=left,
xlabel = Standard Deviation, ylabel = Expected Return,
ymin=-3, ymax=7, xmin=0, xmax=8,
enlargelimits=true,
]
\addplot[
domain = 2.64683:10,
% domain = 2:10,
samples = 200,
ultra thick,
blue,
] {0.665918 * (sqrt(x^2 - 7.00569) + 1.12816)};
\addplot[
domain = 2.64683:10,
% domain = 2:10,
samples = 200,
ultra thick,
green,
] {-0.665918 * (sqrt(x^2 - 7.00569) - 1.12816)};
\end{axis}
\end{tikzpicture}
\end{document} The other possibility (probably better, but I'm too lazy to find out now, to avoid using too many samples) is to derive the function of the parabolic thing like an x=f(y) and plot the thing using a y domain, like in https://tex.stackexchange.com/a/375478/38080.

If you add this to your plot

 \addplot[
red, dotted, mark=*, samples=100, domain=-4:5
]
({sqrt(7.00569 + ( x/0.665918-1.12816)^2)},{x});


you'll have: 