本文介绍了工作了一个方程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解决一个微分方程数值,并在写一个公式,这将使我的解决方案,每个时间点的数组。

I'm trying to solve a differential equation numerically, and am writing an equation that will give me an array of the solution to each time point.

import numpy as np
import matplotlib.pylab as plt

pi=np.pi
sin=np.sin
cos=np.cos
sqrt=np.sqrt
alpha=pi/4
g=9.80665
y0=0.0
theta0=0.0

sina = sin(alpha)**2
second_term = g*sin(alpha)*cos(alpha)

x0 = float(raw_input('What is the initial x in meters?'))
x_vel0 = float(raw_input('What is the initial velocity in the x direction in m/s?'))
y_vel0 = float(raw_input('what is the initial velocity in the y direction in m/s?'))
t_f = int(raw_input('What is the maximum time in seconds?'))

r0 = x0
vtan = sqrt(x_vel0**2+y_vel0**2)
dt = 1000
n = range(0,t_f)
r_n = r0*(n*dt)
r_nm1 = r0((n-1)*dt)
F_r = ((vtan**2)/r_n)*sina-second_term
r_np1 = 2*r_n - r_nm1 + dt**2 * F_r
data = [r0]

for time in n:
    data.append(float(r_np1))
print data

我不知道如何使方程求解r_np1在在N各一次。我还是新来的Python,并希望一些帮助了解如何做这样的事情。

I'm not sure how to make the equation solve for r_np1 at each time in the range n. I'm still new to Python and would like some help understanding how to do something like this.

推荐答案

第一个问题是:

N =范围(0,t_f)r_n = R *(N * DT)

n = range(0,t_f)r_n = r0*(n*dt)

下面定义N作为一个列表,并尝试与整数DT乘以名单ñ。这将无法正常工作。纯Python是不是一个量化的语言,像NumPy的或Matlab在这里你可以做向量乘法这样。你可以用做这一行

Here you define n as a list and try to multiply the list n with the integer dt. This will not work. Pure Python is NOT a vectorized language like NumPy or Matlab where you can do vector multiplication like this. You could make this line work with

N = np.arange(0,t_f)r_n = R *(N * DT)

n = np.arange(0,t_f)r_n = r0*(n*dt),

但你不必。相反,你应该将这里的一切都在for循环做计算在每一个时间步长。在present点,你做的计算一次,然后添加相同的唯一的结果 t_f 倍到数据列表。

but you don't have to. Instead, you should move everything inside the for loop to do the calculation at each timestep. At the present point, you do the calculation once, then add the same only result t_f times to the data list.

当然,你要离开你的初始条件(这是ODE解决的一个关键部分)的循环之外,因为它们只影响解决方案的第一步,不是所有的人。

Of course, you have to leave your initial conditions (which is a key part of ODE solving) OUTSIDE of the loop, because they only affect the first step of the solution, not all of them.

所以:

# Initial conditions
r0 = x0
data = [r0]

# Loop along timesteps
for n in range(t_f):

    # calculations performed at each timestep
    vtan = sqrt(x_vel0**2+y_vel0**2)
    dt = 1000
    r_n = r0*(n*dt)
    r_nm1 = r0*((n-1)*dt)
    F_r = ((vtan**2)/r_n)*sina-second_term
    r_np1 = 2*r_n - r_nm1 + dt**2 * F_r

    # append result to output list
    data.append(float(r_np1))

# do something with output list
print data
plt.plot(data)
plt.show()

我没加任何一件code,只能重新安排你的台词。请注意,部分:

I did not add any piece of code, only rearranged your lines. Notice that the part:

n = range(0,t_f)
for time in n:

可以简化为:

Can be simplified to:

for time in range(0,t_f):

不过,您可以使用 N 作为计算时间变量(previously - 和错误地 - 被定义为一个列表,而不是一个单一的数字)。因此,你可以这样写:

However, you use n as a time variable in the calculation (previously - and wrongly - defined as a list instead of a single number). Thus you can write:

for n in range(0,t_f):

注1:我不知道这是否code是正确的数学,因为我甚至不知道你在解方程。在code现在运行,并提供了一​​个结果 - 你必须检查,如果结果是好的

Note 1: I do not know if this code is right mathematically, as I don't even know the equation you're solving. The code runs now and provides a result - you have to check if the result is good.

注2:纯Python是不是用于此目的的最佳工具。你应该尝试一些高度优化的内置插件SciPy的对ODE解决,因为你已经得到了评论提示。

Note 2: Pure Python is not the best tool for this purpose. You should try some highly optimized built-ins of SciPy for ODE solving, as you have already got hints in the comments.

这篇关于工作了一个方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 02:02