H** FORMULE DI NEWTON-COTES ** L
In[154]:=
f @ x_ D := Exp @ x D ; a = 0.; b = 1.;
h = b - a;
true1 = Integrate @ f @ x D , x D ; true = Integrate @ f @ x D , 8 x, 0, 1 <D ; trap = H f @ a D + f @ b DL * h 2.;
x0 = a; x1 = H a + b L 2.; x2 = b;
h = H b - a L 2.;
simps = H f @ x0 D + 4 * f @ x1 D + f @ x2 DL * h 3.;
Print @ "Funzione: Exp @ x D ", " Primitiva: ", true1 D Print @ "Valore numerico esatto = ",
N @ true D , " Trapezi = ", trap, " Simpson = ", simps D ; Plot @ f @ x D , 8 x, a, b < , PlotRange ® 8 0, 3 <D
Funzione: Exp @ x D Primitiva: ã
xValore numerico esatto = 1.71828 Trapezi = 1.85914 Simpson = 1.71886
Out[165]=
0.0 0.2 0.4 0.6 0.8 1.0
0.5 1.0 1.5 2.0 2.5 3.0
In[140]:=
Clear @ f D ; f @ x_ D := Cos @ x D ; Print @D ;
a = 0.; b = Pi 2.;
h = b - a;
true1 = Integrate @ f @ x D , x D ; true = Integrate @ f @ x D , 8 x, a, b <D ; trap = H f @ a D + f @ b DL * h 2.;
x0 = a; x1 = H a + b L 2.; x2 = b;
h = H b - a L 2.;
simps = H f @ x0 D + 4 * f @ x1 D + f @ x2 DL * h 3.;
Print @ "Funzione: Cos @ x D ", " Primitiva: ", true1 D Print @ "Valore numerico esatto = ",
N @ true D , " Trapezi = ", trap, " Simpson = ", simps D ;
Plot @ f @ x D , 8 x, a, b < , PlotRange ® 8 0, 1 <D
Funzione: Cos @ x D Primitiva: Sin @ x D
Valore numerico esatto = 1. Trapezi = 0.785398 Simpson = 1.00228
Out[153]=
0.0 0.5 1.0 1.5
0.2 0.4 0.6 0.8 1.0
Clear @ f D ;
f @ x_ D := 1 - 5 x
2+ x
4; a = -2.; b = 2.;
h = b - a;
true1 = Integrate @ f @ x D , x D ; true = Integrate @ f @ x D , 8 x, a, b <D ; trap = H f @ a D + f @ b DL * h 2.;
x0 = a; x1 = H a + b L 2.; x2 = b;
h = H b - a L 2.;
simps = H f @ x0 D + 4 * f @ x1 D + f @ x2 DL * h 3.;
Print A "Funzione: 1-5 x
2+x
4", " Primitiva: ", true1 E ; Print @ "Valore numerico esatto = ", N @ true D ,
" Trapezi = ", trap, " Simpson = ", simps D ; Plot @ f @ x D , 8 x, a, b < , PlotRange ® Automatic D
Funzione: 1-5 x
2+x
4Funzione: 1-5 x
2+x
4Primitiva: x - 5 x
33 + x
55
Valore numerico esatto = -9.86667 Trapezi = -12. Simpson = -1.33333
Out[139]=
-2 -1 1 2
-5 -4 -3 -2 -1 1 2 Integrazione1.nb