Filter löschen
Filter löschen

infinity recursion with in the program

4 Ansichten (letzte 30 Tage)
Hailat Berhane Ghebremdhin
Hailat Berhane Ghebremdhin am 29 Jan. 2023
Kommentiert: John D'Errico am 30 Jan. 2023
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

Akzeptierte Antwort

John D'Errico
John D'Errico am 29 Jan. 2023
Bearbeitet: John D'Errico am 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 Kommentare
Hailat Berhane Ghebremdhin
Hailat Berhane Ghebremdhin am 29 Jan. 2023
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 am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Passivity and Sector Bounds finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by