2

I'd like to declare first of all, that I'm a mathematician. This might be a stupid stupid question; but I've gone through all the matlab tutorials--they've gotten me nowhere. I imagine I could code this in C (it'd be exhausting); but I need matlab for this particular function. And I don't get exactly how to do it.

Here is the pasted Matlab code of where I'm running into trouble:

function y = TAU(z,n)
   y=0;
   for i =[1,n]
       y(z) = log(beta(z+1,i) + y(z+1)) - beta(z,i);
   end
end

(beta is an arbitrary "float" to "float" function with an index i.)

I'm having trouble declaring y as a function, in which we call the function at a different argument. I want to define y_n(z) with something something y_{n-1}(z+1). This is all done in a recursive process to create the function. I really feel like I'm missing something stupid.

As a default function it assigns y to be an array (or whatever you call the default index assignment). But I don't want an array. I want y to be assigned as a "function" class (i.e. takes "float" to "float"). And then I'm defining a sequence of y_n : "float" to "float". So that z to z+1 is a map on "float" to "float".

I don't know if I'm asking too much of matlab...

Help a poor mathematician who hasn't coded since the glory days of X-box mods.

...Please don't tell me I have to go back to Pari-GP/C drawing boards over something so stupid.

Please help!

EDIT: At rahnema1 & mimocha's request, I'll describe the math, and of what I am trying to do with my program. I can't see how to implement latex in here. So I'll write the latex code in a generator and upload a picture. I'm not so sure if there even is a work around to what I want to do.

enter image description here

As to the expected output. We'd want,

beta(z+1,i) + TAU(z+1,i) = exp(beta(z,i) + TAU(z,i+1))

And we want to grow i to a fixed value n. Again, I haven't programmed in forever, so I apologize if I'm speaking a little nonsensically.

EDIT2:

So, as @rahnema1 suggests; I should produce a reproducible example. In order to do this, I'll write the code for my beta function. It's surprisingly simple. This is for the case where the "multiplier" variable is set to log(2); but you don't need to worry about any of that.

function f = beta(z,n)
    f=0;
    for i = 0:n-1
        f = exp(f)/(1+exp(log(2)*(n-i-z)));
    end
end

This will work fine for z a float no greater than 4. Once you make z larger it'll start to overflow. So for example, if you put in,

beta(2,100)

 1.4242

beta(3,100)

 3.3235

beta(3,100) - exp(beta(2,100))/(1/4+1)

0

The significance of the 100, is simply how many iterations we perform; it converges fast so even setting this to 15 or so will still produce the same numerical accuracy. Now, the expected output I want for TAU is pretty straight forward,

TAU(z,1) = log(beta(z+1,1)) - beta(z,1)
TAU(z,2) = log(beta(z+1,2) + TAU(z+1,1)) - beta(z,2)
TAU(z,3) = log(beta(z+1,3) + TAU(z+1,2)) - beta(z,3)
...
TAU(z,n) = log(beta(z+1,n) + TAU(z+1,n-1)) -beta(z,n)

I hope this helps. I feel like there should be an easy way to program this sequence, and I must be missing something obvious; but maybe it's just not possible in Matlab.

At mimocha's suggestion, I'll look into tail-end recursion. I hope to god I don't have to go back to Pari-gp; but it looks like I may have to. Not looking forward to doing a deep dive on that language, lol.

Thanks, again!

Richard Diagram
  • 209
  • 1
  • 7
  • 4
    It would be helpful if you provide the mathematical formula in addition to an example input and the expected output. – rahnema1 Apr 18 '21 at 08:15
  • 1
    I feel like I almost get what you are trying to achieve, but the phrasings confuse me, so please elaborate (add the equation if possible). The issue I see right now is you are trying to get `y(z+1)`, but it isn't defined. You'll need to define an end-point `y(N)` then work your way backwards `y(N-1), y(N-2), ... y(z)`, recursive function or otherwise. You also seem to be implying you want to *overload* your function (have multiple `TAU` funcs) but I think that is unnecessary, and it is making the task much harder than it needs to be. – mimocha Apr 18 '21 at 17:50
  • 1
    @mimocha That's exactly my problem. In many ways, this is the inverse iteration. It's pretty difficult to add the mathematical formula but I'll edit in what I'm trying to do. This is a program to calculate an analytic Tetration function. Beware the math, it's very high-brow. – Richard Diagram Apr 19 '21 at 23:35
  • 2
    Just a note, it’s not going to help you with your problem, but you should know. `for i =[1,n]` repeats the loop body exactly twice, with `i` having values 1 and `n`. If you want to run through all integers between the two (inclusive) you need to do `for i = 1:n`. – Cris Luengo Apr 20 '21 at 00:40
  • Hey, thanks Cris! I'm still working on syntax in Matlab. The only syntax I really remember is C syntax. Thanks a bunch! – Richard Diagram Apr 20 '21 at 01:27
  • 1
    That is some high-brow math indeed. The folks over in the math stackexchange should be better suited for this task. But if you're willing to bear with us programmers and dumb the problem down some more, I think we'd be able to work something out :^) From what I've gathered, your function needs very large `i` to approximate `tau`; so you'd need tail-recursive functions at the very least. Unfortunately, [MATLAB doesn't seem to support tail-recursion](https://stackoverflow.com/q/5326749/5675094), so I think you'd have to make this an iterative function if you want to use MATLAB. – mimocha Apr 20 '21 at 05:06
  • 2
    You haven't programmed in forever and I am a beginner in math. Really no need to apologize. This site recommends, when asking questions, to provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). You may need to consider us as very beginner/baby in math and forget your actual problem and provide a simple function and say if the input to the function be for example 3 the output would be a sequence of numbers 6 9 45 68 or it would be a function that produce the sequence. As we are baby don't forget to provide an example. Help us to help you. – rahnema1 Apr 20 '21 at 18:48
  • Sure, no problem; I'll add more details. I can definitely produce working code of 1 or 2 iterations; the trouble is once I get very large numbers. I'll read up on tail recursion, and see if that fits the idea of what I'm looking for. – Richard Diagram Apr 21 '21 at 00:52

1 Answers1

2

Is this what you are looking for?

function out = tau(z,n)
    % Ends recursion when n == 1
    if n == 1
       out = log(beta(z+1,1)) - beta(z,1);
       return
    end

    out = log(beta(z+1,n) + tau(z+1,n-1)) - beta(z,n);
end

function f = beta(z,n)
    f = 0;
    for i = 0:n-1
        f = exp(f) / (1 + exp(log(2)*(n-i-z)));
    end
end

This is basically your code from the most recent edit, but I've added a simple catch in the tau function. I tried running your code and noticed that n gets decremented infinitely (no exit condition).

With the modification, the code runs successfully on my laptop for smaller integer values of n, where 1e5 > n >= 1; and for floating values of z, real and complex. So the code will unfortunately break for floating values of n, since I don't know what values to return for, say, tau(1,0) or tau(1,0.9). This should easily be fixable if you know the math though.

However, many of the values I get are NaNs or Infs. So I'm not sure if your original problem was Out of memory error (infinite recursion), or values blowing up to infinity / NaN (numerical stability issue).

Here is a quick 100x100 grid calculation I made with this code.

Then I tested on negative values of z, and found the imaginary part of the output to looks kinda cool.

Not to mention I'm slightly geeking out over the fact that pi is showing up in the imaginary part as well :)

tau(-0.3,2) == -1.45179335740446147085 +3.14159265358979311600i

mimocha
  • 1,041
  • 8
  • 18
  • 1
    YES! That's exactly what I was looking for! Yes, the NAN is expected. Again,the beta function grows like an iterated exponential, overflow errors are to be expected--think of it growing like 1, e, e^e, e^e^e, e^e^e^e,... The function tau does not accept complex arguments in n, only natural numbers (it's a sequence of complex functions). The pi your seeing is because we're taking a log of a negative number here. Presumably matlab uses the principal branch. I'm mostly just interested in seeing if I can use this tau function to approximate the tau function I really want. This looks great, THANKS! – Richard Diagram Apr 22 '21 at 19:42
  • 1
    Just saying thanks again! I massaged it and I got it to produce the graphs I was looking for! I just had to shift the index (I started at log(beta(z+1,2)) - beta(z,2); as its more conducive to the iteration). You have no idea how frustrated I've been about trying to find a way to graph these functions, lol! Thanks again, I wish I could give you more rep! – Richard Diagram Apr 22 '21 at 20:15
  • Hey, I collected all the code for my paper into a GitHub page: https://github.com/JmsNxn92/Recursive_Tetration If you'd like to be referenced, just let me know. – Richard Diagram Apr 27 '21 at 05:23
  • @RichardDiagram Hey, thanks for asking, but I'm totally fine not being referenced. I'm happy just helping out random strangers on the internet :) – mimocha May 01 '21 at 11:25
  • That's what I figured, just covering my bases :) – Richard Diagram May 02 '21 at 00:23