简体   繁体   English

寻找导数的二阶精确方案

[英]second order accurate scheme for finding derivatives

So I'm doing a class project and have to plot a numerical approximation of the derivative of f(x)=x*arctan(x) using the second order accurate scheme, f(xi) ≈(f(xi+1) − f(xi−1))/2h oh and also plot f(x)所以我正在做一个课堂项目,必须使用二阶精确方案f(xi) ≈(f(xi+1) − f(xi−1))/2h绘制f(x)=x*arctan(x)导数的数值近似值f(xi) ≈(f(xi+1) − f(xi−1))/2h哦,还绘制f(x)

I've done this so far:到目前为止,我已经这样做了:

from numpy import *
from matplotlib.pylab import *
a=0
b=2
n=100
h=(b-a)/n
def f(x):
    f=x*arctan(x)
    return(f)
def dydx(x):
    d=(f(x+h)-f(x-h))/2*h
    return(d)
x=linspace(a,b,n+1)
plot(x,f(x),'b')
plot(x,dydx(x),'r--')

the problem i am receiving is that the graph for my derivative is coming out at a significantly lesser value than it should be (ie limit at 0.0006 instead of 1.6 ) - how do I fix this?我收到的问题是,我的导数图的值明显低于它应有的值(即限制在0.0006而不是1.6 ) - 我该如何解决这个问题?

I'm not the brightest light when it comes to math problems, so I might misunderstand your question, but the problem couldn't perhaps be that you simply forgot to place 2*h inside of some parenthesis?在数学问题方面,我不是最聪明的人,所以我可能会误解你的问题,但问题可能不是你只是忘记将 2*h 放在括号内?

So instead of: d=(f(x+h)-f(xh))/2*h Try: d=(f(x+h)-f(xh))/(2*h)因此,而不是: d=(f(x+h)-f(xh))/2*h尝试: d=(f(x+h)-f(xh))/(2*h)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM