Shivam File
Shivam File
1. BISECTION METHOD
2. NEWTON-RAPHSON METHOD
3. SECANT METHOD
4. GAUSS-JACOBI METHOD
5. GAUSS-SEIDEL METHOD
7. LAGRANGE INTERPOLATION
8. NEWTON POLYNOMINAL
9. TRAPEZOIDAL RULE
REGULA FALSI
10.
Practical1(Bisection Method)
Question 1
In[1]:= f[x_] := x ^ 3 - 5 * x + 1;
Plotf[x], {x, - 1, 3} , PlotRange → {- 5, 5} ,
PlotStyle → Red, Thick, PlotLabel → "f[x]=" f[x], AxesLabel → {x, f[x]}
f[x]= x3 - 5 x + 1
3
1-5x +x
Out[2]=
x
-1 1 2 3
-2
-4
In[13]:= x0 = 0;
x1 = 1;
Nmax = 20;
eps = 0.0001;
IfN[f[x0] * f[x1]] > 0,
Print"your value do not satisfy the IVP , so change the values" ,
For i = 1 , i ≤ Nmax , i ++ , m = (x0 + x1) / 2;
IfAbs[(x1 - x0) / 2] < eps , Return[N[m]], Print i, "th iteration value is :", N[m];
Print"estimated error in" , i, "the iteration is:", N[(x1 - x0) / 2];
Iff[m] * f[x1] > 0 , x1 = m, x0 = m;
Print"Root is :", m *
Print"estimated error in" , "the iteration is:", (x1 - x0) / 2
2
Question 2
f[x_] := Cos[x];
Plotf[x], {x, - 1, 3} , PlotRange → {- 5, 5} ,
PlotStyle → Red, Thick, PlotLabel → "f[x]=" f[x], AxesLabel → {x, f[x]}
Out[19]=
f[x]= cos(x)
cos(x)
x
-1 1 2 3
-2
-4
In[25]:= x0 = 1;
x1 = 2;
Nmax = 20;
eps = 0.0001;
IfN[f[x0] * f[x1]] > 0,
Print"your value do not satisfy the IVP , so change the values" ,
For i = 1 , i ≤ Nmax , i ++ , m = (x0 + x1) / 2;
IfAbs[(x1 - x0) / 2] < eps , Return[N[m]], Print i, "th iteration value is :", N[m];
Print"estimated error in" , i, "the iteration is:", N[(x1 - x0) / 2];
Iff[m] * f[x1] > 0 , x1 = m, x0 = m;
Print"Root is :", m *
Print"estimated error in" , "the iteration is:", (x1 - x0) / 2
4
Question 3
In[30]:= f[x_] := Cos[x] - x * Exp[x];
Plotf[x], {x, - 1, 3} , PlotRange → {- 5, 5} ,
PlotStyle → Red, Thick, PlotLabel → "f[x]=" f[x], AxesLabel → {x, f[x]}
Out[31]=
f[x]= cos(x) - ⅇx x
x
-ⅇ x + cos(x)
x
-1 1 2 3
-2
-4
In[32]:= x0 = 0;
x1 = 1;
Nmax = 20;
eps = 0.0001;
IfN[f[x0] * f[x1]] > 0,
Print"your value do not satisfy the IVP , so change the values" ,
For i = 1 , i ≤ Nmax , i ++ , m = (x0 + x1) / 2;
IfAbs[(x1 - x0) / 2] < eps , Return[N[m]], Print i, "th iteration value is :", N[m];
Print"estimated error in" , i, "the iteration is:", N[(x1 - x0) / 2];
Iff[m] * f[x1] > 0 , x1 = m, x0 = m;
Print"Root is :", m *
Print"estimated error in" , "the iteration is:", (x1 - x0) / 2
6
1.0
0.5
x
-1 1 2 3
-0.5
-1.0
In[27]:= x0 = 1;
Nmax = 20;
eps = 10 ^ (- 6);
Print"f[x]=" , f[x];
Print"f[x]=" , D[f[x], x];
Fori = 1, i ≤ Nmax, i ++ , x1 = Nx0 - f[x] /. x → x0 D[f[x], x] /. x → x0;
If Absx1 - x0 < eps, Return[x1] , x0p = x0; x0 = x1;
Print "In", i , "th Number of iteration the approximation to root is:", x1;
Print "Estimated error is :", Abs[x1 - x0p];
Print"the final approximation of root is:" , x1;
Print "Estimated error is :", Abs[x1 - x0]
2
f[x]=Cos[x]
f[x]=- Sin[x]
In1th Number of iteration the approximation to root is:1.64209
Estimated error is :0.642093
In2th Number of iteration the approximation to root is:1.57068
Estimated error is :0.0714173
In3th Number of iteration the approximation to root is:1.5708
Estimated error is :0.00012105
Out[32]=
Return[1.5708]
Question2
In[35]:= f[x_] = x ^ 3 - 5 * x + 1;
Plotf[x] , {x, - 1, 3} , PlotStyle → Red, Thick ,
AxesLabel → {x, f[x]} , PlotLabel → "f[x]=" f[x]
Out[36]=
f[x]= x3 - 5 x + 1
3
1-5x +x
10
x
-1 1 2 3
3
In[37]:= x0 = 0.5;
Nmax = 20;
eps = 10 ^ (- 6);
Print"f[x]=" , f[x];
Print"f[x]=" , D[f[x], x];
Fori = 1, i ≤ Nmax, i ++ , x1 = Nx0 - f[x] /. x → x0 D[f[x], x] /. x → x0;
If Absx1 - x0 < eps, Return[x1] , x0p = x0; x0 = x1;
Print "In", i , "th Number of iteration the approximation to root is:", x1;
Print "Estimated error is :", Abs[x1 - x0p];
Print"the final approximation of root is:" , x1;
Print "Estimated error is :", Abs[x1 - x0]
f[x]=1 - 5 x + x3
f[x]=- 5 + 3 x2
In1th Number of iteration the approximation to root is:0.176471
Estimated error is :0.323529
In2th Number of iteration the approximation to root is:0.201568
Estimated error is :0.0250975
In3th Number of iteration the approximation to root is:0.20164
Estimated error is :0.0000716007
Out[42]=
Return[0.20164]
Question 3
In[45]:= f[x_] = Cos[x] - x * Exp[x];
Plotf[x] , {x, - 1, 3} , PlotStyle → Red, Thick ,
AxesLabel → {x, f[x]} , PlotLabel → "f[x]=" f[x]
Out[46]=
f[x]= cos(x) - ⅇx x
x
-ⅇ x + cos(x)
x
-1 1 2 3
-10
-20
-30
-40
-50
-60
In[55]:= x0 = 1.5;
Nmax = 20;
eps = 10 ^ (- 6);
Print"f[x]=" , f[x];
Print"f[x]=" , D[f[x], x];
Fori = 1, i ≤ Nmax, i ++ , x1 = Nx0 - f[x] /. x → x0 D[f[x], x] /. x → x0;
If Absx1 - x0 < eps, Return[x1] , x0p = x0; x0 = x1;
Print "In", i , "th Number of iteration the approximation to root is:", x1;
Print "Estimated error is :", Abs[x1 - x0p];
Print"the final approximation of root is:" , x1;
Print "Estimated error is :", Abs[x1 - x0]
5
f[x]=-ⅇx x + Cos[x]
f[x]=-ⅇx - ⅇx x - Sin[x]
In1th Number of iteration the approximation to root is:0.954848
Estimated error is :0.545152
In2th Number of iteration the approximation to root is:0.632019
Estimated error is :0.322829
In3th Number of iteration the approximation to root is:0.527616
Estimated error is :0.104403
In4th Number of iteration the approximation to root is:0.517838
Estimated error is :0.00977784
In5th Number of iteration the approximation to root is:0.517757
Estimated error is :0.0000806043
Out[60]=
Return[0.517757]
1.0
0.5
-1 1 2 3
-0.5
-1.0
In[65]:= x0 = 1;
x1 = 2;
Nmax = 20;
eps = 10 ^ (- 6);
Print["f[x]=", f[x]];
Fori = 1, i ≤ Nmax, i ++,
x2 = Nx1 - f[x] /. x → x1 * (x1 - x0) f[x] /. x → x1 - f[x] /. x → x0;
IfAbsx1 - x2 < eps , Return[x2] , x0 = x1; x1 = x2;
Print"In", i, "th Number of iterations the root is :", x2;
Print"estimated error is :", Abs[x1 - x0];
Print"root is :" , x2;
Print"estimated error is:", Abs[x2 - x1];
2
f[x]=Cos[x]
In1th Number of iterations the root is :1.5649
estimated error is :0.435096
In2th Number of iterations the root is :1.57098
estimated error is :0.0060742
In3th Number of iterations the root is :1.5708
estimated error is :0.000182249
Out[70]=
Return[1.5708]
root is :1.5708
estimated error is:1.02185 × 10-9
Question 2
In[73]:= f[x_] = x^3 - 5*x + 1;
Plot f[x], x , - 1, 3
Out[74]=
10
-1 1 2 3
In[75]:= x0 = 0;
x1 = 1;
Nmax = 20;
eps = 10 ^ (- 6);
Print["f[x]=", f[x]];
Fori = 1, i ≤ Nmax, i ++,
x2 = Nx1 - f[x] /. x → x1 * (x1 - x0) f[x] /. x → x1 - f[x] /. x → x0;
IfAbsx1 - x2 < eps , Return[x2] , x0 = x1; x1 = x2;
Print"In", i, "th Number of iterations the root is :", x2;
Print"estimated error is :", Abs[x1 - x0];
Print"root is :" , x2;
Print"estimated error is:", Abs[x2 - x1];
3
f[x]=1 - 5 x + x3
In1th Number of iterations the root is :0.25
estimated error is :0.75
In2th Number of iterations the root is :0.186441
estimated error is :0.0635593
In3th Number of iterations the root is :0.201736
estimated error is :0.0152956
In4th Number of iterations the root is :0.20164
estimated error is :0.0000964033
Out[80]=
Return[0.20164]
root is :0.20164
estimated error is:1.7717 × 10-7
Question 3
In[83]:= f[x_] = Cos[x] - x * Exp[x];
Plot f[x], x , - 1, 3
Out[84]=
-1 1 2 3
-10
-20
-30
-40
-50
-60
4
In[85]:= x0 = 0;
x1 = 1;
Nmax = 20;
eps = 10 ^ (- 6);
Print["f[x]=", f[x]];
Fori = 1, i ≤ Nmax, i ++,
x2 = Nx1 - f[x] /. x → x1 * (x1 - x0) f[x] /. x → x1 - f[x] /. x → x0;
IfAbsx1 - x2 < eps , Return[x2] , x0 = x1; x1 = x2;
Print"In", i, "th Number of iterations the root is :", x2;
Print"estimated error is :", Abs[x1 - x0];
Print"root is :" , x2;
Print"estimated error is:", Abs[x2 - x1];
f[x]=-ⅇx x + Cos[x]
In1th Number of iterations the root is :0.314665
estimated error is :0.685335
In2th Number of iterations the root is :0.446728
estimated error is :0.132063
In3th Number of iterations the root is :0.531706
estimated error is :0.0849777
In4th Number of iterations the root is :0.516904
estimated error is :0.0148014
In5th Number of iterations the root is :0.517747
estimated error is :0.000842998
In6th Number of iterations the root is :0.517757
estimated error is :9.90548 × 10-6
Out[90]=
Return[0.517757]
root is :0.517757
estimated error is:7.07182 × 10-9
Practical 5(Gauss-Jacobi Method)
Question 1
GaussJacobi[A0_ , b0_ , X0_ , maxiter_] :=
ModuleA = N[A0], b = N[b0], xk = X0, xk1, i, j, k = 0, n, m, OutputDetails,
size = Dimensions[A];
n = size〚1〛;
m = size〚2〛;
Ifn ≠ m,
Print "Not a sqaure matrix , cannot proceed with gauss jacobi method ";
Return[];
OutputDeatils = {xk};
Question 2
In[6]:= GaussJacobi[A0_ , b0_ , X0_ , maxiter_] :=
ModuleA = N[A0], b = N[b0], xk = X0, xk1, i, j, k = 0, n, m, OutputDetails,
size = Dimensions[A];
n = size〚1〛;
m = size〚2〛;
Ifn ≠ m,
Print "Not a sqaure matrix , cannot proceed with gauss jacobi method ";
Return[];
OutputDeatils = {xk};
Question 3
In[11]:= GaussJacobi[A0_ , b0_ , X0_ , maxiter_] :=
ModuleA = N[A0], b = N[b0], xk = X0, xk1, i, j, k = 0, n, m, OutputDetails,
size = Dimensions[A];
n = size〚1〛;
m = size〚2〛;
Ifn ≠ m,
Print "Not a sqaure matrix , cannot proceed with gauss jacobi method ";
Return[];
OutputDeatils = {xk};
Question 2
In[36]:= GaussSeidel[A0_ , b0_ , X0_ , maxiter_] :=
ModuleA = N[A0], b = N[b0], xk = X0, xk1, i, j, k = 0, n, m, OutputDetails,
size = Dimensions[A];
n = size〚1〛;
m = size〚2〛;
Ifn ≠ m,
Print "Not a sqaure matrix , cannot proceed with gauss seidel method ";
Return[];
OutputDeatils = {xk};
Question 3
In[41]:= GaussSeidel[A0_ , b0_ , X0_ , maxiter_] :=
ModuleA = N[A0], b = N[b0], xk = X0, xk1, i, j, k = 0, n, m, OutputDetails,
size = Dimensions[A];
n = size〚1〛;
m = size〚2〛;
Ifn ≠ m,
Print "Not a sqaure matrix , cannot proceed with gauss seidel method ";
Return[];
OutputDeatils = {xk};
Question 1
In[49]:= nodes = {0, 1, 3};
values = {1, 3, 55};
lagrangePolynomial[x_] = LagrangePolynomial[nodes, values]
Out[51]=
1 3 55
(1 - x) (3 - x) + (3 - x) x + (- 1 + x) x
3 2 6
In[52]:= Expand[%]
Out[52]=
1 - 6 x + 8 x2
Question 2:
In[53]:= nodes = {0, 1, 3};
values = {1, 3};
lagrangePolynomial[x_] = LagrangePolynomial[nodes, values]
Question3:
In[56]:= nodes = {1, 3, 5, 7, 9};
values = {N[Log[1]], N[Log[3]], N[Log[5]], N[Log[7]], N[Log[9]]};
lagrangePolynomial[x_] = LagrangePolynomial[nodes, values]
Out[58]=
0. + 0.0114439 (5 - x) (7 - x) (9 - x) (- 1 + x) + 0.0251475 (7 - x) (9 - x) (- 3 + x) (- 1 + x) +
0.0202699 (9 - x) (- 5 + x) (- 3 + x) (- 1 + x) + 0.00572194 (- 7 + x) (- 5 + x) (- 3 + x) (- 1 + x)
In[59]:= Simplify[%]
Out[59]=
- 0.987583 + 1.18991 x - 0.223608 x2 + 0.0221231 x3 - 0.000844369 x4
lagrangePolynomial(x)
1 log(x)
2 3 4 5 6 7 8 9 10
Question 4:
In[61]:= nodes = {- 1, 0, 1, 2};
values = {5, 1, 1, 11};
lagrangePolynomial[x_] = LagrangePolynomial[nodes, values]
Out[63]=
5 1 1 11
- (1 - x) (2 - x) x + (1 - x) (2 - x) (1 + x) + (2 - x) x (1 + x) + (- 1 + x) x (1 + x)
6 2 2 6
In[64]:= Simplify[%]
Out[64]=
1 - 3 x + 2 x2 + x3
3
In[65]:= lagrangePolynomial[1.5]
Out[65]=
4.375
In[1]:= sum = 0;
points = {{3, 293 }, {5, 508 }, {6, 585 }, {9, 764 }};
n = Length [points ]
y = points [[All, 1]]
f = points [[All, 2]]
dd [k _] :=
Sum [(f[[i]] / Product [If [Equal [j, i], 1, (y[[i]] - y[[j]])], {j, 1, k}]), {i, 1, k}]
P[x_] = Sum [(dd [i] * Product [If [i ≤ j, 1, x - y[[j]]], {j, 1, i - 1}]), {i, 1, n}]
Simplify [P[x]]
Evaluate [P[2.5 ]]
Out[3]= 4
Out[4]= {3, 5, 6, 9}
215 61 35
Out[7]= 293 + (- 3 + x) - (- 5 + x) (- 3 + x) + (- 6 + x) (- 5 + x) (- 3 + x)
2 6 36
1 2 3
Out[8]= - 9702 + 9003 x - 856 x + 35 x
36
222.288
In[11]:= ClearAll
Out[11]= ClearAll
2 newton polynomial.nb
In[12]:= sum = 0;
points = {{1, 1}, {2, 5}, {3, 2}};
n = Length [points ]
y = points [[All, 1]]
f = points [[All, 2]]
dd [k _] :=
Sum [(f[[i]] / Product [If [Equal [j, i], 1, (y[[i]] - y[[j]])], {j, 1, k}]), {i, 1, k}]
P[x_] = Sum [(dd [i] * Product [If [i ≤ j, 1, x - y[[j]]], {j, 1, i - 1}]), {i, 1, n}]
Simplify [P[x]]
Evaluate [P[9.2 ]]
Out[14]= 3
Out[15]= {1, 2, 3}
Out[16]= {1, 5, 2}
7
Out[18]= 1 + 4 (- 1 + x) - (- 2 + x) (- 1 + x)
2
1 2
Out[19]= - 20 + 29 x - 7 x
2
Out[20]= - 172.84
ClearAll
In[21]:= sum = 0;
points = {{0, 1}, {1, 7}, {2, 23 }, {3, 55 }, {4, 109 }};
n = Length [points ]
y = points [[All, 1]]
f = points [[All, 2]]
dd [k _] :=
Sum [(f[[i]] / Product [If [Equal [j, i], 1, (y[[i]] - y[[j]])], {j, 1, k}]), {i, 1, k}]
P[x_] = Sum [(dd [i] * Product [If [i ≤ j, 1, x - y[[j]]], {j, 1, i - 1}]), {i, 1, n}]
Simplify [P[x]]
Evaluate [P[10 ]]
Out[23]= 5
Out[24]= {0, 1, 2, 3, 4}
Out[27]= 1 + 6 x + 5 (- 1 + x) x + (- 2 + x) (- 1 + x) x
Out[28]= 1 + 3 x + 2 x2 + x3
Out[29]= 1231
newton polynomial.nb 3
In[32]:= ClearAll
sum = 0;
points = {{4, 48 }, {5, 100 }, {7, 294 }, {10, 900 }};
n = Length [points ]
y = points [[All, 1]]
f = points [[All, 2]]
dd [k _] :=
Sum [(f[[i]] / Product [If [Equal [j, i], 1, (y[[i]] - y[[j]])], {j, 1, k}]), {i, 1, k}]
P[x_] = Sum [(dd [i] * Product [If [i ≤ j, 1, x - y[[j]]], {j, 1, i - 1}]), {i, 1, n}]
Simplify [P[x]]
Evaluate [P[0]]
Out[32]= ClearAll
Out[35]= 4
Out[36]= {4, 5, 7, 10 }
Out[39]= 48 + 52 (- 4 + x) + 15 (- 5 + x) (- 4 + x) + (- 7 + x) (- 5 + x) (- 4 + x)
2
Out[40]= (- 1 + x) x
Out[41]= 0
Practical 9(Trapezoidal Method)
Question 1:
In[2]:= a = Input"Enter the left end point:";
b = Input"Enter the right end point:";
n = Input"Enter the number of sub-intervals to be formed:";
h = (b - a) n;
y = Tablea + i * h, i, 1, n;
f[x] := Log[x];
sumodd = 0;
sumeven = 0;
Fori = 1, i < n, i += 2, sumodd += 2 * f[x] /. x → yi;
Fori = 2, i < n, i += 2, sumeven += 2 * f[x] /. x → yi;
Tn = h 2 * f[x] /. x → a + N[sumodd] + N[sumeven] + f[x] /. x → b;
Print"For n=", n, ",Trapezoidal estimate is: ", Tn
in = Integrate[Log[x], {x, 4, 5.2}];
Print"True value is ", in
Print"Absolute error is ", Abs[Tn - in]
Question 2:
In[17]:= a = Input"Enter the left end point:";
b = Input"Enter the right end point:";
n = Input"Enter the number of sub-intervals to be formed:";
h = (b - a) n;
y = Tablea + i * h, i, 1, n;
f[x] := Sin[x];
sumodd = 0;
sumeven = 0;
Fori = 1, i < n, i += 2, sumodd += 2 * f[x] /. x → yi;
Fori = 2, i < n, i += 2, sumeven += 2 * f[x] /. x → yi;
Tn = h 2 * f[x] /. x → a + N[sumodd] + N[sumeven] + f[x] /. x → b;
Print"For n=", n, ",Trapezoidal estimate is: ", Tn
in = IntegrateSin[x], x, 0, Pi 2;
Print"True value is ", in
Print"Absolute error is ", Abs[Tn - in]
Question 3:
In[32]:= a = Input"Enter the left end point:";
b = Input"Enter the right end point:";
n = Input"Enter the number of sub-intervals to be formed:";
h = (b - a) n;
y = Tablea + i * h, i, 1, n;
f[x] := Sin[x] - Log[x] + Exp[x];
sumodd = 0;
sumeven = 0;
Fori = 1, i < n, i += 2, sumodd += 2 * f[x] /. x → yi;
Fori = 2, i < n, i += 2, sumeven += 2 * f[x] /. x → yi;
Tn = h 2 * f[x] /. x → a + N[sumodd] + N[sumeven] + f[x] /. x → b;
Print"For n=", n, ",Trapezoidal estimate is: ", Tn
in = Integrate[Sin[x] - Log[x] + Exp[x], {x, 0.2, 1.4}];
Print"True value is ", in
Print"Absolute error is ", Abs[Tn - in]
Question 4:
In[47]:= a = Input"Enter the left end point:";
b = Input"Enter the right end point:";
n = Input"Enter the number of sub-intervals to be formed:";
h = (b - a) n;
y = Tablea + i * h, i, 1, n;
f[x] := 1 / (1 + x ^ 2);
sumodd = 0;
sumeven = 0;
Fori = 1, i < n, i += 2, sumodd += 2 * f[x] /. x → yi;
Fori = 2, i < n, i += 2, sumeven += 2 * f[x] /. x → yi;
Tn = h 2 * f[x] /. x → a + N[sumodd] + N[sumeven] + f[x] /. x → b;
Print"For n=", n, ",Trapezoidal estimate is: ", Tn
in = Integrate[1 / (1 + x ^ 2), {x, 0, 1}];
Print"True value is ", in
Print"Absolute error is ", Abs[Tn - in]
In[7]:= ClearAll
Out[7]= ClearAll
c =0.860357511345688
-12
f[c] = -5.123901303250022 × 10
In[11]:= ClearAll
Out[11]=
ClearAll
In[15]:= ClearAll
Out[15]=
ClearAll