In[334]:=
BMethod[a0_, b0_, n_, f_, e_] := Module[{a = N[a0], b = N[b0], p},
If[f[a] * f[b] ≥ 0, Print["Root does not exist in the interval (a0, b0)"];
Return[]];
For[i = 1, i ≤ n, i ++, p = (a + b) / 2;
If[(b - a) < 2 * e, Print["The approximation value at iteration ", i, " is ", p];
Return[]];
If[f[a] * f[p] < 0, b = p, a = p];
Print["The root lies in the interval (", a, ", ", b,
"). ", "The approximation value at iteration ", i, " is ", p];];
Print["Maximum iterations exceeded. The approximation value at iteration number ",
n, " is ", p];];
f[x_] := x ^ 3 + 2 * x ^ 2 - 3 * x - 1;
Plot[f[x], {x, -5, 5}, PlotLabel "Plot of f(x)", GridLines Automatic]
BMethod[-5, 5, 22, f, 10 ^ (-5)];
Out[336]=
Plot of f (x)
50
-4 -2 2 4
-50
The root lies in the interval (0., 5.). The approximation value at iteration 1 is 0.
The root lies in the interval (0., 2.5). The approximation value at iteration 2 is 2.5
The root lies in the interval (0., 1.25). The approximation value at iteration 3 is 1.25
The root lies in the interval (0.625, 1.25). The approximation value at iteration 4 is 0.625
The root lies in the interval (0.9375,
1.25). The approximation value at iteration 5 is 0.9375
The root lies in the interval (1.09375,
1.25). The approximation value at iteration 6 is 1.09375
The root lies in the interval (1.17188,
1.25). The approximation value at iteration 7 is 1.17188
The root lies in the interval (1.17188,
1.21094). The approximation value at iteration 8 is 1.21094
The root lies in the interval (1.19141,
1.21094). The approximation value at iteration 9 is 1.19141
2
The root lies in the interval (1.19141, 1.20117
). The approximation value at iteration 10 is 1.20117
The root lies in the interval (1.19629, 1.20117
). The approximation value at iteration 11 is 1.19629
The root lies in the interval (1.19629, 1.19873
). The approximation value at iteration 12 is 1.19873
The root lies in the interval (1.19751, 1.19873
). The approximation value at iteration 13 is 1.19751
The root lies in the interval (1.19812, 1.19873
). The approximation value at iteration 14 is 1.19812
The root lies in the interval (1.19843, 1.19873
). The approximation value at iteration 15 is 1.19843
The root lies in the interval (1.19858, 1.19873
). The approximation value at iteration 16 is 1.19858
The root lies in the interval (1.19865, 1.19873
). The approximation value at iteration 17 is 1.19865
The root lies in the interval (1.19865, 1.19869
). The approximation value at iteration 18 is 1.19869
The root lies in the interval (1.19867, 1.19869
). The approximation value at iteration 19 is 1.19867
The approximation value at iteration 20 is 1.19868
The root lies in the interval (0., 2.5). The approximation value at iteration 2 is 2.5
The root lies in the interval (0., 1.25). The approximation value at iteration 3 is 1.25
The root lies in the interval (0.625, 1.25). The approximation value at iteration 4 is 0.625
The root lies in the interval (0.9375,
1.25). The approximation value at iteration 5 is 0.9375
The root lies in the interval (1.09375,
1.25). The approximation value at iteration 6 is 1.09375
The root lies in the interval (1.17188,
1.25). The approximation value at iteration 7 is 1.17188
The root lies in the interval (1.17188,
1.21094). The approximation value at iteration 8 is 1.21094
The root lies in the interval (1.19141,
1.21094). The approximation value at iteration 9 is 1.19141
The root lies in the interval (1.19141, 1.20117
). The approximation value at iteration 10 is 1.20117
The root lies in the interval (1.19629, 1.20117
). The approximation value at iteration 11 is 1.19629
The root lies in the interval (1.19629, 1.19873
). The approximation value at iteration 12 is 1.19873
The root lies in the interval (1.19751, 1.19873
). The approximation value at iteration 13 is 1.19751
The root lies in the interval (1.19812, 1.19873
). The approximation value at iteration 14 is 1.19812
The root lies in the interval (1.19843, 1.19873
). The approximation value at iteration 15 is 1.19843
3
The root lies in the interval (1.19858, 1.19873
). The approximation value at iteration 16 is 1.19858
The root lies in the interval (1.19865, 1.19873
). The approximation value at iteration 17 is 1.19865
Out[101]=
Plot of f (x)
50
-4 -2 2 4
-50
4
The root lies in the interval (0., 5.). The approximation value at iteration 1 is 0.
The root lies in the interval (0., 2.5). The approximation value at iteration 2 is 2.5
The root lies in the interval (0., 1.25). The approximation value at iteration 3 is 1.25
The root lies in the interval (0.625, 1.25). The approximation value at iteration 4 is 0.625
The root lies in the interval (0.9375,
1.25). The approximation value at iteration 5 is 0.9375
The root lies in the interval (1.09375,
1.25). The approximation value at iteration 6 is 1.09375
The root lies in the interval (1.17188,
1.25). The approximation value at iteration 7 is 1.17188
The root lies in the interval (1.17188,
1.21094). The approximation value at iteration 8 is 1.21094
The root lies in the interval (1.19141,
1.21094). The approximation value at iteration 9 is 1.19141
The root lies in the interval (1.19141, 1.20117
). The approximation value at iteration 10 is 1.20117
The root lies in the interval (1.19629, 1.20117
). The approximation value at iteration 11 is 1.19629
The root lies in the interval (1.19629, 1.19873
). The approximation value at iteration 12 is 1.19873
The root lies in the interval (1.19751, 1.19873
). The approximation value at iteration 13 is 1.19751
The root lies in the interval (1.19812, 1.19873
). The approximation value at iteration 14 is 1.19812
The root lies in the interval (1.19843, 1.19873
). The approximation value at iteration 15 is 1.19843
The root lies in the interval (1.19858, 1.19873
). The approximation value at iteration 16 is 1.19858
The root lies in the interval (1.19865, 1.19873
). The approximation value at iteration 17 is 1.19865
The root lies in the interval (1.19865, 1.19869
). The approximation value at iteration 18 is 1.19869
The root lies in the interval (1.19867, 1.19869
). The approximation value at iteration 19 is 1.19867
The approximation value at iteration 20 is 1.19868
Module: Module called with 1 argument; 2 or more arguments are expected.
5
The approximate value achieved at iteration number 1 is 2.
The approximate value achieved at iteration number 2 is 1.5
The approximate value achieved at iteration number 3 is 4.
The approximate value achieved at iteration number 4 is 3.038461538
The approximate value achieved at iteration number 5 is 2.390282147
The approximate value achieved at iteration number 6 is 1.911611898
Max number of iterations has been exceeded. The
final approximation achieved at iteration number 6 is 1.911611898
Out[35]=
Null4 , {}
NMethod[x0_, f_, n_, e_] := Module[{x = N[x0], xa1},
For[i = 1, i ≤ n, i ++, xa1 = x - (f[x] / f '[x]);
If[Abs[xa1 - x] < e, Print["The final approximation achieved at iteration number ",
i, " is ", NumberForm[xa1, 10]];
Return[]];
Print["The approximate value achieved at iteration number ",
i, " is ", NumberForm[xa1, 10]];
x = xa1;];
Print[
"Max number of iterations has been exceeded. The final approximation achieved
at iteration number ", n, " is ", NumberForm[xa1, 10]];];
f[x_] := x ^ 3 - 3 * x ^ 2 + 2 * x + 1;
Plot[f[x], {x, -1, 2}, PlotLabel "Plot of f(x)", GridLines Automatic]
NMethod[1, f, 6, 10 ^ (-4)];
Out[73]=
Plot of f (x)
-1.0 -0.5 0.5 1.0 1.5 2.0
-1
-2
-3
-4
-5
6
The approximate value achieved at iteration number 1 is 2.
The approximate value achieved at iteration number 2 is 1.5
The approximate value achieved at iteration number 3 is 4.
The approximate value achieved at iteration number 4 is 3.038461538
The approximate value achieved at iteration number 5 is 2.390282147
The approximate value achieved at iteration number 6 is 1.911611898
Max number of iterations has been exceeded. The
final approximation achieved at iteration number 6 is 1.911611898
In[119]:=
NMethod[x0_, f_, n_, e_] := Module[{x = N[x0], xa1},
For[i = 1, i ≤ n, i ++, xa1 = x - (f[x] / f '[x]);
If[Abs[xa1 - x] < e,
Print[
" The approximate value at iteration number,", i, "is", NumberForm[xa1, 10]];
Return[];];
Print[" The approximate value at iteration number,",
i, "is", NumberForm[xa1, 10]];
x = xa1]];
f[x_] := x ^ 3 - 3 * x ^ 2 + 2 * x + 1;
Plot[f[x], {x, -1, 2}, PlotLabel " Plot for F[x]", GridLines Automatic]
NMethod[1, f, 6, 10 ^ (-4)];
Out[121]=
Plot for F[x]
-1.0 -0.5 0.5 1.0 1.5 2.0
-1
-2
-3
-4
-5
The approximate value at iteration number,1is2.
The approximate value at iteration number,2is1.5
The approximate value at iteration number,3is4.
The approximate value at iteration number,4is3.038461538
The approximate value at iteration number,5is2.390282147
The approximate value at iteration number,6is1.911611898
7
In[131]:=
NMethod[x0_, f_, n_, e_] := Module[{x = N[x0], xa1},
For[i = 1, i ≤ n, i ++, xa1 = x - (f[x] / f '[x]);
If[Abs[xa1 - x] < e,
Print[
" The approximate value at iteration number", i, "is", NumberForm[xa1, 10]];
Return[];];
Print[
" the approximate value at iteration number", i, "is", NumberForm[xa1, 10]];
x = xa1];
];
f[x_] := x ^ 3 - 3 * x ^ 2 + 2 * x + 1;
Plot[f[x], {x, -1, 2}, PlotLabel "Plot f[x]", GridLines Automatic]
NMethod[1, f, 6, 10 ^ (-4)];
Out[133]=
Plot f [x]
-1.0 -0.5 0.5 1.0 1.5 2.0
-1
-2
-3
-4
-5
the approximate value at iteration number1is2.
the approximate value at iteration number2is1.5
the approximate value at iteration number3is4.
the approximate value at iteration number4is3.038461538
the approximate value at iteration number5is2.390282147
the approximate value at iteration number6is1.911611898
8
In[49]:= GaussJacobi[a0_, b0_, x0_, nmax_] :=
Module[{a = N[a0], b = N[b0], x = N[x0], xa, i, j, k, n, m, size}, size = Dimensions[a];
n = size〚1〛;
m = size〚2〛;
If[n ≠ m,
Print["Not a square matrix. Gauss-Jacobi iterations cannot be evaluated."];
Return[]];
xa = Table[0, {n}];
For[k = 1, k ≤ nmax, k ++,
Print["The approximate value of x at iteration number ", k, " is: "];
For[i = 1, i ≤ n, i ++, xa〚i〛 = (b〚i〛 -
Sum[a〚i, j〛 * x〚j〛, {j, 1, i - 1}] - Sum[a〚i, j〛 * x〚j〛, {j, i + 1, n}]) / a〚i, i〛;
Print[xa〚i〛];];
x = xa; ];];
a = {{5, 1, 2}, {-3, 9, 4}, {1, 2, -7}};
b = {10, -14, 33};
x = {0, 0, 0};
GaussJacobi[a, b, x, 3];
The approximate value of x at iteration number 1 is:
2.
-1.55556
-4.71429
The approximate value of x at iteration number 2 is:
4.19683
1.20635
-4.87302
The approximate value of x at iteration number 3 is:
3.70794
2.00917
-3.77007
9
In[54]:= GaussSeidel[a0_, b0_, x0_, nmax_] :=
Module[{a = N[a0], b = N[b0], x = N[x0], i, j, k, n, m, size}, size = Dimensions[a];
n = size〚1〛;
m = size〚2〛;
If[n ≠ m,
Print["Not a square matrix. Gauss-Seidel iterations cannot be evaluated."];
Return[]];
For[k = 1, k ≤ nmax, k ++,
Print["The approximate value of x at iteration number ", k, " is: "];
For[i = 1, i ≤ n, i ++, x〚i〛 = (b〚i〛 - Sum[a〚i, j〛 * x〚j〛, {j, 1, i - 1}] -
Sum[a〚i, j〛 * x〚j〛, {j, i + 1, n}]) / a〚i, i〛;];
Print[x]; ];];
a = {{5, 1, 2}, {-3, 9, 4}, {1, 2, -7}};
b = {10, -14, 33};
x = {0, 0, 0};
GaussSeidel[a, b, x, 3];
The approximate value of x at iteration number 1 is:
{2., -0.888889, -4.68254}
The approximate value of x at iteration number 2 is:
{4.05079, 1.87584, -3.59965}
The approximate value of x at iteration number 3 is:
{3.06469, 1.06585, -3.97194}
10
In[155]:=
GaussJacobi[a0_, b0_, x0_, nmax_] :=
Module[{a = N[a0], b = N[b0], x = N[x0], xa, i, j, k, n, m, size}, size = Dimensions[a];
n = size〚1〛;
m = size〚2〛;
If[n ≠ m,
Print[ " not a square matrix. Gauss- Jacobi iterations cannot be evaluated."];
Return[];];
xa = Table[0, {n}];
For[k = 1, k ≤ nmax, k ++, Print[" The value of x at iteration", k, "is:"];
For[i = 1, i ≤ n, i ++, xa〚i〛 = (b〚i〛 - Sum[ a〚i, j〛 * x〚j〛, {j, 1, i - 1}] -
Sum[a〚i, j〛 * x〚j〛, {j, i + 1, n}]) / a〚i, i〛;
Print[xa〚i〛];];
x = xa];];
a = {{5, 1, 2}, {-3, 9, 4}, {1, 2, -7}};
b = {10, -14, 33};
x = {0, 0, 0}
GaussJacobi[a, b, x, 3];
Out[158]=
{0, 0, 0}
The value of x at iteration1is:
2.
-1.55556
-4.71429
The value of x at iteration2is:
4.19683
1.20635
-4.87302
The value of x at iteration3is:
3.70794
2.00917
-3.77007
11
In[165]:=
GaussJacobi[a0_, b0_, x0_, nmax_] :=
Module[{a = N[a0], b = N[b0], x = N[x0], xa, i, j, k, m, n, size}, size = Dimensions[a];
n = size〚1〛;
m = size〚2〛;
If[n ≠ m,
Print["not a square matrix. Gauss-Jacobi iterations cannot be evaluated"];
Return[]];
xa = Table[0, {n}];
For[k = 1, k ≤ nmax, k ++, Print["The value of x at iteration number", k, " is:"];
For[i = 1, i ≤ n, i ++, xa〚i〛 = (b〚i〛 -
Sum[a〚i, j〛 * x〚j〛, {j, 1, i - 1}] - Sum[a〚i, j〛 * x〚j〛, {j, i + 1, n}]) / a〚i, i〛;
Print[xa〚i〛];];
x = xa];]
a = {{5, 1, 2}, {-3, 9, 4}, {1, 2, -7}};
b = {10, -14, 33};
x = {0, 0, 0}
GaussJacobi[a, b, x, 3];
Out[168]=
{0, 0, 0}
12
The value of x at iteration number1 is:
2.
-1.55556
-4.71429
The value of x at iteration number2 is:
4.19683
1.20635
-4.87302
The value of x at iteration number3 is:
3.70794
2.00917
-3.77007
(-7.)[
33. - 1. × 5.[10. - 2. (-7.)[33.] - 1. × 9.[-14.]] - 2. × 9.[-14. - 4. (-7.)[33.] + 3. × 5.[10.]]]
9.[-14. - 4. (-7.)[33. - 1. × 5.[10.] - 2. × 9.[-14.]] + 3. × 5.[10. - 2. (-7.)[33.] - 1. × 9.[-14.]]]
5.[10. - 2. (-7.)[33. - 1. × 5.[10.] - 2. × 9.[-14.]] - 1. × 9.[-14. - 4. (-7.)[33.] + 3. × 5.[10.]]]
The value of x at iteration number3 is:
(-7.)[33. - 1. × 5.[10.] - 2. × 9.[-14.]]
9.[-14. - 4. (-7.)[33.] + 3. × 5.[10.]]
5.[10. - 2. (-7.)[33.] - 1. × 9.[-14.]]
The value of x at iteration number2 is:
(-7.)[33.]
9.[-14.]
5.[10.]
The value of x at iteration number1 is:
13
2.
-1.55556
The approximate value of x at iteration number 2 is:
4.19683
1.20635
-4.87302
The approximate value of x at iteration number 3 is:
3.70794
2.00917
-3.77007
f(x) = {1, 1, 1}
Approximate Integral Value = 0.693771
Integrate: Invalid integration variable or limit(s) in {{0, 0, 0}, 0, 1}.
NIntegrate: Tag List in {0, 0, 0} is Protected.
NIntegrate: Tag List in {0, 0, 0} is Protected.
NIntegrate: Tag List in {0, 0, 0} is Protected.
General: Further output of NIntegrate::write will be suppressed during this calculation.
True Integral Value = {NIntegrate[1, {{0, 0, 0}, 0, 1}],
NIntegrate[1, {{0, 0, 0}, 0, 1}], NIntegrate[1, {{0, 0, 0}, 0, 1}]}
NIntegrate: Tag List in {0, 0, 0} is Protected.
NIntegrate: Tag List in {0, 0, 0} is Protected.
NIntegrate: Tag List in {0, 0, 0} is Protected.
General: Further output of NIntegrate::write will be suppressed during this calculation.
Error = {-0.693771 + NIntegrate[1, {{0, 0, 0}, 0, 1}],
-0.693771 + NIntegrate[1, {{0, 0, 0}, 0, 1}], -0.693771 + NIntegrate[1, {{0, 0, 0}, 0, 1}]}
In[318]:=
TrapezoidalRule[a0_, b0_, n_, f_] :=
Module[{a = a0, b = b0, h, ApproximateIntegral}, h = (b - a) / n;
ApproximateIntegral = ((h / 2) * (f[a] + f[b])) + h * Sum[f[a + h * k], {k, 1, n - 1}];
Return[ApproximateIntegral];];
f[x_] := Exp[x ^ 2];
Print["f[x]=", f[x]];
Print["Approximate Integral Value=", N[TrapezoidalRule[0, 1, 100, f]]];
TrueValue = Integrate[Exp[x ^ 2], {x, 0, 1}];
Print["True Integral value=", N[TrueValue]];
Print["Error=", N[TrueValue - TrapezoidalRule[0, 1, 100, f]]];
14
f[x]={1, 1, 1}
Approximate Integral Value=1.4627
Integrate: Invalid integration variable or limit(s) in {{0, 0, 0}, 0, 1}.
NIntegrate: Tag List in {0, 0, 0} is Protected.
NIntegrate: Tag List in {0, 0, 0} is Protected.
NIntegrate: Tag List in {0, 0, 0} is Protected.
General: Further output of NIntegrate::write will be suppressed during this calculation.
True Integral value={NIntegrate[1, {{0, 0, 0}, 0, 1}],
NIntegrate[1, {{0, 0, 0}, 0, 1}], NIntegrate[1, {{0, 0, 0}, 0, 1}]}
NIntegrate: Tag List in {0, 0, 0} is Protected.
NIntegrate: Tag List in {0, 0, 0} is Protected.
NIntegrate: Tag List in {0, 0, 0} is Protected.
General: Further output of NIntegrate::write will be suppressed during this calculation.
Error={-1.4627 + NIntegrate[1, {{0, 0, 0}, 0, 1}],
-1.4627 + NIntegrate[1, {{0, 0, 0}, 0, 1}], -1.4627 + NIntegrate[1, {{0, 0, 0}, 0, 1}]}
Integrate: Invalid integration variable or limit(s) in {{0, 0, 0}, 0, 1}.
NIntegrate: Tag List in {0, 0, 0} is Protected.
NIntegrate: Tag List in {0, 0, 0} is Protected.
NIntegrate: Tag List in {0, 0, 0} is Protected.
General: Further output of NIntegrate::write will be suppressed during this calculation.
True Integral value = {NIntegrate[1, {{0, 0, 0}, 0, 1}],
NIntegrate[1, {{0, 0, 0}, 0, 1}], NIntegrate[1, {{0, 0, 0}, 0, 1}]}
NIntegrate: Tag List in {0, 0, 0} is Protected.
NIntegrate: Tag List in {0, 0, 0} is Protected.
NIntegrate: Tag List in {0, 0, 0} is Protected.
General: Further output of NIntegrate::write will be suppressed during this calculation.
15
In[325]:=
TrapezoidalRule[a0_, b0_, n_, f_] :=
Module[{a = a0, b = b0, h, ApproximateIntegral}, h = (b - a) / n;
ApproximateIntegral = ((h / 2) * (f[a] + f[b])) + h * Sum[f[a + h * k], {k, 1, n - 1}];
Return[ApproximateIntegral];];
(*Define the function f as an explicit function*)
f[x_] := Exp[x ^ 2];
(*Print the function definition*)
Print["f[x] = Exp[x^2]"];
(*Compute the approximate integral with n=100 for better accuracy*)
approxIntegral = TrapezoidalRule[0, 1, 100, f];
Print["Approximate Integral Value = ", N[approxIntegral]];
(*Compute the true integral value symbolically*)
TrueValue = Integrate[Exp[x ^ 2], {x, 0, 1}];
Print["True Integral value = ", N[TrueValue]];
(*Calculate the error*)
error = N[TrueValue - approxIntegral];
Print["Error = ", error];
f[x] = Exp[x^2]
Approximate Integral Value = 1.4627
Integrate: Invalid integration variable or limit(s) in {{0, 0, 0}, 0, 1}.
NIntegrate: Tag List in {0, 0, 0} is Protected.
NIntegrate: Tag List in {0, 0, 0} is Protected.
NIntegrate: Tag List in {0, 0, 0} is Protected.
General: Further output of NIntegrate::write will be suppressed during this calculation.
True Integral value = {NIntegrate[1, {{0, 0, 0}, 0, 1}],
NIntegrate[1, {{0, 0, 0}, 0, 1}], NIntegrate[1, {{0, 0, 0}, 0, 1}]}
NIntegrate: Tag List in {0, 0, 0} is Protected.
NIntegrate: Tag List in {0, 0, 0} is Protected.
NIntegrate: Tag List in {0, 0, 0} is Protected.
General: Further output of NIntegrate::write will be suppressed during this calculation.
Error = {-1.4627 + NIntegrate[1, {{0, 0, 0}, 0, 1}],
-1.4627 + NIntegrate[1, {{0, 0, 0}, 0, 1}], -1.4627 + NIntegrate[1, {{0, 0, 0}, 0, 1}]}