Code Monkey home page Code Monkey logo

gointegral's Introduction

gointegral --- golang implementation of Simpson method

シンプソン法の golang 実装。

十分小さな区間

における関数

の定積分は、次の近似式で与えられる。

区間

の定積分であれば、区間を N 等分した十分に小さな区間

の定積分の合計と考えればよい。

where


gointegral では積分区間 interval、区間を等分する個数 N、積分対象とする関数 f を JavaScript のコードで与える。

例として、sin 関数の[0,π] 区間での定積分では次のような JavaScript コードを使う。

// sin.js

// 積分区間 [a, b]
var interval = [0, Math.PI];

// 区間等分数 N
var n = 1000;

// 積分の対象とする関数 f(x)
function f(x) {
    return Math.sin(x);
}

gointegral の使い方は次の通りである。

$ ./gointegral sin.js
interval = [0 3.141592653589793]
n = 1000
result = 1.999995065201925
$ 

積分結果がわかっていて、シンプソン法のエラー率[%]を求めたい場合には、次のように answer 変数を指定する。

// 積分区間 [a, b]
var interval = [0, Math.PI];

// 区間等分数 N
var n = 1000;

// 積分の対象とする関数 f(x)
function f(x) {
    return Math.sin(x);
}

// エラー率[%]を算出するための正しい積分結果の指定 
var answer = 2;

実行結果は以下のとおりである。

$ ./gointegral sin.js
interval = [0 3.141592653589793]
n = 1000
result = 1.999995065201925
answer = 2
error  = 0.0002467399037531237 [%]

いくつか実行例を示す。

// 積分区間
var interval = [0, 1];

// 区間等分数 N
var n = 1000;

// 積分対象の関数
function f(x) {
    return 5*Math.pow(x, 4);
}

// エラー率を算出するための正しい積分結果の指定 
var answer = 1;

実行結果:

interval = [0 1]
n = 1000
result = 1.0000000000000449
answer = 1
error  = 4.4853010194856324e-12 [%]

// 積分区間
var interval = [0, 2*Math.PI];

// 区間等分数
var n = 1000;

// 積分対象の関数
function f(x) {
    return Math.abs(Math.cos(x));
}

// エラー率を算出するための正しい積分結果の指定 
var answer = 4;

実行結果:

interval = [0 6.283185307179586]
n = 1000
result = 4.006283143967614
answer = 4
error  = 0.15707859919034117 [%]

// 積分区間
var interval = [Math.sqrt(2), 2];

// 区間等分数
var n = 1000;

// 積分対象の関数
function f(x) {
    return 1/(x*Math.sqrt(Math.pow(x, 2)-1));
}

// エラー率を算出するための正しい積分結果の指定 
var answer = Math.PI/12;

実行結果:

interval = [1.4142135623730951 2]
n = 1000
result = 0.26196843201518166
answer = 0.2617993877991494
error  = 0.06457013419830616 [%]

gointegral's People

Contributors

bunji2 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.