RuntimeWarning:在double_scalars app.launch_new_instance()中遇到无效的值 [英] RuntimeWarning: invalid value encountered in double_scalars app.launch_new_instance()

查看:205
本文介绍了RuntimeWarning:在double_scalars app.launch_new_instance()中遇到无效的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在应用欧拉方法来求解微分方程.这是我的代码:

I am applying Euler's method to solve a differential equation. This is my code:

def f(x, y):
    return ((x**(2))*y)/((x**(4)) + (y**(4)))

di = 0.01
I = 100

x = np.linspace(-I, I, int(I/di) + 1)
w = np.zeros(len(x))

x[0], w[0]
for i in range(1, len(w)):
    w[i] = w[i - 1] + f(x[i - 1], w[i - 1])*di

plt.plot(x, w, label='approximation' )
plt.xlabel("x") 
plt.ylabel("y")
plt.show()

运行代码时,我收到以下警告:

When I run the code I have this Warning:

"C:\ Users \ USER \ Anaconda3 \ lib \ site-packages \ ipykernel__main __.py:3:RuntimeWarning:在double_scalars app.launch_new_instance()中遇到无效值"

"C:\Users\USER\Anaconda3\lib\site-packages\ipykernel__main__.py:3: RuntimeWarning: invalid value encountered in double_scalars app.launch_new_instance()"

我现在想知道如何解决它并使它工作.

I would like to now how to solve it and make it work.

推荐答案

您的代码正在运行除以零错误.试试这个说服自己:

Your code is running into Divide by Zero Error. Try this to convince yourself:

>>> def f(x, y):
...     return ((x**(2))*y)/((x**(4))+(y**(4)))
...
>>> I, di = 100, 0.01
>>> x = np.linspace(-I, I, int(I/di) + 1)
>>> w = np.zeros(len(x))
>>> i = len(x)//2 + 1
>>> i
5001
>>> f(x[i-1], w[i-1])
nan

从上面的交互式会话中可以明显看出,当i在for循环中采用值5001时,f(x[i-1], w[i-1])产生nan.有几种解决此问题的方法.例如,为了避免 NaN 值,您可以检查是否返回的分数的分母f()为零,然后执行除法.如果是,则应返回您选择的常规值(例如0)而不是除法的结果.以下代码段通过条件表达式实现了这种方法:

It clearly emerges from the interactive session above that when i takes the value 5001 in the for loop, f(x[i-1], w[i-1]) yields nan. There are several solutions to this issue. For instance, in order to avoid NaN values you could check whether the denominator of the fraction returned by f() is zero prior to perform the division. If it is, you should return a conventional value of your choice (for example 0) instead of the result of the division. The following snippet implements such approach though a conditional expression:

def f(x, y):
    return (0 if x==0 and y==0 else float(x**2*y)/(x**4 + y**4))

或者,您可以通过在脚本中包含以下代码来禁用运行时警告(但是,如果这样做,您需要了解潜在风险):

Alternatively, you could disable run time warnings (but if you do so you need to be aware of the potential risks) by including this code in your script:

import warnings

def f(x, y):
    with warnings.catch_warnings():
        warnings.simplefilter('ignore')        
        return ((x**(2))*y)/((x**(4)) + (y**(4)))


建议的解决方法避免了RuntimeWarning,但没有使您的代码正常工作.实际上,计算出的解w是一个向量,其中所有元素均为零.我想您的代码无法正常运行的原因是您错过了为w[0]分配与0不同的初始值的原因.


The proposed workarounds avoid the RuntimeWarning but don't get your code working as you expect. Indeed, the calculated solution w is a vector in which all the elements are zero. I guess the reason for your code not being working properly is that you missed to assign w[0] an initial value different to 0.

例如,如果您仅在for循环之前添加以下行:

For example, if you simply add this line just before the for loop:

w[0] = 0.5

您得到的是这个(看似有意义的)曲线,而不是平坦的图.

you get this (apparently meaningful) curve rather than a flat plot.

希望这会有所帮助!

这篇关于RuntimeWarning:在double_scalars app.launch_new_instance()中遇到无效的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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