簡體   English   中英

如何使用node.js制作一個計算圖形曲線下面積的函數?

[英]How to make a function that will calculate the area under the curve of the graph using node.js?

我需要創建函數,以便在我在命令行上輸入函數 f 和系數 a、b 和 n 時計算面積。

現在我有這個:

module.exports = function (f,a,b,n,next) {

parseFloat(a);
parseFloat(b);
parseInt(n);
var h = (b-a)/n;
var s = 0;
var suma = function () {
    for (var i = 0; i <= n; i++) {
        var ximj = a+h*(i-1);
        var xi = a+h*i;
        var x = (ximj + xi)/2;
        s += eval(f,x);
    }
    return s;
};

if (n<1) {
    next(new Error("Koeficijent n je broj ekvidistantnih točaka."))
}
else next(null, {povrsina : function () {return h*suma();}}
    );

我認為函數suma()沒有像它應該的那樣工作。

在命令行中,這應該如下所示:

f: x*x-2*x+7
a: 2
b: 4
n: 10
Povrsina ispod grafa funkcije f je 20.66000...

我的提示文件如下所示:

var pov = require('./integrator.js');

var prompt = require('prompt');

prompt.get (['f','a','b','n'],function (err,koef) {
if (err) return console.log(err);
console.log("f: ",koef.f);
console.log("a: ",koef.a);
console.log("b: ",koef.b);
console.log("n: ",koef.n);

parseFloat(koef.a);
parseFloat(koef.b);
parseInt(koef.n);

pov(koef.f,koef.a,koef.b,koef.n,function(err,rj){
    if (err)
        console.log(err);
    else 
        console.log("Povrsina ispod grafa funkcije f je " + rj.povrsina());
});

謝謝你的幫助:)

eval函數讀取一個字符串並對其求值。 如果你想用參數x計算函數f ,那么你可以只寫f(x)而不是調用eval() 這是因為,在 JavaScript 中,函數是一等值,可以像數字、字符串等一樣傳遞。

suma ,特別是var ximj = a+h*(i-1); ,你的 for 循環將h乘以 -1,然后乘以 0,然后乘以 1。我懷疑你的意思是var ximj = a+h*(i+1); 因為h是你的微分的寬度。 但這只會產生小錯誤。

正如 Lends 所寫,您還需要更正Parse*調用:

a = parseFloat(a);
b = parseFloat(b);
n = parseInt(n);

您應該使用math.js來評估字符串數學表達式,例如:

var math = require('mathjs');

var integral = function(f, a, b, n, next) {
    a = parseFloat(a);
    b = parseFloat(b);
    n = parseInt(n);
    var h = (b - a) / n;
    var s = 0;
    var suma = function() {
        for (var i = 0; i <= n; i++) {
            var ximj = a + h * (i - 1);
            var xi = a + h * i;
            var x = (ximj + xi) / 2;
            s += math.eval(f, { x: x });
        }

        return s;
    };

    if (n < 1) {
        next(new Error("Koeficijent n je broj ekvidistantnih točaka."))
    } else {
        next(null, { povrsina: function() { return h * suma(); } });
    }
};

var f = 'x * x - 2 * x + 7';
var a = '2';
var b = '4';
var n = '10000';

integral(f, a, b, n, function(err, rj) {
    if (err)
        console.log(err);
    else
        console.log("Povrsina ispod grafa funkcije f je " + rj.povrsina());
});

/* output:
Povrsina ispod grafa funkcije f je 20.66806662000201
*/

您的積分函數似乎有問題,但 n 必須非常高才能接近預期結果(如 10000)。 或者它應該以這種方式工作? 例如,對於 n = 10,它給出 22。

另一種解決方案是使用現有的包或至少閱讀源代碼來獲得一個想法。

首先,如果您想計算曲線下的面積,您將需要提供 x 上下限,否則答案將永遠是無限的。 我個人使用我寫的這個函數

function findArea(lowerLimit, upperLimit, coefficients) {

  let lower = 0
  let upper = 0

  for (let i = 0; i < coefficients.length; i++) {

    lower += (coefficients[i] * Math.pow(lowerLimit, i + 1)) / (i + 1)
    upper += (coefficients[i] * Math.pow(upperLimit, i + 1)) / (i + 1)

  }

  return Math.abs(upper - lower)

}

如果你傳入兩個數字作為限制和一個格式的系數數組, a + bx + cx^2 + dx^3 ... 等等,你會得到正確的答案。 例如,假設你有方程y = 5 + 4x + 3x^2 + 2x^3,並且你想找到x = 1x = 3之間曲線下的面積,你可以像這樣調用這個函數, findArea(1, 3, [5, 4, 3, 2]) ,你會得到答案92 ,它代表單位的平方數。 請記住,這僅在您評估的區域完全高於或完全低於 x 軸時才有效。 如果您的區域穿過 x 軸,您將需要分別計算上方和下方的區域!

您可以根據曲線計算器下符號區域檢查結果,但這應該適用於任何曲線。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM