infinity recursion with in the program

5 views (last 30 days)
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

Accepted Answer

John D'Errico
John D'Errico on 29 Jan 2023
Edited: John D'Errico on 29 Jan 2023
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 Comments
Hailat Berhane Ghebremdhin
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 on 30 Jan 2023
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.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!