0

I have two variables (n;p) in two independent equations, one to find C and the other to find T. For the first iteration I assume the values of n and p finding C and T, now I have to find all the pairs of n and p that will make C=T. Additionally to being used as a constant, n will define the range for a list in the first equation and C is the sum of all the values on that list.

I was able to program the first iteration defining n and p as constants, thus finding C and T which were not equal to each other, but I don't know how to program the whole process so that it will repeat for a fix set of p (that will be a list and not a constant) and finding the n's that will make C=T. I have to iterate through p and for each one find one n that will satisfy the condition. Therefore I need to make a for loop inside a for loop inside a for loop. It will be something like this:

for all the values of p in range (0, 12, 0.1) do:
     for all the values of n in range(0, 160, 0.001) do:
             "the rest of the operation that also has for loop in it"

that operation will result in C and T, then,

if C = T
print(p and n)  # the pair (p;n) that made it possible

As you can see that's an idea, not actual code, I don't know how to write the actual code for it. I saw something about a zip function but still don't get it. Help.

greenrabbit
  • 99
  • 1
  • 10
  • 1
    It's hard to help you because the problem description is too abstract, but I think you need to follow a tutorial and make sure you understand the fundamentals. – Karl Knechtel Aug 30 '21 at 06:53

1 Answers1

0

I figure it out by trying to enunciate the question (and making some research):

p = np.arange(0.1, 12.1, 0.1)  
for k in range(0, p.size):
# this will evaluate the next "for loop" (iterate with k) for all the values of p
    n = np.arange(0, 160, 0.002)
    for j in range(0, n.size):
    # this will evaluate the next "for loop" (iterate with j) for all the values of n  
        # the next for loop uses "i" to iterate other operations not relevant for 
        # this problem right now

Inside of the "for loop" that uses "j", at the end, I needed to compare the results of C and T and find if they were equal (or as equal as possible), and then print the n and p that made this posible.

I still need to work this part of the code but a simple way to compare it could be:

        comparison_parameter = 1
        comparison = T - C
        if comparison_parameter > comparison > -comparison_parameter:
            print('For p = ', round(p[k], 1), ', n =', round(n[j], 4))
        else:
            pass

This is definitely not the most elegant way to do all this, here are some problems with this code that I would recommend to improve:

  • I'm using lists and not generator, therefore python will store every calculation in memory

  • I'm not using "assertAlmostEqual() function" to compare C and T (https://www.geeksforgeeks.org/python-unittest-assertalmostequal-function/)

  • My way gives me multiple values of n (given a certain p) that satisfy the condition for a particular comparison_parameter, but I only want the one correspondant to the comparison being closest to 0, meaning that C and T will be closer to each other.

greenrabbit
  • 99
  • 1
  • 10
  • 1
    You can just do `for p in np.arrange(...)` instead of using `p[k]`. For the inner part, try `if abs(T - C) < 1`, and the `else: pass` does not do anything. – tobias_k Aug 30 '21 at 07:12