Practical Numerical Methods
Practical Numerical Methods
f[x_] := x ^ 3 + 2 * x ^ 2 - 3 * x - 1;
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., 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
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
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
-2
-3
-4
-5
6
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
-2
-3
-4
-5
In[131]:=
Out[133]=
Plot f [x]
-1
-2
-3
-4
-5
2.
-1.55556
-4.71429
4.19683
1.20635
-4.87302
3.70794
2.00917
-3.77007
9
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];];
2.
-1.55556
-4.71429
4.19683
1.20635
-4.87302
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
2.
-1.55556
-4.71429
4.19683
1.20635
-4.87302
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.]]]
(-7.)[33.]
9.[-14.]
5.[10.]
2.
-1.55556
4.19683
1.20635
-4.87302
3.70794
2.00917
-3.77007
f(x) = {1, 1, 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}
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];];
f[x] = Exp[x^2]