本文介绍了在Matlab中声明函数递归序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想首先声明,我是一名数学家.这可能是一个愚蠢的愚蠢问题.但是我已经遍历了所有的Matlab教程-他们无处可寻.我想我可以用C编写代码(这很累);但我需要使用matlab来实现此特定功能.而且我不知道该怎么做.

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.

这是我遇到麻烦的地方粘贴的Matlab代码:

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是带有索引i的"float"到"float"函数的任意值)

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

我无法将 y 声明为函数,在该函数中我们以不同的参数调用该函数.我想用一些y_ {n-1}(z + 1)定义y_n(z).所有这些都是在递归过程中完成的,以创建函数.我真的感觉自己想念一些愚蠢的东西.

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.

作为默认函数,它将y分配为数组(或任何您称为默认索引赋值).但是我不想要一个数组.我希望将y分配为功能".类(即浮动"到浮动").然后我定义了y_n的序列:到浮动".因此,z到z + 1是"float"图上的映射.到浮动".

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".

我不知道我是否对Matlab要求过多...

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

帮助一个自 X-box mods 的辉煌时代以来就没有编码过的可怜的数学家.

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

...请不要告诉我,我不得不因为如此愚蠢的事情而回到Pari-GP/C绘图板上.

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

请帮助!

在rahnema1&mimocha的要求,我将描述数学以及我想用我的程序做的事情.我在这里看不到如何实现乳胶.因此,我将在生成器中编写乳胶代码并上传图片.我不确定是否可以解决我想做的事情.

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.

关于预期输出.我们想要,

As to the expected output. We'd want,

我们想要将i增大为固定值n.再说一次,我还没有永远编程,所以如果我说的有些荒谬,我深表歉意.

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.

所以,正如@ rahnema1所建议的那样;我应该举一个可重复的例子.为了做到这一点,我将为我的beta函数编写代码.非常简单.这是针对乘数"为0的情况.变量设置为log(2);但您不必为此担心.

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

这对于z浮点数不大于4的工作正常.一旦将z变大,它将开始溢出.例如,如果您输入

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

100的意义就是我们执行了多少次迭代.它收敛很快,因此即使将其设置为15左右,仍将产生相同的数值精度.现在,我想要的TAU预期输出非常简单,

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)

我希望这会有所帮助.我觉得应该有一种简单的方法来编程这个序列,而且我一定遗漏了一些明显的东西;但是也许在Matlab中是不可能的.

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.

在mimocha的建议下,我将研究尾端递归.我希望天哪,我不必回到Pari-gp;但看起来我可能必须这样做.不希望深入探讨该语言,大声笑.

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.

再次感谢!

推荐答案

这是您要寻找的吗?

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

这基本上是您最近一次编辑中的代码,但是我在 tau 函数中添加了一个简单的功能.我尝试运行您的代码,发现 n 无限减小(无退出条件).

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).

通过修改,代码可以在我的笔记本电脑上成功运行 n 的较小整数值,其中 1e5>n> = 1 ;对于 z 的浮点数,实数和复数.因此,不幸的是,由于 n 的浮动值,该代码将中断,因为我不知道要返回什么值,例如 tau(1,0)tau(1,0.9).如果您知道数学的话,这应该很容易解决.

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.

但是,我得到的许多值都是 NaN s或 Inf s.因此,我不确定您的原始问题是否是内存不足错误(无限递归)或值爆炸到无穷大/NaN(数值稳定性问题).

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).

这是我使用此代码进行的100x100快速网格计算.

然后我对 z 的负值进行了测试,发现了虚部输出看起来很酷.

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

更不用说我对pi也出现在虚部这一事实有些不高兴了:)

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

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

这篇关于在Matlab中声明函数递归序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 00:23
查看更多