定义用于从数据文件求解方程式的函数 [英] defining a fuction to be used in solving equation from a data file

查看:79
本文介绍了定义用于从数据文件求解方程式的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对python以及实际上是任何基本编程语言都是全新的,我使用Mathematica进行所有的符号和数值计算.我正在学习使用python并发现它真的很棒!这是我要解决的问题,但毫无头绪!例如,我有一个数据文件

I am completely new to python and in fact any fundamental programming language, I use Mathematica for my all my symbolic and numeric calculations. I am learning to work with python and finding it really awesome! Here is a problem I am trying to solve but stuck without a clue! I have a data file for example

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

仅{t,Cos [2t]}.我想从这些数据中定义一个函数,并将其用于解决python中的方程式.我的Mathematica直觉告诉我,我应该定义如下函数:

Which just the {t, Cos[2t]}. I want to define a function out of this data and use it in solving an equation in python. My Mathematica intuition tells me that I should define the function like:

    iFunc[x_] = Interpolation[iData, x]

,其余工作很容易.例如

and rest of the job is easy. for instance

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

轻松解决方程式.(尽管我没有尝试更复杂的情况).现在,如何在python中完成工作以及准确性对我来说是一个重要的问题.所以,现在我想问两个问题.

Solves the equation easily. (I have not tried with more complicated cases though). Now how to do the job in python and also accuracy is an important issue for me. So, now I would like to ask two questions.

1.这是Mathematica中最准确的方法吗?

2.在python中解决问题的更准确方法等效于什么?

这是我谦虚的尝试(使用StackOverflow的大量输入)来解决其中cos(2t)定义起作用的问题:

Here is my humble attempt to solve the problem (with a lot of input from StackOverflow) where the definition with cos(2t) works:

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)数据运行带有插值函数的代码时,无法正常工作...错误消息告诉我们


*When I run the code with interpolated function from cos(2t) data, is not working...the error message tell

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. 

我真的无法破译它们.请帮忙...

I really can't decipher them. Please help...

推荐答案

转至子问题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

you construct phinew not as callable function but as array of values, closing the circle array to interpolation function to array. Use f which is a scalar function directly in the derivatives function,

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

这篇关于定义用于从数据文件求解方程式的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆