Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第五次作業 -- 請寫出避免數值誤差的 n 次微分程式 #5

Open
ccckmit opened this issue Apr 24, 2019 · 0 comments
Open

Comments

@ccckmit
Copy link
Contributor

ccckmit commented Apr 24, 2019

const h = 0.001

function df(f, x, n) {
  if (n===0) return f(x)
  return (df(f,x+h,n-1)-df(f,x,n-1))/h
}

function f(x) {
  return Math.exp(x)
  // return Math.sin(x)
}

for (let n=0; n<=15; n++) {
  console.log('df(f,1,%d)=', n, df(f, 1, n))
}

執行結果

csienqu-teacher:04-search csienqu$ node diffn
df(f,1,0)= 2.718281828459045
df(f,1,1)= 2.7196414225332255
df(f,1,2)= 2.721001696848191
df(f,1,3)= 2.7223623355610016
df(f,1,4)= 2.724043213220284
df(f,1,5)= 2.220446049250313
df(f,1,6)= 1332.2676295501878
df(f,1,7)= -3108624.4689504383
df(f,1,8)= 5329070518.200751
df(f,1,9)= -5329070518200.751
df(f,1,10)= -3996802888650563.5
df(f,1,11)= 37747582837255315000
df(f,1,12)= -1.2390088954816745e+23
df(f,1,13)= 3.0908609005564358e+26
df(f,1,14)= -6.634692795159935e+29
df(f,1,15)= 1.2803091919977304e+33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant