简体   繁体   中英

Python fsolve via data interpolation with array values

I create interpolation from data set. Then, I'd like to find values corresponding to the interpolation. I can get one value problem, but not multiple values problem, like array or matrix. It only gives me ans for the first value (8), not for all (8, 4 and 2)

#Find time becoming 80% of CA0
import numpy as np
t = np.array([0,20,40,60,120,180,300])
CA = np.array([10,8,6,5,3,2,1])

#data to interpoldation function
from scipy.interpolate import interpld
g = interpld(t, CA)

#plotting function
import matplotlib.pyplot as plt
data=g(t)
plt.plot(t, data, 'b-')
plt.show()

#this only give me an ans for the first value, 8, not for other 4 and 2?
yvalue=np.array([8,4,2])
def func(x):
    return yvalue-g(x)

initial_guess = 2
ans, = fsolve(func, initial_guess)
print(ans)

Then, it only give me an answer only for the 1st value, not all. Anyone?

The problem is that you are only providing a scalar 2 for your initial guess. When I set initial guess to an array of values, then fsolve works as expected. Specifically when I set

initial_guess = 2 * np.ones(3)

then the ans I get is

np.array([  20.   90.  180.])

which at a glance seems correct.

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