infinity recursion with in the program

조회 수: 5 (최근 30일)
Hailat Berhane Ghebremdhin
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
John D'Errico 2023년 1월 29일
편집: 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
Hailat Berhane Ghebremdhin
Hailat Berhane Ghebremdhin 2023년 1월 29일
even though I try to take fix fixpunkt function like the following, I still get the same problem:
f= @(x) 1-sin(x);
fixpointgraf(f,0,pi,-1,1)
testpoints = linspace(.45,.55);
punktStabil(f,testpoints)
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
John D'Errico
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Passivity and Sector Bounds에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by