I am studying the use of python for numerical computing. I want to calculate the derivative using the central difference method. When I try to set my dx interval, does python translate it to 0 even though the actual value is (1/6)? Any way to get rid of this?
Here is the code:
import numpy as np import matplotlib.pyplot as plt a = 0 b = 1 n = 7 dx = np.float(((b-a)/n)) x = np.linspace(a,b,n) xpp = np.zeros(n) for ii in range(1,n-1): xpp[ii] = (x[ii-1] - 2*x[ii+1] + x[ii+1])/(pow(dx,2)) print xpp
In Python 2.7, integer division does floor:
floor
>>> a = 0 >>> b = 1 >>> n = 7 >>> (b - a) / n 0
=> dxbecomes 0.0=> Inside the body of the cycle for, it is used as a divisor. (0 2 = 0)
dx
0.0
for
( ) float, :
>>> float(b - a) / n 0.14285714285714285
future, .
from __future__ import division
Source: https://habr.com/ru/post/1620475/More articles:How to get form form and form from submit button - javascriptHow `command> file 2> & 1` differs from` command 2> & 1> file` - bashhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1620472/merge-two-maps-to-resultant-map-in-groovy&usg=ALkJrhgLMIiNPuebrTyB_Sow7X8BIWEA4wArray sorting based on arbitrary index - javaGet waypoints between two points - iosHow to calculate the area of a non-convex polygon? - algorithmhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1620477/android-development-on-eclipse-activity-1-changes-to-activity-2-fine-not-3&usg=ALkJrhivf7MCXjZrwtTUYY-2M-ON46yZRQspring websocket with spring security failure with 'No bean named' stompWebSocketHandlerMapping 'defined' - springSign in with Facebook on the UIwebView app - iosHow do I get Yii2 to handle two different instances of a datapath? - phpAll Articles