简体   繁体   中英

Trying to create a list with variable and calculation in python in a loop to pass it to a binary search

I am trying to create a list with a variable in python within a loop so that I can calculate the value of that variable through a binary search later. This is quite similar to how an IRR is calculated. Here is what I have so far but I am unable to get it to work. Can the team help me here.

term=20

enter code here

  y = Symbol('y')

  for i in range (1,term+1):       
      eqn+=(1/((1+y/2)**(30/360*term)))
      data.append([eqn])

I want to use this eqn into a binary search function to evaluate the value of y

enter code here

def binary_search (100,eqn)
    upperbound=10
    lowerbound=0
    y = (lower + upper)/2
    while upper<lower:
      if eqn< 100:
         upper = y
         y = (lower + upper)/2
      elif eqn> 100:
        lower = y
        y= (lower + upper)/2 
     print(y)

Any help on this will be really appreciated since I am stuck even on the step 1 where I need to define a looped list with a variable itself.

from sympy import *
if __name__ == '__main__':
    x = symbols('x')
    gfg_exp = - 0
    for i in range(1,4):
        gfg_exp = gfg_exp + 1/(1 + i * x)

    print("Before Integration : {}".format(gfg_exp))

    # Use sympy.integrate() method
    intr = solve(gfg_exp, x)
    print("After Integration : {}".format(float(intr[0])))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM