I n [ ] : = f[x_] := x3 - 2 * x - 5;
a = 1;
b = 2;
f[1];
f[2];
a+b
Doc = ;
2
If[f[a] * f[c] < 0, a = c, b = c];
Print["Approximate root after ", i, " iterations is ", N[c]], {i, 1, 10}
Approximate root after 1 iterations is 1.5
Approximate root after 2 iterations is 1.25
Approximate root after 3 iterations is 1.125
Approximate root after 4 iterations is 1.0625
Approximate root after 5 iterations is 1.03125
Approximate root after 6 iterations is 1.01563
Approximate root after 7 iterations is 1.00781
Approximate root after 8 iterations is 1.00391
Approximate root after 9 iterations is 1.00195
Approximate root after 10 iterations is 1.00098
Regula Falsi Method
g[z_] := z2 + 3 * z - 5;
a = 1;
b=2;
f[1];
f[2];
a * g[b] - b * g[a]
Doc = ;
g[b] - g[a]
If[g[c] * g[a] < 0, a = c, b = c];
Print["Approximate root after ", i, " iterations is ", N[c]], {i, 1, 10}
2 All formulas imp_Numerical [Link]
Approximate root after 1 iterations is 1.16667
Approximate root after 2 iterations is 1.19355
Approximate root after 3 iterations is 1.19258
Approximate root after 4 iterations is 1.19258
Approximate root after 5 iterations is 1.19258
Approximate root after 6 iterations is 1.19258
Approximate root after 7 iterations is 1.19258
Approximate root after 8 iterations is 1.19258
Approximate root after 9 iterations is 1.19258
Approximate root after 10 iterations is 1.19258
Secant method
I n [ ] : = f[x_] := x2 + 3 * x - 5;
a = 1;
b = 2;
f[1];
f[2];
a * f[b] - b * f[a]
Doc = ;
f[b] - f[a]
Print["Approximate root after ", i , "Iterations is ", N[c]];
a = b;
b = c;
, {i, 1, 10}
Approximate root after 1Iterations is 1.16667
Approximate root after 2Iterations is 1.18919
Approximate root after 3Iterations is 1.1926
Approximate root after 4Iterations is 1.19258
Approximate root after 5Iterations is 1.19258
Approximate root after 6Iterations is 1.19258
Approximate root after 7Iterations is 1.19258
Approximate root after 8Iterations is 1.19258
Approximate root after 9Iterations is 1.19258
Approximate root after 10Iterations is 1.19258
Newton Raphson method
All formulas imp_Numerical [Link] 3
I n [ ] : = f[z_ ] := z3 - 2 * z - 5
df[z_] = D[f[z], z]
x=2
f[x]
Dox = x - ;
df[x]
Print["Iteration ", i, ":", N[x]], {i, 1, 5}
O u t [ ] =
-2 + 3 z2
O u t [ ] =
2
Iteration 1:2.1
Iteration 2:2.09457
Iteration 3:2.09455
Iteration 4:2.09455
Iteration 5:2.09455
Gauss Jacobi Method
4 All formulas imp_Numerical [Link]
I n [ ] : = A = {{4, 1, 1}, {1, 5, 2} , {1, 2, 4}};
b = {2, -6, -4};
D1 = DiagonalMatrix[Diagonal[A]];
L = LowerTriangularize[A, -1];
U = UpperTriangularize[A, +1];
x0 = {0, 0, 0};
lhs = (D1 + L);
rhs = -U.x0 + b;
x1 = LinearSolve[N[lhs], N[rhs]];
n = 1;
ϵ = 10-8
Norm[xn - xn-1 ]
While > ϵ, n ++;
Norm[xn ]
lhs = (D1 + L);
rhs = -[Link]-1 + b;
xn = LinearSolve[N[lhs], N[rhs]];
Print["Approximate values after ", i, " Iterations is ", N[x n ]];
s = Table[xi , {i, 0, n}];
TableForm[s, TableHeadings {Automatic, {"x1 ", "x2 ", "x3 "}}]
O u t [ ] =
1
100 000 000
Approximate values after i Iterations is {0.949153, -1.11864, -0.677966}
O u t [ ] / / T a b l e F o r m =
x1 x2 x3
1 0 0 0
2 0.5 -1.3 -0.475
3 0.94375 -1.19875 -0.636562
4 0.958828 -1.13714 -0.671137
5 0.952069 -1.12196 -0.677038
6 0.949749 -1.11913 -0.67787
7 0.949251 -1.1187 -0.677962
8 0.949166 -1.11865 -0.677967
9 0.949154 -1.11864 -0.677967
10 0.949153 -1.11864 -0.677966
11 0.949153 -1.11864 -0.677966
12 0.949153 -1.11864 -0.677966
13 0.949153 -1.11864 -0.677966
Euler’s method
All formulas imp_Numerical [Link] 5
I n [ ] : = f[x_, y_] := x + y;
x = 0.0;
y = 1.0;
h = 0.1;
steps = 5;
Print["Start: x=", x, ", y=", y];
Do[slope = f[x, y];
y = y + h * slope;
x = x + h;
Print["Step ", i, ": x = ", N[x, 3], ", y = ", N[y, 6]], {i, 1, steps}]
Start: x=0., y=1.
Step 1: x = 0.1, y = 1.1
Step 2: x = 0.2, y = 1.22
Step 3: x = 0.3, y = 1.362
Step 4: x = 0.4, y = 1.5282
Step 5: x = 0.5, y = 1.72102
Practice(Euler’s method)
I n [ ] : = f[x_, y_] := x + y;
x = 0.0;
y = 0.1;
h = 0.1;
steps = 5;
Print["Start with x: ", x , ", y:", y ];
Do[slope = f[x, y];
y = y + h * slope;
x = x + h;
Print["Step: ", i, " x = ", N[x, 3], ", y= ", N[y, 6]], {i, 1, steps}]
Start with x: 0., y:0.1
Step: 1 x = 0.1, y= 0.11
Step: 2 x = 0.2, y= 0.131
Step: 3 x = 0.3, y= 0.1641
Step: 4 x = 0.4, y= 0.21051
Step: 5 x = 0.5, y= 0.271561
Runge kutta method
6 All formulas imp_Numerical [Link]
f[x_, y_] := x + y;
x = 0;
y = 1;
h = 0.1;
Print["Start with x=", x , ", y=", y]
Do
k1 = h * f[x, y];
h h
k2 = h * fx + , y + * k1 ;
2 2
h h
k3 = h * fx + , y + * k2 ;
2 2
k4 = h * f[x + h, y + k3 ];
(k1 + 2 * k2 + 2 * k3 + k4 )
y= y + ;
6
x = x + h;
Print["Step ", i, " x: ", N[x, {Infinity, 1}], ", y: ", N[y, 6]], {i, 1, 5}
Start with x=0, y=1
Step 1 x: 0.1, y: 1.01071
Step 2 x: 0.2, y: 1.02255
Step 3 x: 0.3, y: 1.03553
Step 4 x: 0.4, y: 1.04966
Step 5 x: 0.5, y: 1.06496
Lagrange Interpolation
I n [ ] : = data = {{0, 1}, {1, 3}, {2, 9}, {3, 25}};
xVal = data〚All, 1〛;
yVal = data〚All, 2〛;
n = Length[data];
poly = Sum
yVal〚j〛 * Product
(x - xVal〚i〛)
Ifi ≠ j, , 1, {i, 1, n}, {j, 1, n};
(xVal〚j〛 - xVal〚i〛)
Print["Raw Polynomial: "];
Print[poly];
Print["Simplified Polynomial: "];
Print[Simplify[poly]]
Raw Polynomial:
1 3 9 25
(1 - x) (2 - x) (3 - x) + (2 - x) (3 - x) x + (3 - x) (-1 + x) x + (-2 + x) (-1 + x) x
6 2 2 6
Simplified Polynomial:
1 + 2 x - x 2 + x3
All formulas imp_Numerical [Link] 7
Trapezoidal Rule
1
I n [ ] : = f[x_] := ;
1+x
a = 0;
b = 1;
n = 10;
(b - a)
h= ;
n
h n-1
integral = f[a] + 2 * f[a + j * h] + f[b] ;
2 j=1
1 1
exact = x;
1+x
0
Print["Approximate integral is:", N[integral]];
Print["exact integral is:", N[exact]];
Approximate integral is:0.693771
exact integral is:0.693147
Simpson’s rule
1
f[x_] := ;
1+x
a = 0;
b = 1;
n = 10;
(b - a)
h= ;
n
n
m= ;
2
h m m-1
integral = f[a] + 4 * f[a + (2 * j - 1) * h ] + 2 * f[a + (2 * j * h)] + f[b] ;
3 j=1 j=1
1 1
exact = x;
1+x
0
Print["Approximate integral is: ", N[integral]];
Print["exact integral is: ", N[exact]];
Approximate integral is: 0.69315
exact integral is: 0.693147
Practice (Bisection)
8 All formulas imp_Numerical [Link]
I n [ ] : = f[x_] := x2 - 5 x + 3;
a = 1;
b = 2;
f[1];
f[2];
a+b
Doc = ;
2
If[f[a] * f[c] < 0, a = c, b = c];
Print["Approximate root after ", i, " Iterations is ", N[c]], {i, 0, 10};
Print["Final approximate root is ", N[c]]
Approximate root after 0 Iterations is 1.5
Approximate root after 1 Iterations is 1.25
Approximate root after 2 Iterations is 1.125
Approximate root after 3 Iterations is 1.0625
Approximate root after 4 Iterations is 1.03125
Approximate root after 5 Iterations is 1.01563
Approximate root after 6 Iterations is 1.00781
Approximate root after 7 Iterations is 1.00391
Approximate root after 8 Iterations is 1.00195
Approximate root after 9 Iterations is 1.00098
Approximate root after 10 Iterations is 1.00049
Final approximate root is 1.00049
Practice(regula falsi)
I n [ ] : = f[x_] := x2 - 5 x + 3;
a = 1;
b = 2;
f[1];
f[2];
(a * f[b]) - (b * f[a])
Doc = ;
f[b] - f[a]
If[f[a] * f[c] < 0, a = c, b = c];
Print["Approximate root after", i, " Iterations is ", N[c]], {i, 0, 10};
Print["Approximate final root is ", N[c]]
All formulas imp_Numerical [Link] 9
Approximate root after0 Iterations is 0.5
Approximate root after1 Iterations is 0.8
Approximate root after2 Iterations is 0.636364
Approximate root after3 Iterations is 0.730769
Approximate root after4 Iterations is 0.677966
Approximate root after5 Iterations is 0.708029
Approximate root after6 Iterations is 0.691083
Approximate root after7 Iterations is 0.70069
Approximate root after8 Iterations is 0.695261
Approximate root after9 Iterations is 0.698334
Approximate root after10 Iterations is 0.696596
Approximate final root is 0.696596
Practice(Newton Ralph son)
I n [ ] : = f[z_] := z3 - 3 * z + 5
df[z_] = D[f[z], z]
x=2
f[x]
Dox = x - ;
df[x]
Print["Apprxoimate root after ", i, " iterations ", N[x]], {i, 0, 5};
Print["Approximate final root is ", N[x]]
O u t [ ] =
-3 + 3 z2
O u t [ ] =
2
Apprxoimate root after 0 iterations 1.22222
Apprxoimate root after 1 iterations -0.910185
Apprxoimate root after 2 iterations 12.6447
Apprxoimate root after 3 iterations 8.47234
Apprxoimate root after 4 iterations 5.70448
Apprxoimate root after 5 iterations 3.87072
Approximate final root is 3.87072
practice(trapezoidal rule)
10 All formulas imp_Numerical [Link]
1
In[10]:= f[x_] := ;
1+x
a = 0;
b = 1;
n = 10
b-a
h= ;
n
h n-1
integral = f[a] + 2 * f[a + (j * h)] + f[b] ;
2 j=1
1 1
exact = x;
1+x
0
Print["Approximate integral is ", N[integral]];
Print["exact integral is ", N[exact]];
Out[13]=
10
Approximate integral is 0.693771
exact integral is 0.693147
Practice(Simpsons 1/3 rule)