2

我對Python和實際上任何基本的編程語言都是全新的,我將Mathematica用於我所有的符號和數字計算。我正在學習使用python並發現它非常棒!這是我正在努力解決的一個問題,但沒有任何線索而陷入困境! 我有一個數據文件例如定義一個函數用於從數據文件求解方程

0.  1.  
0.01 0.9998000066665778  
0.02 0.9992001066609779  
...  .. 

哪些只是{噸,COS [2噸]}。 我想從這個數據中定義一個函數,並用它來求解python中的方程。我的Mathematica直覺告訴我,我應該定義如下功能:

iFunc[x_] = Interpolation[iData, x] 

其餘的工作很容易。例如

NDSolve[{y''[x] + iFunc[x] y[x] == 0, y[0] == 1, y[1] == 0}, y, {x, 0, 1}] 

很容易求解方程。 (雖然我沒有嘗試過更復雜的情況)。 現在如何在Python中完成這項工作,而且準確性對我來說也是一個重要問題。所以,現在我想問兩個問題。

1.這是Mathematica中最準確的方法嗎?

2.什麼是更準確的方式來做這個問題在python中?

這是我要解決的問題(從StackOverflow上大量的投入),謙虛的嘗試,其中有COS(2T)定義工作:

from scipy.integrate import odeint 
import numpy as np 
import matplotlib.pyplot as plt 
from math import cos 
from scipy import interpolate 

data = np.genfromtxt('cos2t.dat') 

T = data[:,0] #first column 
phi = data[:,1] #second column 

f = interpolate.interp1d(T, phi) 

tmin = 0.0# There should be a better way to define from the data 
dt = 0.01 
tmax = 2*np.pi 
t = np.arange(tmin, tmax, dt) 

phinew = f(t) # use interpolation function returned by `interp1d` 

""" 
def fun(z, t): 
    x, y = z 
    return np.array([y, -(cos(2*t))*x ]) 
"""  
def fun(z, t): 
    x, y = z 
    return np.array([y, -(phinew(t))*x ])  

sol1 = odeint(fun, [1, 0], t)[..., 0] 

# for checking the plots   
plt.plot(t, sol1, label='sol') 
plt.show() 

*當我運行的代碼從cos(2t)數據插值函數,不工作...錯誤消息告訴

Traceback (most recent call last): File "testde.py", line 30, 
    in <module> sol1 = odeint(fun, [1, 0], t)[..., 0] 
File "/home/archimedes/anaconda3/lib/python3.6/site-packages/scip‌​y/integrate/odepack.‌​py", 
    line 215, in odeint ixpr, mxstep, mxhnil, mxordn, mxords) 
File "testde.py", 
    line 28, in fun return np.array([y, -(phinew(t))*x ]) 
TypeError: 'numpy.ndarray' object is not callable. 

我真的不能解密它們。請幫助...

+0

你的問題到底是什麼? – Wrzlprmft

+0

帶插值的代碼不起作用...錯誤消息告訴 回溯(最近呼叫最後): 文件「testde。py「,第30行,在 sol1 = odeint(fun,[1,0],t)[...,0] 文件」/home/archimedes/anaconda3/lib/python3.6/site-packages/文件「testde.py」,第28行,在樂趣中 返回np.array([y, - (phinew) (t))* x]) TypeError:'numpy.ndarray'對象無法調用 我真的無法破譯它們 – Archimedes

+0

請在您的問題中添加其他信息,還有更好的格式。對於錯誤消息,請隨時編輯您的文本。 – LutzL

回答

0

子問題2

隨着

t = np.arange(tmin, tmax, dt) 

phinew = f(t) # use interpolation function returned by `interp1d` 

相當於

phinew = np.array([ f(s) for s in t]) 

您構建phinew不是可調用的功能,但作爲值的數組,關閉圓陣列插值函數到數組。使用f它直接是一個標量函數在衍生物功能,

def fun(z, t): 
    x, y = z 
    return np.array([y, -f(t)*x ])  
+0

我修改了代碼,正如您所建議的那樣。現在顯示以下錯誤 Fil e「testde.py」,第39行,在 sol1 = odeint(fun,[1,0],t)[...,0] 文件「/usr/lib/python2.7/dist-packages/文件「testde.py」,第37行,在樂趣 返回np.array([y,-f(x,y) t)* x]) 文件「/usr/lib/python2.7/dist-packages/scipy/interpolate/polyint.py」,第80行,在__call__中 y = self._evaluate(x) – Archimedes

+0

文件「/ usr /lib/python2.7/dist-packages/scipy/interpolate/interpolate.py「,第586行,在_evaluate below_bounds,above_bounds = self._check_bounds(x_new) 文件」/usr/lib/python2.7/dist- packages/scipy/interpolate/interpolate.py「,第618行,在_check_bounds中 raise ValueError(「x_new中的值高於插值」 ValueError:x_new中的值高於插值範圍。 – Archimedes

+0

發生這種情況是因爲odeint resp。 lsoda使用自適應步長,內部步驟可以離開積分區間的界限。使'cos2t.dat'的採樣點間隔變大(或者積分間隔減小相同量),即使是在模擬的'T = np.arange(tmin,tmax + dt,dt)中的一點。 phi = np.cos(2 * T)',它的工作原理沒有錯誤。 – LutzL

1

在數學,通常的方式是簡單地

iFunc = Interpolation[iData] 

Interpolation[iData]已經返回的功能。

+0

任何方式來檢查準確性?我能想到的只是繪製插值曲線的數據。 – Archimedes

+0

我不明白這是什麼意思「檢查準確性」。插值將通過所有數據點。也許你想要擬合而不是內插? – Szabolcs

+0

我的意思是說,'通常的方式'也是Mathematica中最好的方法嗎?我試圖看看Wolfram文檔,它幾乎描述了一切。但對我來說,這似乎是一個複雜問題的簡單解決方案,或者這是Mathematica的方式! – Archimedes