CÔNG THỨC SIMPSON TÍNH TÍCH PHÂN


Mã:
(*Cong thuc Simpson*)
Clear[x, y, f, a, b, n, h, x, fx];
    y = Input["Nhap f(x)"];
    f[x_] = y;
    a = Input["Nhap a"];
    b = Input["Nhap b"];
    Print["f(x) = ", y];
    Print["a = ", a];
    Print["b = ", b];
    (*Cau a : Tich tich phan*)
    Print["---- Cau a ----"];
    Print["I= ", Integrate[y, {x, a, b}]];
    (*Cau b : Cong thuc Simpson*)
    Print["---- Cau b ----"];
    n = Input["Nhap n"];
    h = (b - a)/(2n);
    x = Table[a + i*h, {i, 0, 2n}];
    fx = Table[f[x[[i]]], {i, 1, 2n + 1}];
    Simpson = (h/3)*(fx[[1]] + fx[[2n + 1]] + 4Sum[fx[[i]], {i, 2, 2n, 2}] + 2Sum[fx[[i]], {i, 3, 2n - 1, 2}]);
    Print["Cong thuc Simpson I = ", Simpson];
    (*Cau c : Ve do thi*)
    Print["---- Cau c ----"];
    Plot[y, {x, a, b}];