I need some help with this progrma as it is a infinite loop and i don´t know how to stop it.
llucia
on 24 Mar 2023
Latest activity Edit by Christopher Stapels
on 28 Mar 2023
%This is the code. I have to introduce a small value, for example, e = 0.01
%and the loop has to stop when de difference in absolue value between a1
%and pi is less than that quantity. But it never stops and i dont know why.
%Hope you can help me. I appreciate it.
function [a1,a2,i,j] = ejercicio06(e)
i=2;
j=2;
p(1) = e+1;
s(1) = e+1;
while p(i-1) > e
a1 = (1./((4.*i+1).*(4.*i+3)));
a1=8*sum(a1);
p(i) = abs(a1-pi);
i = i+1;
end
disp('hola este bucle se ha terminado')
while s(j-1) > e
a2 = (1./(1+j).^2);
a2 = sqrt(6*sum(a2));
s(j) = abs(a2-pi);
j = j+1;
end
end
1 Comment
Time DescendingAre you running this in ThingSpeak?
I would try removing the semicolons to inspect the output of your calculations. P(i) seems to be getting smaller as i increases, so p(i-1) will alwyas be greater than e, thus the infinite loop.
i=100;
a1 = (1./((4.*i+1).*(4.*i+3)))
a1 =
6.1880e-06
i=1000;
a1 = (1./((4.*i+1).*(4.*i+3)))
a1 =
6.2438e-08
Sign in to participate