Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(b) Compare the answers you obtain in (a) with the exact by computing the relati

ID: 3839251 • Letter: #

Question

(b) Compare the answers you obtain in (a) with the exact by computing the relative error in each case.

(c) Comment on the results you obtain.

(* SIMPSON'S COMPOSITE ALGORITHM 4.1
*
* To approximate I=integral ( ( f(x) dx ) ) from a to b:
*
* INPUT: endpoints a, b; even positive integer n.
*
* OUTPUT: approximation XI to I
*)
TEMP = Input["This is Simpson's Method.
Input the function F(X) in terms of x.

For example: Cos[x] "];
F[x_] := Evaluate[TEMP];
OK = 0;
While[OK == 0,
A = Input["Input the lower limit of integration "];
B = Input["Input the upper limit of integration "];
If[A > B,
Input["Lower limit must be less than upper limit

Press 1 [enter] to continue "],
OK = 1;
];
];
OK = 0;
While[OK == 0,
n = Input["Input an even positive integer N. "];
If[ n > 0 && Mod[n,2]==0,
OK = 1,
Input["Number must be even and positive

Press 1 [enter] to continue "];
];
];
If[OK == 1,
(* Step 1 *)
H = (B-A)/n;
(* Step 2 *)
Xi0 = F[A]+F[B];
(* Summation of f(x(2*i-1)) *)
Xi1 = 0.0;
(* Summation of f(x(2*i)) *)
Xi2 = 0.0;
(* Step 3 *)
NN = n-1;
For[i = 1,
i <= NN,
i++,
(* Step 4 *)
X = A+i*H;
(* Step 5 *)
If[Mod[i,2] == 0,
Xi2 = Xi2+F[X],
Xi1 = Xi1+F[X];
];
];
(* Step 6 *)
Xi = (Xi0+2.0*Xi2+4.0*Xi1)*H/3.0;
(* Step 7 *)
Print[" "];
Print["The integral of F from ",A," to ",B,
" is ",N[Xi,9]];
];
.1a

0

Explanation / Answer

TEMP = Input["This is Simpson's Method.
Input the function F(X) in terms of x.

For example: Cos[x] "];
F[x_] := Evaluate[TEMP];
OK = 0;
While[OK == 0,
A = Input["Input the lower limit of integration "];
B = Input["Input the upper limit of integration "];
If[A > B,
Input["Lower limit must be less than upper limit

Press 1 [enter] to continue "],
OK = 1;
];
];
OK = 0;
While[OK == 0,
n = Input["Input an even positive integer N. "];
If[ n > 0 && Mod[n,2]==0,
OK = 1,
Input["Number must be even and positive

Press 1 [enter] to continue "];
];
];
If[OK == 1,
(* Step 1 *)
H = (B-A)/n;
(* Step 2 *)
Xi0 = F[A]+F[B];
(* Summation of f(x(2*i-1)) *)
Xi1 = 0.0;
(* Summation of f(x(2*i)) *)
Xi2 = 0.0;
(* Step 3 *)
NN = n-1;
For[i = 1,
i <= NN,
i++,
(* Step 4 *)
X = A+i*H;
(* Step 5 *)
If[Mod[i,2] == 0,
Xi2 = Xi2+F[X],
Xi1 = Xi1+F[X];
];
];
(* Step 6 *)
Xi = (Xi0+2.0*Xi2+4.0*Xi1)*H/3.0;
(* Step 7 *)
Print[" "];
Print["The integral of F from ",A," to ",B,
" is ",N[Xi,9]];
];