infinity recursion with in the program
3 次查看(过去 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
0 个评论
采纳的回答
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 个评论
John D'Errico
2023-1-30
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
Sigh. Of course there is a problem. Delta is now undefined.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Stability Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!