MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   MatLab递归错误(初学者) (https://www.labfans.com/bbs/showthread.php?t=22689)

poster 2019-12-10 16:49

MatLab递归错误(初学者)
 
好。所以我在MatLab中有两个互相调用的函数。

黎曼

function I = Riemann(f, dx, a, b) x = a:dx:b; fx = f(x).*dx; I = sum(fx); 和myfunc.m

function f = myfunc(x) f = sin(1./x); for n=1:100 I = Riemann(@myfunc, 0.001, 1/n, 1); end plot(I) 问题是要使其运行。我如何调用myfunc来获得任何收益。我尝试过的所有内容最终都陷入了无尽的递归调用堆栈中(这很有意义)。


回答:
您的问题在于函数的定义:要能够使用递归定义,您[B]必须[/B]至少能够计算两个函数中的一个而不包含另一个,至少对于某些值。您还[B]必须[/B]确保每个计算最终将依靠这些结果可以获取不递归。

对于您的特定问题,我觉得您想集成函数f(x)= sin(1./x)。如果是这样,第二个函数的代码应显示为:

function f = myfunc(x) fct = @(x) sin(1./x); f = fct(x); for n=1:100 I = Riemann(fct, 0.001, 1/n, 1); end plot(I)

[url=https://stackoverflow.com/questions/938806]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 23:24

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.