0

I have the following formula:

y = a1cos(θ1) +a2cos(θ1+θ2) +...+ancos(θ1+...+θn)

The goal is to implement it in Python so it works with any number of a-value/theta angles combination.

So far I have written code which stores in two separate dictionaries:

  1. A combination of a-value number and a value
  2. A combination of a-value number and an angle value

This is implemented via two for loops where i is the number of values the user wants the formula to work with. Here is the code:

a_values_list = {}
a_number = int(input("\nHow many values of a would you like to enter?"))


for i in range(0, a_number):
    key = input("Input a-value number")
    value = input("Input corresponding value of a")
    a_values_list[key] = value


angles_list = {}

for i in range(0,a_number):
    key = input('Input a-value number')
    value = input("\n Enter a corresponding angle displacement between 0 and pi")
    angles_list[key] = value
 

I need help with the following:

  1. Figuring out how to implement the formula based on the number of a-values/angle combinations stored in the dictionaries
  2. Getting the angle values from the dictionaries and adding them within the cos statements

Thank you for your time

  • If they are pairs, then why don't you load them in pairs? Then you wouldn't need to ask for a "key". A dict is not a good choice, just store the numbers in two lists. `a = []` / `theta = []` / `for i in range(number):` / `....a.append(int(input('Enter a value:')))` / `....theta.append(float(input('Enter theta:')))` – Tim Roberts Mar 02 '21 at 03:19
  • This seems to be more elegant solution to the first part of the problem, but I am still having difficulty implementing the formula. How do I use these values within the formula? – Alienspecimen Mar 02 '21 at 03:57
  • Simple loop `y = 0` / `for i in range(len(a)):` / `y += a[i] * sum(theta[:i+1])` – Tim Roberts Mar 02 '21 at 04:46
  • When you get more sophisticated, you could use a one-liner: `y = sum(a[i] * sum(theta[:i+1]) for i in range(len(a))`. – Tim Roberts Mar 02 '21 at 04:50

0 Answers0