To preface, I'm very, very new to Mathematica. I've written a few functions to calculate compound dividends from stocks.

compoundDivsInterest[shares_, divyield_, years_, compoundrate_] := 
   ((shares (1 + (divyield/compoundrate))^(compoundrate*years) - shares))

compoundDivsTotal[shares_, divyield_, years_,compoundrate_] := 
   ((shares (1 + (compoundrate))^(compoundrate*years)))

TL;DR: I can't seem to get these functions to graph their correct exponential form. I've tried using a sum, using Show[Table[Plot[..., For[...Plot[..., and a "recursive" function. None have worked so far. What should I do to get them to plot correctly?

As they are now, they will give the right value for a given x, but any other values are incorrect (as the function doesn't technically compound off the new principal). If I graph the functions, they are linear in nature.

A friend better versed in Mathematica than I suggested I use a sum to graph them. Plot[Sum[compoundDivsTotal[x, 0.0659, 1, 4], {x, 1, n, 1}],{n, 0,1000}]

This does yield an exponential curve as would be expected, but none of the values are correct.

In order to plot the correct, exponential graph of compounded interest, I figured it would probably have to be done recursively or iteratively. I've tried many different ways from google, such as Show[Table[Plot[..., and For[...Plot[.... Finally, I tried doing the closest thing I could find to recursion, which was to re-implement the function as:

recCompoundDivsTotal[shares_,divyield_,years_,compoundrate_] := 
 recCompoundDivsTotal[shares] = 
   ((shares (1 +(divyield/compoundrate))^(compoundrate * years)))

It gives the right value, but it's still linear in nature even though, from what I understand, it ought to recursively iterate using the previous value of shares

I'm at a loss for what to do. Any ideas?

  • $\begingroup$ what is the parameters divyield? $\endgroup$ – Alex Trounev Jan 23 '19 at 19:53
  • $\begingroup$ It's the dividend yield of the stock as a decimal percentage. In effect, the interest rate of the stock. $\endgroup$ – vlca Jan 23 '19 at 20:37
  • $\begingroup$ And divyield=? $\endgroup$ – Alex Trounev Jan 23 '19 at 20:40
  • $\begingroup$ My bad. Use .0659. I've been calling the function using compoundDivsTotal[x, 0.0659, 1, 4] $\endgroup$ – vlca Jan 23 '19 at 20:45
  • $\begingroup$ So compoundrate = 4? It's a lot. $\endgroup$ – Alex Trounev Jan 23 '19 at 21:20

This is an option if the functions are defined correctly.

compoundDivsInterest[shares_, divyield_, years_, 
      compoundrate_] := ((shares (1 + (divyield/
              compoundrate))^(compoundrate*years) - shares))

compoundDivsTotal[shares_, divyield_, years_, 
  compoundrate_] := ((shares (1 + (compoundrate))^(compoundrate*

   Table[compoundDivsTotal[10^n, 0.0659, t, .4], {n, 2, 3}]], {t, 0, 
   10}, AxesLabel -> {"t, years", ""}, 
  PlotLabel -> "Compound Dividends Total", PlotLegends -> Automatic], 
   Table[compoundDivsTotal[s, 0.0659, t, .4], {n, 2, 3}]], {t, 0, 
   10}, {s, 0, 1000}, AxesLabel -> {"t, years", "shares", ""}, 
  PlotLabel -> "Compound Dividends Total", Mesh -> None, 
  ColorFunction -> "Rainbow"]}



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.