# infinity recursion with in the program

10 ビュー (過去 30 日間)
Hailat Berhane Ghebremdhin 2023 年 1 月 29 日
コメント済み: John D'Errico 2023 年 1 月 30 日
I get this message while trying to generate a resule using the following function " out of memory. The likely cause is an infinite recursion within the program.
Error in newplot( line 53)
fig goobjects(0);"
f= @(x) 1-sin(x);
fixpointgraf(f,0,pi,-1,1)
testpoints = linspace(.45,.55)
punktStabil(f,testpoints)
fixIter(f,.5,100)
function stabil = punktStabil(f,testpoints)
h=1e-7;
Delta=(f(testpoints+(h))-f(testpoints))/(h);
stabil= false;
for i=1:1:length(Delta)-1
if abs(Delta(i))>1
stabil= true;
end
end
if punktStabil(f,Delta)
disp('punkten nära 1.6 är stabil för f')
else
disp('punkten nära 1.6 äe ej stabil för f1.')
end
end
function iter_lst = fixIter(f,x0,N)
iter_lst = zeros(N,1); % skapa en lista med 100 nollor vi kan fylla.
iter_lst(1) = x0;
for k=1:1:N
iter_lst(1i ) = f(iter_lst(1i-1));
end
figure()
scatter(1:1:N,iter_lst)
end

サインインしてコメントする。

### 採用された回答

John D'Errico 2023 年 1 月 29 日

What do you expect? Look carefully at your code. I've extracted the only lines that matter below.
function stabil = punktStabil(f,testpoints)
... (stuff)
if punktStabil(f,Delta)
... (stuff)
We see a function called punktStabil. Now, suppose you call this code as a function. One of the first things the code does is call the function punktStabil. And worse, there is no way for the code to terminate before it reaches the recursive call to punktStabil.
Now we should count how many times punktStabil gets called. That is,
punktStabil calls punktStabil which calls punktStabil which calls punktStabil which calls punktStabil...
The recursive calls never terminate. Your function calls itself infinitely many times. Eventually MATLAB runs out of memory.
Now, go back and read the message you got.
out of memory. The likely cause is an infinite recursion within the program.
##### 2 件のコメント表示非表示 1 件の古いコメント
John D'Errico 2023 年 1 月 30 日
f= @(x) 1-sin(x);
fixpointgraf(f,0,pi,-1,1)
testpoints = linspace(.45,.55);
punktStabil(f,testpoints)
Unrecognized function or variable 'Delta'.

Error in solution>punktStabil (line 14)
Delta(f(testpoints+(h))-f(testpoints))/(h);
fixIter(f,.5,100)
function fixpointgraf(f,a,b,c,d)
x= linspace(a,b);
plot(x,f(x))
hold on
plot(x,x )
end
function stabil = punktStabil(f,testpoints)
h=1e-7;
Delta(f(testpoints+(h))-f(testpoints))/(h);
for i=1:1:length(Delta)-1
if abs(Delta(i))>1
stabil= true;
end
end
if found
disp('punkten nära 1.6 är stabil för f')
else
disp('punkten nära 1.6 äe ej stabil för f1.')
end
end
function iter_lst = fixIter(f,x0,N)
iter_lst = zeros(N,1); % skapa en lista med 100 nollor vi kan fylla.
iter_lst(1) = x0;
for k=2:1:N
iter_lst(1i ) = f(iter_lst(1i-1));
end
figure()
scatter(1:1:N,iter_lst)
end
Sigh. Of course there is a problem. Delta is now undefined.

サインインしてコメントする。

### カテゴリ

Find more on Get Started with MATLAB in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by