For loop in python

1

Good morning,

The value of x is a decrease of 1.0 - 0.01 asi, the first value of x=0.99 the second x=0.98 and the third x=0.97

The first value is x1=1.0 The problem is the update of x1 and that the loop uses the new value x=0.98

My code is as follows:

import scipy.optimize
from scipy import optimize
import numpy as np

n=763.0
s=762.0
y=0.5673
b=0.0026

x=[0.99,0.98,0.97]
x1=1.0
i=1

for iteration in range(1,3):
    def l(x, x1, b,n,y,s):
        def p(x):
            return 1.0/(x*(b*n+y*np.log(x)-s*b*x))
        ds = scipy.integrate.fixed_quad(p, x, x1, args=(), n=5)
        return (ds[0]-1)
    def t(x, x1, b, n, y, s):
        x1=scipy.optimize.brentq(l, x, x1, args=(b, n, y, s))
        return x1
    x=x1
    i=i+1
    print x

The output is wrong and it is:

1.0
1.0

I appreciate the help.

    
asked by Santiago Yepes 13.03.2017 в 19:19
source

1 answer

1

You have entered the definition of your functions within the for. What I think happens is that simply after the definitions, the x takes the value of x1 (1.0) and repeats it twice.

Then I also do not know why you increase the value of i inside the for loop, because then you do not use it anywhere.

Try in principle to define the functions outside the loop and call them inside, and if they do what they have to do, it will work for you.

    
answered by 13.03.2017 / 19:40
source