Infinite loop - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Infinite loop (/thread-34630.html) |
Infinite loop - Danado - Aug-15-2021 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) RE: Infinite loop - deanhystad - Aug-15-2021 c increases so f© decreases. Either your loop never runs or runs forever. You are doing the trapezoid method wrong RE: Infinite loop - Danado - Aug-15-2021 (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 wrongThanks, 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. RE: Infinite loop - Danado - Aug-15-2021 (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) RE: Infinite loop - deanhystad - Aug-16-2021 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 += deltaI 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 += hThis 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)) |