Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

python scipy-fsolve doesn`t work. why fsolve return 'None'?

first, sorry about my poor English skill.

I make BDT-model, but something problem here.

this function is change code for column 4

def f(x):
    a=1 / (1 + f_tree.loc[1, 4] + x)
    b=1 / (1 + f_tree.loc[2, 4] + x)
    c=1 / (1 + f_tree.loc[3, 4] + x)
    d=1 / (1 + f_tree.loc[4, 4] + x)
    e=0.5*(a+b)/ (1 + f_tree.loc[1, 3])
    f=0.5*(b+c)/ (1 + f_tree.loc[2, 3])    
    g=0.5*(c+d)/ (1 + f_tree.loc[3, 3]) 
    h=0.5*(e+f)/ (1 + f_tree.loc[1, 2])
    i=0.5*(f+g)/ (1 + f_tree.loc[2, 2])  
    return -Market_data['PV'][3] + (0.5 * (h+i) / (1+f_tree.loc[1,1]))
x=fsolve(f,0)
x
f_tree.loc[1,4]+=x
f_tree.loc[2,4]+=x
f_tree.loc[3,4]+=x
f_tree.loc[4,4]+=x
f_tree

table before

1 2 3 4 5
1 0.05 0.065334 0.081673 0.067493 0.074591
2 0.00 0.055317 0.070603 0.055259 0.061070
3 0.00 0.000000 0.061539 0.045242 0.050000
4 0.00 0.000000 0.000000 0.037041 0.040937
5 0.00 0.000000 0.000000 0.000000 0.033516

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's how to read the error: focus on not iterable. Something in the line is attempting to iterate. The only part that makes sense for is sum(map(float, final)). So we know that final is None.

final is the return value of calculate. Looking at the code, calculate does indeed return nothing, rather than the expected iterable.

At one point, calculate states

if set_row == 3:
    return cal_temp

However, the corresponding else recursively calls calculate(loop_temp, set_row) and discards the return value. You probably want to keep the recursive return value and return it:

return calculate(loop_temp, set_row)

As usual with these types of problems, the error is entirely in your code, and not in the thoroughly tested and generally well thought-out libraries you are using.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...