Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
134 views

Numerical Methods and Programming Using Mathematica

Numerical methods and Programming techniques for BSc(H) third semester delhi university using mathematica.

Uploaded by

AkulBansal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views

Numerical Methods and Programming Using Mathematica

Numerical methods and Programming techniques for BSc(H) third semester delhi university using mathematica.

Uploaded by

AkulBansal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Bisection Method

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.

The new interval is: H1.,2.L

..........................................
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

Numerical Methods and Programming.nb

..........................................
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

Both error tolerance and max condition


In[39]:=

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

Numerical Methods and Programming.nb

The root enclosing interval is: H1.5,2.L

.....................................................
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

Numerical Methods and Programming.nb

The error is 0.000488281


The root enclosing interval is: H1.57031,1.5708L

.....................................................
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

Only Error tolerance


In[56]:=

Clear@f, x0, x1, i, p, f1D;

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

...............................................................

Numerical Methods and Programming.nb

4 th iteration approximation is 1.625 in interval H1.5,1.75L


Error bound is 0.125

...............................................................
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

Numerical Methods and Programming.nb

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

Estimated error is: 0.642093


__________________________________
2 th iteration is: 1.57068
Estimated error is: 0.0714173
__________________________________
3 th iteration is: 1.5708
Estimated error is: 0.00012105
__________________________________
Final approximation is: 1.5708

Newtons method with error tolerance only


In[216]:=

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:

Gauss Jacobi Method


4x1+x2+x3=2
x1+5x2+2x3=-6
x1+2x2+3x3=-4

1.5708

Numerical Methods and Programming.nb

In[152]:=

Out[155]=

Out[158]=

A = 885, 1, 2<, 8- 3, 9, 4<, 81, 2, - 7<<;


B = 810, - 14, - 33<;
X = 80, 0, 0<;
order = Length@AD
d = A * SparseArray@8i_, j_< ; j i 1, 8order, order<D;
u = A * SparseArray@8i_, j_< ; j < i 1, 8order, order<D;
l = A * SparseArray@8i_, j_< ; j > i 1, 8order, order<D
For@i = 1, i 41, i ++, xCopy = X; X = Inverse@dD.HB - l.X - u.XL;
If@Max@Abs@X - xCopyDD < 5 * 10 ^ H- 4L, Break@DD;
Print@i, " th iteration approximation is: ", N@XD MatrixFormDD;
Print@i, " th and final Approximation is: ", N@XD MatrixFormD
Print@N@A.XDD
3

880, 1, 2<, 80, 0, 4<, 80, 0, 0<<

Numerical Methods and Programming.nb

1 th iteration approximation is:

2.
- 1.55556
4.71429

2 th iteration approximation is:

0.425397
- 2.98413
4.55556

3 th iteration approximation is:

0.774603
- 3.43845
3.92245

4 th iteration approximation is:

1.11871
- 3.04067
3.84253

5 th iteration approximation is:

1.07112
- 2.89044
4.00534

6 th iteration approximation is:

0.975953
- 2.97867
4.04146

7 th iteration approximation is:

0.979148
- 3.02644
4.00266

8 th iteration approximation is:

1.00422
- 3.00813
3.98947

9 th iteration approximation is:

1.00584
- 2.99391
3.99828

10 th iteration approximation is:

0.99947
- 2.99729
4.00257

11 th iteration approximation is:

0.998428
- 3.00132
4.0007

12 th iteration approximation is:

0.999985
- 3.00083
3.9994

13 th iteration approximation is:

1.00041
- 2.99974
3.99976

14 th and final Approximation is:

1.00004
- 2.99976
4.00013

810.0007, - 13.9974, - 33.0004<

Gauss Sidel Method

Numerical Methods and Programming.nb

In[263]:=

A = 885, 1, 2<, 8- 3, 9, 4<, 81, 2, - 7<<;


B = 810, - 14, - 33<;
X = 80, 0, 0<;
d = A * SparseArray@8i_, j_< ; i j 1, 83, 3<D;
l = A * SparseArray@8i_, j_< ; i < j 1, 83, 3<D;
u = A * SparseArray@8i_, j_< ; i > j 1, 83, 3<D;
X1 = Inverse@d + uD.HB - l.XL;
Print@1, " th approximation is: ", N@X1D MatrixFormD;
For@i = 2, Max@Abs@X1 - XDD > 5 * 10 ^ H- 4L, i ++,
X = X1;
X1 = Inverse@d + uD.HB - l.XL;
Print@i, " th approximation is: ", N@X1D MatrixFormDD
Print@N@A.X1DD
1 th approximation is:

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.<

Regular Falsi Method


In[283]:=

Clear@fD

0.99991
- 3.00008
3.99996

10

Numerical Methods and Programming.nb

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

2 th approximation is: 1.15174


3 th approximation is: 1.17684
4 th approximation is: 1.18863
5 th approximation is: 1.19408
6 th approximation is: 1.19658
7 th approximation is: 1.19773
8 th approximation is: 1.19825
9 th approximation is: 1.19849
10 th approximation is: 1.1986

Regular Falsi Method with stopping condition


In[358]:=

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

Numerical Methods and Programming.nb

1 th approximation is: 1.1


2 th approximation is: 1.15174
3 th approximation is: 1.17684
4 th approximation is: 1.18863
5 th approximation is: 1.19408
6 th approximation is: 1.19658
7 th approximation is: 1.19773
8 th approximation is: 1.19825
9 th approximation is: 1.19849
10 th approximation is: 1.1986
11 th approximation is: 1.19865

Basic Computations
In[369]:=

In[379]:=

Out[380]=

f@n_D := If@n 1, Return@1D, N@1 n + f@n - 1DDD


a = Input@"Enter the the n"D;
f@aD
1.5

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]:=

y = Input@"Enter an array which you want to sort ?"D;


Print@"The array is ", yD;
For@i = 1, i < 10, i ++,
If@y@@iDD > y@@i + 1DD,
For@j = i, j 1, j --,
If@y@@i + 1DD > y@@jDD, Break@DDD;
y = Insert@y, y@@i + 1DD, j + 1D;
y = Delete@y, i + 2D
D;
D;
Print@"The sorted array is ", yD
The array is 810, 7, 2, 9, 8, 6, 3, 4, 5, 1<

The sorted array is 81, 2, 3, 4, 5, 6, 7, 8, 9, 10<

Secant Method

11

12

Numerical Methods and Programming.nb

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

2 th approximation root is: 0.186441


3 th approximation root is: 0.201736
4 th approximation root is: 0.20164
5 th approximation root is: 0.20164
Final root is 0.20164

Lagranges Interpolation
In[48]:=

inp = 81, 5, 20<;


fVal = 81, 3, 35<;

LagrangePolynomial@y_, g_D := ModuleB8inp = y , fVal = g<,


n = Length@inpD;
ForBi = 1, i <= n , i ++,
L@i, x_D = Hx - inp@@jDDL Hinp@@iDD - inp@@jDDL
i-1

j=1

Hx - inp@@jDDL Hinp@@iDD - inp@@jDDL F;


n

j=i+1

polynomial@x_D = L@j, xD * fVal@@jDD;


n

j=1

Return@Simplify@polynomial@xDDDF

In[47]:=

LagrangePolynomial@inp, fValD
1

Out[47]=

I5 + 4 x + 6 x2 M

trapezoidal rule

Numerical Methods and Programming.nb

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

Simpsons 1/3 Rule


In[84]:=

SimpsonRule@f_, a0_, b0_, m0_D :=


ModuleB8b = b0<,
If@Mod@m, 2D 0, Print@"m should be even positive integer"D; Return@D;D;
h = Hb - a0L m0;
n = m0 2;
integral = Hh 3L * Hf@a0D + f@bDL +
H2 h 3L * f@a0 + H2 * kL hD + H4 h 3L * f@a0 + HH2 * kL - 1L * hD
n-1

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

NthDivDif@x0_, f0_, sind_, eind_D := Module@8x = x0, f = f0, i = sind, j = eind<,


If@i j, Return@f@@iDDD,
answer = HNthDivDif@x, f, i + 1, jD - NthDivDif@x, f, i, j - 1DL
Hx@@jDD - x@@iDDL; Return@answerDD;D
NthDivDif@80, 1, 3<, 81, 3, 55<, 2, 3D

26

NewtonDiPol@x0_, f0_D := ModuleB8y = x0, f = f0<,


answer = f@@1DD +

k-1

k=2

j=1

Return@Expand@answerDDF

In[100]:=
Out[100]=

Hx - y@@jDDL * NthDivDif@y, f, 1, kD ;

Length@yD

NewtonDiPol@80, 1, 3<, 81, 3, 55<D

1 - 6 x + 8 x2

13

14

Numerical Methods and Programming.nb

In[152]:=

Out[153]=

Out[154]=

Out[155]=

Out[156]=

A = 884, 2, 3<, 8- 3, 1, 4<, 82, 4, 5<<;


8lu, p, c< = LUDecomposition@AD
l = lu SparseArray@8i_, j_< ; j < i 1, 83, 3<D + IdentityMatrix@3D
u = lu SparseArray@8i_, j_< ; j i 1, 83, 3<D
MatrixForm 8A, l, u, l.u<
::82, 4, 5<, 82, - 6, - 7<, ::81, 0, 0<, 82, 1, 0<, :-

,-

1
2

0
1

-2 -6

0
0 ,
1

>>, 83, 1, 2<, 1>

, 1>>
6

:82, 4, 5<, 80, - 6, - 7<, :0, 0,


4 2 3
: -3 1 4 ,
2 4 5

,-

10

10
3

>>

2 4 5
0 -6 -7 ,
10
0 0
3

2 4 5
4 2 3 >
-3 1 4

You might also like