1

Problematic

Thanks to schrodinger's cat visualizing-a-function-defined-through-a-summation, I adapted his answer to display the value of a bond security that pays a fix amount (Coupon \C) over a certain amount of time. It's the equation in the title of the plot.

Questions

When I try do display the DF function DF(\n,\r)=pow((1+\r),-\n) it's smooth.

Why do I get this strange wavelet when I introduce the sum ?

enter image description here

enter image description here

MWE

\documentclass[tikz,export,fleqn]{standalone}
\usepackage{animate}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\newcounter{isum}
\pgfplotsset{summand/.initial=max}
%-------- On définit la somme   ----------------  
\pgfmathdeclarefunction{sum}{2}{%
\begingroup%
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
\edef\myfun{\pgfkeysvalueof{/pgfplots/summand}}%
\pgfmathsetmacro{\mysum}{0}%
\pgfmathsetmacro{\myx}{#2}%
\pgfmathtruncatemacro{\imax}{#1}%
\setcounter{isum}{1}%
\loop
\pgfmathsetmacro{\mysum}{\mysum+\myfun(\value{isum},#2)}%
\ifnum\value{isum}<\imax\relax
\stepcounter{isum}\repeat
\pgfmathparse{\mysum}%
\pgfmathsmuggle\pgfmathresult\endgroup%
}%
\begin{document}

%Different levels of \C, they represent percentage to a dollar
    \pgfplotsinvokeforeach{0.01,0.02,...,0.04}{
        \begin{tikzpicture}[
            declare function={DF(\n,\r)=pow((1+\r),-\n);},
            ]
            \begin{axis}[
            xlabel=Rates,
            ylabel=Time to Maturity,
            zlabel=Bond value,,             
            title={$P=\sum_{k=1}^{n} \frac{#1}{(1+r)^{k}}+\frac{1}{(1+r)^{n}}$},
                    domain=0.0:0.05,    % Interest rates
                    y domain=0:10,  % Maturity
                    view={30}{20},
                ]

\def\C{0.05}
    \addplot3[summand=DF,surf]  {#1*sum(y,x)+ DF(y,x)};
%   \addplot3[summand=DF,surf]  {\C*sum(y,x)+ DF(y,x)};
%   \addplot3[summand=DF,surf]  {DF(y,x)};
            \end{axis}
        \end{tikzpicture}
    }
    \end{document}
JeT
  • 3,020
  • The answer is very simple, I think. You plot #1*sum(y,x)+ DF(y,x) where #1 is a constant. The sum(y,x) evaluates to a sum over i from 1 to int(y) over DF(i,x). Clearly, int(y) has steps. I think that if you explain what you really want to plot it will be possible to make this happen. –  Apr 28 '20 at 02:25
  • Off-topic, wavelets are a very specific type of signals :) . So these are much more steps than wavelets. – BambOo Apr 28 '20 at 08:32
  • I changed the title. – JeT Apr 28 '20 at 08:33
  • Did you have a look at Schrödinger's cat comment ? – BambOo Apr 28 '20 at 08:41
  • Yes ! Working on it right now. – JeT Apr 28 '20 at 08:42
  • Note that if you want to address a user or let him know you answered, you need to point this user using an @ call. In your case, as this is your question, you are automatically pinged for any activity so we do not have to do it. – BambOo Apr 28 '20 at 09:36
  • Sure will do ! :) – JeT Apr 28 '20 at 09:40
  • Any news ?????? – BambOo Apr 30 '20 at 12:30
  • @BambOo locked home with a toddler, i can hardly seat 5 mins in peace :) Schrodinger's cat is right, I use maturity as an integer not as an y varying continuously. – JeT Apr 30 '20 at 13:57
  • @BambOo @Schrodinger's cat showed me I was using wrongly an integer parameter (n) to pass it as a real number (y). Shall we close this question ? – JeT May 01 '20 at 13:28
  • @BambOo just found https://tex.stackexchange.com/questions/455721/how-to-pass-an-array-to-function-in-pgfmathdeclarefunction that I need to study. – JeT May 01 '20 at 13:57
  • @Julien-ElieTaieb, I asked a while ago how to handle sums inside addplots, not totally sure this could help but maybe you should have a look https://tex.stackexchange.com/questions/429508/sum-inside-addplot-call – BambOo May 01 '20 at 14:43
  • @BambOo, thank you i'll look at it ! – JeT May 01 '20 at 23:42
  • @JeT, why did you remove the 3d tag from the question (I added earlier)? Having these tags helps me filtering when I am searching for duplicates. – Stefan Pinnow May 18 '22 at 15:00
  • @StefanPinnow my bad ! I put it back :) – JeT May 18 '22 at 17:23
  • @JeT, many thanks :) – Stefan Pinnow May 19 '22 at 11:13

0 Answers0