Numerical Methods and Programming Using Mathematica
Numerical Methods and Programming Using Mathematica
Numerical Methods and Programming Using Mathematica
f@x_D := Cos@xD;
x0 = 0.0; x1 = 2.0; n = 14;
f1 = Sign@f@x1DD;
If@f@x0D * f1 > 0,
Print@"IVP not satisfied"D,
For@i = 1, i n, i ++, p = Hx0 + x1L 2;
Print@i, " th iteration value is: ", pD;
pSign = Sign@f@pDD;
If@pSign * f1 < 0, x0 = p, x1 = p; f1 = pSignD;
Print@"The new interval is: ", "H", x0, ",", x1, "L"D;
Print@".........................................."D;
D;
Print@"Final Approximation is ", pD
D
1 th iteration value is: 1.
..........................................
2 th iteration value is: 1.5
The new interval is: H1.5,2.L
..........................................
3 th iteration value is: 1.75
The new interval is: H1.5,1.75L
..........................................
4 th iteration value is: 1.625
The new interval is: H1.5,1.625L
..........................................
5 th iteration value is: 1.5625
The new interval is: H1.5625,1.625L
..........................................
6 th iteration value is: 1.59375
The new interval is: H1.5625,1.59375L
..........................................
7 th iteration value is: 1.57813
The new interval is: H1.5625,1.57813L
..........................................
8 th iteration value is: 1.57031
The new interval is: H1.57031,1.57813L
..........................................
9 th iteration value is: 1.57422
The new interval is: H1.57031,1.57422L
..........................................
10 th iteration value is: 1.57227
The new interval is: H1.57031,1.57227L
..........................................
11 th iteration value is: 1.57129
The new interval is: H1.57031,1.57129L
..........................................
12 th iteration value is: 1.5708
The new interval is: H1.57031,1.5708L
..........................................
13 th iteration value is: 1.57056
The new interval is: H1.57056,1.5708L
..........................................
14 th iteration value is: 1.57068
The new interval is: H1.57068,1.5708L
..........................................
Final Approximation is 1.57068
Clear@fD;
f@x_D := Cos@xD;
x0 = 0.0; x1 = 2.0; n = 20;
f1 = Sign@f@x1DD;
If@f@x0D * f1 > 0,
Print@"IVP not satisfied"D,
For@i = 1, i n, i ++,
p = Hx0 + x1L 2.0;
Print@i, " th iteration value is ", pD;
pSign = Sign@f@pDD;
Print@"The error is ", Abs@x1 - x0D 2.0D;
If@Abs@x1 - x0D < 2 * 0.0001, Break@DD;
If@pSign * f1 < 0, x0 = p, x1 = p; f1 = pSignD;
Print@"The root enclosing interval is: ", "H", x0, ",", x1, "L"D;
Print@"....................................................."D;
D;
Print@"....................................................."D;
Print@"Final approximation is: ", pD;
D
1 th iteration value is 1.
The error is 1.
The root enclosing interval is: H1.,2.L
.....................................................
2 th iteration value is 1.5
The error is 0.5
.....................................................
3 th iteration value is 1.75
The error is 0.25
The root enclosing interval is: H1.5,1.75L
.....................................................
4 th iteration value is 1.625
The error is 0.125
The root enclosing interval is: H1.5,1.625L
.....................................................
5 th iteration value is 1.5625
The error is 0.0625
The root enclosing interval is: H1.5625,1.625L
.....................................................
6 th iteration value is 1.59375
The error is 0.03125
The root enclosing interval is: H1.5625,1.59375L
.....................................................
7 th iteration value is 1.57813
The error is 0.015625
The root enclosing interval is: H1.5625,1.57813L
.....................................................
8 th iteration value is 1.57031
The error is 0.0078125
The root enclosing interval is: H1.57031,1.57813L
.....................................................
9 th iteration value is 1.57422
The error is 0.00390625
The root enclosing interval is: H1.57031,1.57422L
.....................................................
10 th iteration value is 1.57227
The error is 0.00195313
The root enclosing interval is: H1.57031,1.57227L
.....................................................
11 th iteration value is 1.57129
The error is 0.000976563
The root enclosing interval is: H1.57031,1.57129L
.....................................................
12 th iteration value is 1.5708
.....................................................
13 th iteration value is 1.57056
The error is 0.000244141
The root enclosing interval is: H1.57056,1.5708L
.....................................................
14 th iteration value is 1.57068
The error is 0.00012207
The root enclosing interval is: H1.57068,1.5708L
.....................................................
15 th iteration value is 1.57074
The error is 0.0000610352
.....................................................
Final approximation is: 1.57074
In[97]:=
f@x_D := Cos@xD;
x0 = 0.0; x1 = 2.0;
f1 = Sign@f@x1DD;
If@f@x0D * f1 > 0, Print@"IVP not satisfied"D,
For@i = 1, Abs@x1 - x0D > 2 * 0.0001, i ++,
p = Hx1 + x0L 2.0;
Print@i, " th iteration approximation is ",
p, " in interval ", "H", x0, ",", x1, "L"D;
Print@"Error bound is ", Abs@x1 - x0D 2.0D;
Print@"..............................................................."D;
pSign = Sign@f@pDD;
If@pSign * f1 < 0, x0 = p, x1 = p, f1 = pSignD;
D;
Print@"Final approximation is: ", p, " with error ", Abs@x1 - x0D 2.0D
D;
1 th iteration approximation is 1. in interval H0.,2.L
Error bound is 1.
...............................................................
2 th iteration approximation is 1.5 in interval H1.,2.L
Error bound is 0.5
...............................................................
3 th iteration approximation is 1.75 in interval H1.5,2.L
Error bound is 0.25
...............................................................
...............................................................
5 th iteration approximation is 1.5625 in interval H1.5,1.625L
Error bound is 0.0625
...............................................................
6 th iteration approximation is 1.59375 in interval H1.5625,1.625L
Error bound is 0.03125
...............................................................
7 th iteration approximation is 1.57813 in interval H1.5625,1.59375L
Error bound is 0.015625
...............................................................
8 th iteration approximation is 1.57031 in interval H1.5625,1.57813L
Error bound is 0.0078125
...............................................................
9 th iteration approximation is 1.57422 in interval H1.57031,1.57813L
Error bound is 0.00390625
...............................................................
10 th iteration approximation is 1.57227 in interval H1.57031,1.57422L
Error bound is 0.00195313
...............................................................
11 th iteration approximation is 1.57129 in interval H1.57031,1.57227L
Error bound is 0.000976563
...............................................................
12 th iteration approximation is 1.5708 in interval H1.57031,1.57129L
Error bound is 0.000488281
...............................................................
13 th iteration approximation is 1.57056 in interval H1.57031,1.5708L
Error bound is 0.000244141
...............................................................
14 th iteration approximation is 1.57068 in interval H1.57056,1.5708L
Error bound is 0.00012207
...............................................................
Final approximation is: 1.57068 with error 0.0000610352
Newtons Method
In[122]:=
x0 = 1.0;
Nmax = 5; eps = 0.0001;
f@x_D := Cos@xD;
For@i = 1, i Nmax, i ++,
x1 = N@x0 - Hf@x0DL Hf '@x0DLD;
If @Abs@x1 - x0D < eps, Break@D,
Print@i, " th iteration is: ", x1D;
Print@"Estimated error is: ", Abs@x1 - x0DD;
Print@"__________________________________"D;
x0 = x1D;
D;
Print@"Final approximation is: ", x1D
1 th iteration is: 1.64209
x0 = 1.0;
eps = 0.0001;
f@x_D := Cos@xD;
x1 = N@x0 - f@x0D f '@x0DD;
Print@1, " th iteration approximation is: ", x1D;
For@i = 2, Abs@x1 - x0D > eps, i ++,
x0 = x1;
x1 = N@x0 - f@x0D f '@x0DD;
Print@i, " th iteration approximation is: ", x1D
D;
Print@"Final approximation is:
", x1D
1 th iteration approximation is: 1.64209
2 th iteration approximation is: 1.57068
3 th iteration approximation is: 1.5708
4 th iteration approximation is: 1.5708
Final approximation is:
1.5708
In[152]:=
Out[155]=
Out[158]=
2.
- 1.55556
4.71429
0.425397
- 2.98413
4.55556
0.774603
- 3.43845
3.92245
1.11871
- 3.04067
3.84253
1.07112
- 2.89044
4.00534
0.975953
- 2.97867
4.04146
0.979148
- 3.02644
4.00266
1.00422
- 3.00813
3.98947
1.00584
- 2.99391
3.99828
0.99947
- 2.99729
4.00257
0.998428
- 3.00132
4.0007
0.999985
- 3.00083
3.9994
1.00041
- 2.99974
3.99976
1.00004
- 2.99976
4.00013
In[263]:=
2.
- 0.888889
4.74603
2 th approximation is:
0.279365
- 3.57178
3.73369
3 th approximation is:
1.22088
- 2.80801
4.08641
4 th approximation is:
0.927039
- 3.06272
3.97166
5 th approximation is:
1.02388
- 2.97944
4.00929
6 th approximation is:
0.992174
- 3.00674
3.99696
7 th approximation is:
1.00256
- 2.99779
4.001
8 th approximation is:
0.99916
- 3.00072
3.99967
9 th approximation is:
1.00028
- 2.99976
4.00011
10 th approximation is:
89.9994, - 14.0006, - 33.<
Clear@fD
0.99991
- 3.00008
3.99996
10
In[299]:=
f@x_D := x ^ 3 + 2 x ^ 2 - 3 x - 1;
x0 = 1.0; x1 = 2.0;
nMax = 10;
f1 = Sign@f@x1DD;
If@f@x0D * f1 > 0, Print@"IVP not satisfied."D,
For@i = 1, i nMax, i ++,
p = N@x1 - f@x1D * HHx1 - x0L Hf@x1D - f@x0DLL, 7D;
Print@i, " th approximation is: ", pD;
pSign = Sign@f@pDD;
If@pSign * f1 < 0, x0 = p, x1 = p; f1 = pSignDD
D
1 th approximation is: 1.1
f@x_D := x ^ 3 + 2 x ^ 2 - 3 x - 1;
x0 = 1.0; x1 = 2.0; n = 20;
f1 = Sign@f@x1DD;
errList = 8<;
eps = 5 * 10 ^ H- 5L;
If@f@x0D * f1 > 0, Print@"Ivp not satisfied"D,
For@i = 1, i 2, i ++,
p = x1 - HHx1 - x0L Hf@x1D - f@x0DLL * f@x1D;
Print@i, " th approximation is: ", pD;
errList = Append@errList, pD;
pSign = Sign@f@pDD;
If@pSign * f1 < 0, x0 = p, x1 = p; f1 = pSignD
D;
For@i = 3, i n, i ++,
p = x1 - HHx1 - x0L Hf@x1D - f@x0DLL * f@x1D;
Print@i, " th approximation is: ", pD;
errList = Append@errList, pD;
= HerrList@@- 1DD - errList@@- 2DDL HerrList@@- 2DD - errList@@- 3DDL;
If@Abs@ H - 1LD * Abs@HerrList@@- 1DD - errList@@- 2DDLD < eps, Break@DD;
pSign = Sign@f@pDD;
If@pSign * f1 < 0, x0 = p, x1 = p; f1 = pSignD
D;
D
Basic Computations
In[369]:=
In[379]:=
Out[380]=
In[388]:=
a=3
Out[388]=
In[389]:=
3
sum = 0.0;
For@i = 1, i a, i ++, sum = sum + 1 iD
Print@sumD
1.83333
In[396]:=
Secant Method
11
12
In[400]:=
f@x_D := x ^ 3 - 5 x + 1;
p0 = 0.0;
p1 = 1.0;
Nmax = 20;
For@i = 1, i Nmax, i ++,
p2 = p1 - HHp1 - p0L Hf@p1D - f@p0DLL * f@p1D;
Print@i, " th approximation root is: ", p2D;
p0 = p1;
p1 = p2;
If@Abs@p1 - p0D < eps, Break@D, Continue@DD;
D
Print@"Final root is ", p2D
1 th approximation root is: 0.25
Lagranges Interpolation
In[48]:=
j=1
j=i+1
j=1
Return@Simplify@polynomial@xDDDF
In[47]:=
LagrangePolynomial@inp, fValD
1
Out[47]=
I5 + 4 x + 6 x2 M
trapezoidal rule
In[60]:=
f@x_D := Log@xD;
a = 1.0;
b = 2.2;
n = 12;
h = Hb - aL n;
s = 0;
For@i = 1, i < n, i ++,
s = s + f@a + i * hDD
approx = Hh 2L * Hf@aD + f@bD + 2 * HsLL;
Print@approxD
0.534152
k=1
k=1
Return@integralD;F
Out[86]=
In[96]:=
Out[97]=
In[98]:=
f@x_D := 1 H1 + xL;
SimpsonRule@f, 0.0, 1.0, 4D
0.693254
26
k-1
k=2
j=1
Return@Expand@answerDDF
In[100]:=
Out[100]=
Hx - y@@jDDL * NthDivDif@y, f, 1, kD ;
Length@yD
1 - 6 x + 8 x2
13
14
In[152]:=
Out[153]=
Out[154]=
Out[155]=
Out[156]=
,-
1
2
0
1
-2 -6
0
0 ,
1
, 1>>
6
,-
10
10
3
>>
2 4 5
0 -6 -7 ,
10
0 0
3
2 4 5
4 2 3 >
-3 1 4