Home > database >  Using loops to calculate the values of an equation with 2 parameters
Using loops to calculate the values of an equation with 2 parameters

Time:01-07

I have this equation v = (a * (e((b) - 1000))/400)

b is a list of values = 1300, 2100, 2900, 3200, 3800, 4000, 4100, 4200, 4900, 5600

a is all values in the range(10)

e is exponential

for the value a= 0, the value of b=1300 is incorporated in the equation.

For the value a=1 the value b = 2100 is incorporated and so on.

However, my code is just producing invalid numbers. I'm not sure how to make sure the equation loops through the correct values of b with the corresponding a values. Any help would be appreciated. This is my code:

import numpy as np
import math
def v(a, b):
    v = ((a) * (np.exp(b) - 1000) / 400)
    b = [1300, 2100, 2900, 3200, 3800, 4000, 4100, 4200, 4900, 5600]
    for a in range(10):
        return ((a) * (np.exp(b) - 1000) / 400)

Tried creating a function and for loop to loop through values of a in range(10). But do not know how to incorporate corresponding b values.

CodePudding user response:

You can define a as a numpy array and use NumPy's built-in vector maths, e.g.,

import numpy as np
import math
def v():
    a = np.arange(10)  # an array containing 0, 1, 2, ..., 9 
    b = [1300, 2100, 2900, 3200, 3800, 4000, 4100, 4200, 4900, 5600]
    return a * np.exp(b - 1000) / 400

This will return a 1D NumPy array with 10 values. Or you can pass the length of a and the b values into the function, e.g.,:

n = 10
b = [1300, 2100, 2900, 3200, 3800, 4000, 4100, 4200, 4900, 5600]

def v(n, b):
    a = np.arange(n)  # an array containing 0, 1, 2, ..., 9 
    
    return a * np.exp(b - 1000) / 400

# call this with, e.g.,
x = v(n, b)

Note: many of your values of b will return inf when doing e**(b-1000) as they are large!

CodePudding user response:

It looks like you are trying to use the values of a and b from the input parameters in your equation, but you are also re-assigning the value of b to a fixed list of values within the function. This means that the input value of b is being overwritten and is not being used in the equation.

To fix this, you can remove the line b = [1300, 2100, 2900, 3200, 3800, 4000, 4100, 4200, 4900, 5600] from your function. This will allow you to use the input value of b in the equation.

You can then use a loop to iterate over the range of values for a, and use the current value of a to calculate the value of v for each value of b in the list. Here's how you could do this:

import numpy as np
import math

def v(a, b_values):
    results = []
    for a in range(10):
        for b in b_values:
            result = ((a) * (np.exp(b) - 1000) / 400)
            results.append(result)
    return results

b_values = [1300, 2100, 2900, 3200, 3800, 4000, 4100, 4200, 4900, 5600]
v_values = v(10, b_values)
print(v_values)

This code defines a function v that takes two parameters: a and a list of values for b. It then uses nested loops to iterate over the range of values for a and the values in the list for b, and calculates the value of v for each combination of a and b. The results are stored in a list, which is returned by the function.

  • Related