Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Infinite loop
#1
The program is for finding approximate values of an integral using the trapezoid method, but it is in an infinite loop. I couldn't find the error.
intervalo = input("Entre com o  número de intervalos : ")
a = 1
b = 3
n = float (intervalo)
c = float (a)
soma = 0
# FUNÇÃO : 1/x^2
def f(x):
  return 1/(x)**2
h = math.fabs (b-a)/n
c += h
def tpz(f,a,b,c,n):
  while f(c) < b):
    soma = (f(c) + f(c + h))
    c += h
  soma *= 2
  soma = ((f(a) + f(b)) + soma) * h/2
d = tpz(f,a,b,c,n)
print("O resultado da integral é aproximadamente :",d)
Reply
#2
c increases so f© decreases. Either your loop never runs or runs forever. You are doing the trapezoid method wrong
Danado likes this post
Reply
#3
(Aug-15-2021, 08:13 PM)deanhystad Wrote: c increases so f© decreases. Either your loop never runs or runs forever. You are doing the trapezoid method wrong
Thanks, fix this part, now do I have to start the code from scratch or can I fix the parts that aren't right in the code above?
I tried to do it here without copying anything from the internet, but it's not working. I thought it would be easier not to use array.
Reply
#4
(Aug-15-2021, 08:13 PM)deanhystad Wrote: c increases so f© decreases. Either your loop never runs or runs forever. You are doing the trapezoid method wrong

I modified it, but it's still not working.

intervalo = input("Entre com o  número de intervalos : ")
a = 1
b = 3
# FUNÇÃO : 1/x^2
def f(x):
  return 1/((x)**2)
n = float (intervalo)
soma = 0
h = math.fabs (b-a)/n
c = a + h
print(": ", c)
while (c < b ):
    soma = (f(c) + f(c + h)) + soma
    c += h
soma = 2 * soma
soma = ((f(a) + f(b)) + soma) 
d = soma * (h/2)
print("O resultado da integral é aproximadamente :",d)
Yoriz write Aug-15-2021, 09:53 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#5
Your results are wrong because you are merging two different approaches to calculating the area of trapezoids.

The obvious way to calculate the integral of f(x) using trapezoids is calculate the area of each trapezoid and add them together.
delta = (end - start) / intervals
area = 0
for _ in range(intervals):
    area += (func(start) + func(start + delta)) / 2 * delta  # Average height * width
    start += delta
I can see you doing this in your code, but you are starting with the wrong value for c.
while (c < b ):
    soma = (f(c) + f(c + h)) + soma
    c += h
This is not efficient. You call f(x) nearly twice as many times as required. To see this, use a simple case with only 4 intervals: ab, bc, cd, de. Our area calculation would be:

area = ((f(a)+f(b))/2 + (f(b)+f©)/2 + (f©+f(d))/2 + (f(d)+f(e))/2) * delta

Notice that f(b), f© and f(d) are calculated twice. If there are many intervals the extra calculations might take a lot of time. We can improve efficiency by calculating f(x) only once for each value of x.

area = (f(a) + f(e) + 2 * (f(b) + f© + f(d))) / 2 * delta

I can see where you are doing this, but soma is already wrong for this method:
soma = 2 * soma
soma = ((f(a) + f(b)) + soma) 
d = soma * (h/2)
You have to decide which method you want to use. You cannot mix then together the way that you are.

I also think you should write an integrator function that takes arguments for the function you want to integrate, the start and end of the range, and the number of intervals. Here's a start.
def integrate(function, start, end, count):
    '''Compute integral of function over the range start..end.  Uses Trapezoid method, dividing range into count trapezoids'''
    area = 0
    # Body of integrator code
    return area

print(integrate(lambda x: 1/x**2, 1, 3, 100))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with while loop creating an infinite loop. FWendeburg 3 3,044 Jan-30-2019, 08:28 PM
Last Post: FWendeburg
  Infinite loop/ only half working anclark686 5 4,764 Sep-09-2018, 07:31 AM
Last Post: buran
  Why is this giving me an infinite loop? wlsa 4 3,921 Jul-25-2018, 10:11 PM
Last Post: cyberpatje
  Another infinite loop wlsa 7 4,684 Jul-20-2018, 12:04 AM
Last Post: wlsa
  How to stop an infinite loop in spyder if it's currently running? wlsa 3 24,735 Jun-30-2018, 03:27 AM
Last Post: ichabod801
  Infinite loop Truman 9 9,136 Jan-19-2018, 11:25 PM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020