微積分

甚至我們可以將函數列為參數,以進行更高階的運算:

舉例而言,假如我們想檢查某個函數在某點的微分是否為正斜率,那麼就可以寫出下列程式來檢驗:

檔案: diff.js

var c = console;

var dx = 0.001;

function diff(f,x) {
    return (f(x+dx)-f(x))/dx;
}

function isDiffPositive(f,x) {
    return diff(f,x)>0;
}

c.log("isDiffPositive(sin, pi/4)=", isDiffPositive(Math.sin, Math.PI/4));
c.log("isDiffPositive(sin, 3pi/4)=", isDiffPositive(Math.sin, 3*Math.PI/4));

執行結果:

D:\Dropbox\gitbook\rlab\code\set>node diff
isDiffPositive(Math.sin, pi/4)= true
isDiffPositive(Math.sin, 3pi/4)= false

results matching ""

    No results matching ""