0% found this document useful (0 votes)
9 views

Numerical Methods Practical File

The document is a practical file for a B.Sc. (Hons.) Mathematics course focusing on Numerical Methods. It includes various practical exercises such as finding absolute values, checking even/odd numbers, calculating sums and factorials, and solving quadratic equations. The file outlines multiple numerical methods and algorithms used in mathematics, demonstrating their implementation through code snippets.

Uploaded by

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

Numerical Methods Practical File

The document is a practical file for a B.Sc. (Hons.) Mathematics course focusing on Numerical Methods. It includes various practical exercises such as finding absolute values, checking even/odd numbers, calculating sums and factorials, and solving quadratic equations. The file outlines multiple numerical methods and algorithms used in mathematics, demonstrating their implementation through code snippets.

Uploaded by

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

NUMERICAL METHODS

PRACTICAL FILE

Name Aarushi Kansal


Exam Roll Number 18034563036
Course B.Sc. (Hons.) Mathematics
Semester 5
Subject Numerical Methods
Unique Paper Code 32357501
CONTENTS

PRACTICAL NUMBER PRACTICAL NAME

1. ABSOLUTE VALUE AND ODD/EVEN NUMBER

FINDING SUM OF A SERIES AND FACTORIAL OF A


2.
NUMBER

3. DIVISIBILITY, PRIMALITY AND POWER OF NUMBERS

4. FINDING THE ROOTS OF A QUADRATIC EQUATION

5. BISECTION METHOD

6. METHOD OF FALSE POSITION

7. ARRAY

8. DEFINING FUNCTIONS
9. SECANT METHOD

10. NEWTON’S METHOD

11. GAUSS-JACOBI METHOD

12. GAUSS-SEIDEL METHOD

13. LAGRANGE INTERPOLATING POLYNOMIAL

14. NEWTON INTERPOLATING POLYNOMIAL

15. CALCULATING 1st DERIVATIVE

16. CALCULATING 2nd DERIVATIVE

17. INTEGRATION

18. EULER’S METHOD


PRACTICAL-1
ABSOLUTE VALUE AND ODD/EVEN NUMBER

FINDING THE ABSOLUTE VALUE OF A NUMBER

In[1]:= n = Input ["enter the number n"];


Print ["n = ", n]
If [n ≥ 0, Print ["Absolute value of n is ", n], Print ["Absolute value of n is ", - n]]

n = 25

Absolute value of n is 25

In[4]:= n = Input ["enter the number n"];


Print ["n = ", n]
If [n ≥ 0, Print ["Absolute value of n is ", n], Print ["Absolute value of n is ", - n]]

n = - 78

Absolute value of n is 78

In[7]:= n = Input ["enter the number n"];


Print ["n = ", n]
If [n ≥ 0, Print ["Absolute value of n is ", n], Print ["Absolute value of n is ", - n]]

n = 686 407

Absolute value of n is 686 407

In[10]:= n = Input ["enter the number n"];


Print ["n = ", n]
If [n ≥ 0, Print ["Absolute value of n is ", n], Print ["Absolute value of n is ", - n]]

n = - 6 274 065

Absolute value of n is 6 274 065

TO CHECK IF A NUMBER IS EVEN OR ODD

In[13]:= n = Input [" enter the number n"];


Print ["n = ", n]
If [Mod [n, 2] == 0, Print [n, " is even "], Print [n, " is odd "]]

n = 54

54 is even
2

In[16]:= n = Input [" enter the number n"];


Print ["n = ", n]
If [Mod [n, 2] == 0, Print [n, " is even "], Print [n, " is odd "]]

n = 97

97 is odd

In[19]:= n = Input [" enter the number n"];


Print ["n = ", n]
If [Mod [n, 2] == 0, Print [n, " is even "], Print [n, " is odd "]]

n = 78 943

78 943 is odd

In[22]:= n = Input [" enter the number n"];


Print ["n = ", n]
If [Mod [n, 2] == 0, Print [n, " is even "], Print [n, " is odd "]]

n = 3 658 790

3 658 790 is even


PRACTICAL-2
FINDING SUM OF A SERIES AND FACTORIAL OF A NUMBER

SUM OF A SERIES

1)Finding the sum of first 'n' natural numbers

In[1]:= n = Input ["enter n"];


Print ["n = ", n]
sum = 0;
For [i = 1, i ≤ n, i ++ , sum = sum + i];
Print ["The sum of first n natural numbers is ", sum ]

n = 10

The sum of first n natural numbers is 55

In[6]:= n = Input ["enter n"];


Print ["n = ", n]
sum = 0;
For [i = 1, i ≤ n, i ++ , sum = sum + i];
Print ["The sum of first n natural numbers is ", sum ]

n = 62

The sum of first n natural numbers is 1953

2)Finding the sum of the series ∑ n^2

In[11]:= n = Input ["enter n"];


k = Input ["enter k"];
Print ["n = ", n]
Print ["k = ", k]
sum = 0;
For [i = 1, i ≤ n, i ++ , sum = sum + i ^ k];
Print ["The sum of the series is ", sum ]

n = 5

k = 2

The sum of the series is 55


2

In[18]:= n = Input ["enter n"];


k = Input ["enter k"];
Print ["n = ", n]
Print ["k = ", k]
sum = 0;
For [i = 1, i ≤ n, i ++ , sum = sum + i ^ k];
Print ["The sum of the series is ", sum ]

n = 40

k = 2

The sum of the series is 22 140

3)Finding the sum of the series ∑ n^3

In[25]:= n = Input ["enter n"];


k = Input ["enter k"];
Print ["n = ", n]
Print ["k = ", k]
sum = 0;
For [i = 1, i ≤ n, i ++ , sum = sum + i ^ k];
Print ["The sum of the series is ", sum ]

n = 5

k = 3

The sum of the series is 225

In[32]:= n = Input ["enter n"];


k = Input ["enter k"];
Print ["n = ", n]
Print ["k = ", k]
sum = 0;
For [i = 1, i ≤ n, i ++ , sum = sum + i ^ k];
Print ["The sum of the series is ", sum ]

n = 75

k = 3

The sum of the series is 8 122 500


3

FACTORIAL OF A NUMBER

In[39]:= n = Input ["enter n"];


Print ["n= ", n ];
fact = 1;
For [i = 1, i ≤ n, i ++ , fact = fact * i];
Print ["Factorial of n is ", fact ]

n=6

Factorial of n is 720

In[49]:= n = Input ["enter n"];


Print ["n= ", n ];
fact = 1;
For [i = 1, i ≤ n, i ++ , fact = fact * i];
Print ["Factorial of n is ", fact ]

n = 15

Factorial of n is 1 307 674 368 000

In[54]:= n = Input ["enter n"];


Print ["n= ", n ];
fact = 1;
For [i = 1, i ≤ n, i ++ , fact = fact * i];
Print ["Factorial of n is ", fact ]

n = 30

Factorial of n is 265 252 859 812 191 058 636 308 480 000 000
PRACTICAL-3
DIVISIBILITY , PRIMALITY AND POWER OF NUMBERS

DIVISIBILITY BY 3

In[1]:= k = Input ["enter the number n"];


n = k;
Print ["n = ", k]
sum = 0;
i = 1;
While [n > 0, m = Mod [n, 10 ]; sum = sum + m; n = Floor [n / 10 ]; i ++]
Print ["The sum of the digits of given number is = ", sum ];
If [Mod [sum , 3] == 0, Print [k, " is divisible by 3."],
Print [k, " is not divisible by 3."]]

n = 30

The sum of the digits of given number is = 3

30 is divisible by 3.

In[9]:= k = Input ["enter the number n"];


n = k;
Print ["n = ", k]
sum = 0;
i = 1;
While [n > 0, m = Mod [n, 10 ]; sum = sum + m; n = Floor [n / 10 ]; i ++]
Print ["The sum of the digits of given number is = ", sum ];
If [Mod [sum , 3] == 0, Print [k, " is divisible by 3."],
Print [k, " is not divisible by 3."]]

n = 17

The sum of the digits of given number is = 8

17 is not divisible by 3.
2

In[17]:= k = Input ["enter the number n"];


n = k;
Print ["n = ", k]
sum = 0;
i = 1;
While [n > 0, m = Mod [n, 10 ]; sum = sum + m; n = Floor [n / 10 ]; i ++]
Print ["The sum of the digits of given number is = ", sum ];
If [Mod [sum , 3] == 0, Print [k, " is divisible by 3."],
Print [k, " is not divisible by 3."]]

n = 7 754 982

The sum of the digits of given number is = 42

7 754 982 is divisible by 3.

DIVISIBILITY BY 4

In[25]:= n = Input ["enter the number n"];


Print ["n = ", n]
m = Mod [n, 100 ];
If [Mod [m, 4] == 0, Print [n, " is divisible by 4."], Print [n, " is not divisible by 4."]]

n = 48

48 is divisible by 4.

In[29]:= n = Input ["enter the number n"];


Print ["n = ", n]
m = Mod [n, 100 ];
If [Mod [m, 4] == 0, Print [n, " is divisible by 4."], Print [n, " is not divisible by 4."]]

n = 57

57 is not divisible by 4.

In[33]:= n = Input ["enter the number n"];


Print ["n = ", n]
m = Mod [n, 100 ];
If [Mod [m, 4] == 0, Print [n, " is divisible by 4."], Print [n, " is not divisible by 4."]]

n = 9 845 678

9 845 678 is not divisible by 4.


3

TO CHECK IF A NUMBER IS PRIME OR COMPOSITE

In[37]:= n = Input [" Enter the number "];


Print ["n = ", n]
m = Floor [n / 2];
If [n == 1, Print [" 1 is neither prime nor composite ."]; Return []]
For [i = 2, i ≤ m, i ++ , k = Mod [n, i];
If [k == 0, Print [n, " is composite "]; Break []] ×
If [i == m, Print [n, " is prime "];
Return []]]

n = 23

23 is prime

In[42]:= n = Input [" Enter the number "];


Print ["n = ", n]
m = Floor [n / 2];
If [n == 1, Print [" 1 is neither prime nor composite ."]; Return []]
For [i = 2, i ≤ m, i ++ , k = Mod [n, i];
If [k == 0, Print [n, " is composite "]; Break []] ×
If [i == m, Print [n, " is prime "];
Return []]]

n = 46

46 is composite

In[52]:= n = Input [" Enter the number "];


Print ["n = ", n]
m = Floor [n / 2];
If [n == 1, Print ["1 is neither prime nor composite ."]; Return []]
For [i = 2, i ≤ m, i ++ , k = Mod [n, i];
If [k == 0, Print [n, " is composite "]; Break []] ×
If [i == m, Print [n, " is prime "];
Return []]]

n = 1

1 is neither prime nor composite .


4

In[57]:= n = Input [" Enter the number "];


Print ["n = ", n]
m = Floor [n / 2];
If [n == 1, Print ["1 is neither prime nor composite ."]; Return []]
For [i = 2, i ≤ m, i ++ , k = Mod [n, i];
If [k == 0, Print [n, " is composite "]; Break []] ×
If [i == m, Print [n, " is prime "];
Return []]]

n = 2017

2017 is prime

In[62]:= n = Input [" Enter the number "];


Print ["n = ", n]
m = Floor [n / 2];
If [n == 1, Print ["1 is neither prime nor composite ."]; Return []]
For [i = 2, i ≤ m, i ++ , k = Mod [n, i];
If [k == 0, Print [n, " is composite "]; Break []] ×
If [i == m, Print [n, " is prime "];
Return []]]

n = 5 633 480

5 633 480 is composite

POWERS OF A NUMBER

In[32]:= n = Input ["enter n"];


m = Input ["enter m"];
Print ["n = ", n]
Print ["m = ", m]
pow = n;
i = 1;
While [pow < m, Print [pow ];
pow = pow * n;
i ++]
5

n = 3

m = 1000

27

81

243

729

In[25]:= n = Input ["enter n"];


m = Input ["enter m"];
Print ["n = ", n]
Print ["m = ", m]
pow = n;
i = 1;
While [pow < m, Print [pow ];
pow = pow * n;
i ++]

n = 2

m = 500

16

32

64

128

256
PRACTICAL-4
FINDING THE ROOTS OF A QUADRATIC EQUATION

In[1]:= a = Input ["enter the constant term "];


b = Input ["enter the constant term "];
c = Input ["enter the constant term "];
Print ["The given equation is ", a "x^2 + ", b "x + ", c]
d = b ^ 2 - 4 a c;
If [d < 0, Print ["Roots are imaginary ."], Print ["Roots are real ."]]
x1 = (- b + Sqrt [d]) / (2 * a);
x2 = (- b + Sqrt [d]) / (2 * a);
Print ["Roots of the given equation are :"]
Print ["x1 = ", x1 ]
Print ["x2 = ", x2 ]

The given equation is x^2 + 2 x + 1

Roots are real .

Roots of the given equation are :

x1 = - 1

x2 = - 1

In[12]:= a = Input ["enter the constant term "];


b = Input ["enter the constant term "];
c = Input ["enter the constant term "];
Print ["The given equation is ", a "x^2 + ", b "x + ", c]
d = b ^ 2 - 4 a c;
If [d < 0, Print ["Roots are imaginary ."], Print ["Roots are real ."]]
x1 = (- b + Sqrt [d]) / (2 * a);
x2 = (- b + Sqrt [d]) / (2 * a);
Print ["Roots of the given equation are :"]
Print ["x1 = ", x1 ]
Print ["x2 = ", x2 ]

The given equation is x^2 + x + 1

Roots are imaginary .

Roots of the given equation are :

1
x1 = - 1 + ⅈ 3
2
1
x2 = - 1 + ⅈ 3
2
PRACTICAL-5
BISECTION METHOD

1) STOPPING CONDITION IS NUMBER OF ITERATIONS

In[1]:= f[x_ ] = Input ["enter the function "];


a = Input ["enter a"];
b = Input ["enter b"];
n = Input ["the number of iterations "];
Print ["The given function is ", f[x]]
i = 0;
c = (a + b) / 2;
OutputDetails = {{ i, N[a], N[b], N[c], N[f[a]], N[f[b]], N[f[c]]}} ;
For [i = 1, i ≤ n, i ++ ,
If [f[a] * f[c] < 0, b = c, a = c];
c = N[(a + b) / 2];
OutputDetails = Append [OutputDetails , { i, N[a], N[b], N[c], N[f[a]], N[f[b]], N[f[c]]}]]
Print [NumberForm [TableForm [OutputDetails ,
TableHeadings → { None , { "i", "a", "b", "c", "f[a]", "f[b]", "f[c]"}}], 6]]

The given function is - 1 - 3 x + 2 x 2 + x 3

i a b c f [a ] f [b ] f [c ]
0 1. 2. 1.5 - 1. 9. 2.375
1 1. 1.5 1.25 - 1. 2.375 0.328125
2 1. 1.25 1.125 - 1. 0.328125 - 0.419922
3 1.125 1.25 1.1875 - 0.419922 0.328125 - 0.067627
4 1.1875 1.25 1.21875 - 0.067627 0.328125 0.124725
2

In[21]:= f[x_ ] = Input ["enter the function "];


a = Input ["enter a"];
b = Input ["enter b"];
n = Input ["the number of iterations "];
Print ["The given function is ", f[x]]
i = 0;
c = (a + b) / 2;
OutputDetails = {{ i, N[a], N[b], N[c], N[f[a]], N[f[b]], N[f[c]]}} ;
For [i = 1, i ≤ n, i ++ ,
If [f[a] * f[c] < 0, b = c, a = c];
c = N[(a + b) / 2];
OutputDetails = Append [OutputDetails , { i, N[a], N[b], N[c], N[f[a]], N[f[b]], N[f[c]]}]]
Print [NumberForm [TableForm [OutputDetails ,
TableHeadings → { None , { "i", "a", "b", "c", "f[a]", "f[b]", "f[c]"}}], 6]]

The given function is - 3 + x 6

i a b c f [a ] f [b ] f [c ]
0 1. 2. 1.5 - 2. 61. 8.39063
1 1. 1.5 1.25 - 2. 8.39063 0.814697
2 1. 1.25 1.125 - 2. 0.814697 - 0.972713
3 1.125 1.25 1.1875 - 0.972713 0.814697 - 0.195847
4 1.1875 1.25 1.21875 - 0.195847 0.814697 0.277085

2) STOPPING CONDITION IS ERRORBOUND

In[31]:= f[x_ ] = Input ["enter the function "];


a = Input ["enter a"];
b = Input ["enter b"];
errorbound = Input ["enter the error bound "];
Print ["The given function is ", f[x]]
i = 0;
c = (a + b) / 2;
OutputDetails = {{ i, N[a], N[b], N[c], N[f[a]], N[f[b]], N[f[c]]}} ;
While [Abs [f[c]] > errorbound , i ++ ;
If [f[a] * f[c] < 0, b = c, a = c];
c = N[(a + b) / 2];
OutputDetails = Append [OutputDetails , { i, N[a], N[b], N[c], N[f[a]], N[f[b]], N[f[c]]}]]
Print [NumberForm [TableForm [OutputDetails ,
TableHeadings → { None , { "i", "a", "b", "c", "f[a]", "f[b]", "f[c]"}}], 6]]
3

The given function is - 1 - 3 x + 2 x 2 + x 3

i a b c f [a ] f [b ] f [c ]
0 1. 2. 1.5 - 1. 9. 2.375
1 1. 1.5 1.25 - 1. 2.375 0.328125
2 1. 1.25 1.125 - 1. 0.328125 - 0.419922
3 1.125 1.25 1.1875 - 0.419922 0.328125 - 0.067627
4 1.1875 1.25 1.21875 - 0.067627 0.328125 0.124725
5 1.1875 1.21875 1.20313 - 0.067627 0.124725 0.0271797
6 1.1875 1.20313 1.19531 - 0.067627 0.0271797 - 0.0205646
7 1.19531 1.20313 1.19922 - 0.0205646 0.0271797 0.00322217

In[41]:= f[x_ ] = Input ["enter the function "];


a = Input ["enter a"];
b = Input ["enter b"];
errorbound = Input ["enter the error bound "];
Print ["The given function is ", f[x]]
i = 0;
c = (a + b) / 2;
OutputDetails = {{ i, N[a], N[b], N[c], N[f[a]], N[f[b]], N[f[c]]}} ;
While [Abs [f[c]] > errorbound , i ++ ;
If [f[a] * f[c] < 0, b = c, a = c];
c = N[(a + b) / 2];
OutputDetails = Append [OutputDetails , { i, N[a], N[b], N[c], N[f[a]], N[f[b]], N[f[c]]}]]
Print [NumberForm [TableForm [OutputDetails ,
TableHeadings → { None , { "i", "a", "b", "c", "f[a]", "f[b]", "f[c]"}}], 6]]

The given function is - 3 + x 6

i a b c f [a ] f [b ] f [c ]
0 1. 2. 1.5 - 2. 61. 8.39063
1 1. 1.5 1.25 - 2. 8.39063 0.814697
2 1. 1.25 1.125 - 2. 0.814697 - 0.972713
3 1.125 1.25 1.1875 - 0.972713 0.814697 - 0.195847
4 1.1875 1.25 1.21875 - 0.195847 0.814697 0.277085
5 1.1875 1.21875 1.20313 - 0.195847 0.277085 0.0329448
6 1.1875 1.20313 1.19531 - 0.195847 0.0329448 - 0.0833201
7 1.19531 1.20313 1.19922 - 0.0833201 0.0329448 - 0.025661
8 1.19922 1.20313 1.20117 - 0.025661 0.0329448 0.00352277
PRACTICAL-6
METHOD OF FALSE POSITION

1) STOPPING CONDITION IS NUMBER OF ITERATIONS

In[1]:= f[x_ ] = Input ["enter the function "];


a = Input ["enter a"];
b = Input ["enter b"];
n = Input ["the number of iterations "];
Print ["The given function is ", f[x]]
i = 0;
c = ((a * f[b]) - (b * f[a])) / (f[b] - f[a]);
OutputDetails = {{ i, N[a], N[b], N[c], N[f[a]], N[f[b]], N[f[c]]}} ;
For [i = 1, i ≤ n, i ++ ,
If [f[a] * f[c] < 0, b = c, a = c];
c = N[((a * f[b]) - (b * f[a])) / (f[b] - f[a])];
OutputDetails = Append [OutputDetails , { i, N[a], N[b], N[c], N[f[a]], N[f[b]], N[f[c]]}]]
Print [NumberForm [TableForm [OutputDetails ,
TableHeadings → { None , { "i", "a", "b", "c", "f[a]", "f[b]", "f[c]"}}], 6]]

The given function is - 3 - 3 x + x 2 + x 3

i a b c f [a ] f [b ] f [c ]
0 1. 2. 1.57143 - 4. 3. - 1.36443
1 1.57143 2. 1.70541 - 1.36443 3. - 0.247745
2 1.70541 2. 1.72788 - 0.247745 3. - 0.0393396
3 1.72788 2. 1.7314 - 0.0393396 3. - 0.00611067
4 1.7314 2. 1.73195 - 0.00611067 3. - 0.000945921
2

In[11]:= f[x_ ] = Input ["enter the function "];


a = Input ["enter a"];
b = Input ["enter b"];
n = Input ["the number of iterations "];
Print ["The given function is ", f[x]]
i = 0;
c = ((a * f[b]) - (b * f[a])) / (f[b] - f[a]);
OutputDetails = {{ i, N[a], N[b], N[c], N[f[a]], N[f[b]], N[f[c]]}} ;
For [i = 1, i ≤ n, i ++ ,
If [f[a] * f[c] < 0, b = c, a = c];
c = N[((a * f[b]) - (b * f[a])) / (f[b] - f[a])];
OutputDetails = Append [OutputDetails , { i, N[a], N[b], N[c], N[f[a]], N[f[b]], N[f[c]]}]]
Print [NumberForm [TableForm [OutputDetails ,
TableHeadings → { None , { "i", "a", "b", "c", "f[a]", "f[b]", "f[c]"}}], 6]]

The given function is - 6 - x + Tan [π x ]

i a b c f [a ] f [b ] f [c ]
0 0.4 0.48 0.420867 - 3.32232 9.41454 - 2.48159
1 0.420867 0.48 0.433203 - 2.48159 9.41454 - 1.73804
2 0.433203 0.48 0.440496 - 1.73804 9.41454 - 1.15359
3 0.440496 0.48 0.444808 - 1.15359 9.41454 - 0.73541
4 0.444808 0.48 0.447358 - 0.73541 9.41454 - 0.455924

2) STOPPING CONDITION IS ERRORBOUND

In[21]:= f[x_ ] = Input ["enter the function "];


a = Input ["enter a"];
b = Input ["enter b"];
errorbound = Input ["enter the error bound "];
Print ["The given function is ", f[x]]
i = 0;
c = ((a * f[b]) - (b * f[a])) / (f[b] - f[a]);
OutputDetails = {{ i, N[a], N[b], N[c], N[f[a]], N[f[b]], N[f[c]]}} ;
While [Abs [f[c]] > errorbound , i ++ ;
If [f[a] * f[c] < 0, b = c, a = c];
c = N[((a * f[b]) - (b * f[a])) / (f[b] - f[a])];
OutputDetails = Append [OutputDetails , { i, N[a], N[b], N[c], N[f[a]], N[f[b]], N[f[c]]}]]
Print [NumberForm [TableForm [OutputDetails ,
TableHeadings → { None , { "i", "a", "b", "c", "f[a]", "f[b]", "f[c]"}}], 6]]
3

The given function is - 3 - 3 x + x 2 + x 3

i a b c f [a ] f [b ] f [c ]
0 1. 2. 1.57143 - 4. 3. - 1.36443
1 1.57143 2. 1.70541 - 1.36443 3. - 0.247745
2 1.70541 2. 1.72788 - 0.247745 3. - 0.0393396
3 1.72788 2. 1.7314 - 0.0393396 3. - 0.00611067
4 1.7314 2. 1.73195 - 0.00611067 3. - 0.000945921
5 1.73195 2. 1.73204 - 0.000945921 3. - 0.000146349

In[31]:= f[x_ ] = Input ["enter the function "];


a = Input ["enter a"];
b = Input ["enter b"];
errorbound = Input ["enter the error bound "];
Print ["The given function is ", f[x]]
i = 0;
c = ((a * f[b]) - (b * f[a])) / (f[b] - f[a]);
OutputDetails = {{ i, N[a], N[b], N[c], N[f[a]], N[f[b]], N[f[c]]}} ;
While [Abs [f[c]] > errorbound , i ++ ;
If [f[a] * f[c] < 0, b = c, a = c];
c = N[((a * f[b]) - (b * f[a])) / (f[b] - f[a])];
OutputDetails = Append [OutputDetails , { i, N[a], N[b], N[c], N[f[a]], N[f[b]], N[f[c]]}]]
Print [NumberForm [TableForm [OutputDetails ,
TableHeadings → { None , { "i", "a", "b", "c", "f[a]", "f[b]", "f[c]"}}], 6]]

The given function is - 6 - x + Tan [π x ]

i a b c f [a ] f [b ] f [c ]
0 0.4 0.48 0.420867 - 3.32232 9.41454 - 2.48159
1 0.420867 0.48 0.433203 - 2.48159 9.41454 - 1.73804
2 0.433203 0.48 0.440496 - 1.73804 9.41454 - 1.15359
3 0.440496 0.48 0.444808 - 1.15359 9.41454 - 0.73541
4 0.444808 0.48 0.447358 - 0.73541 9.41454 - 0.455924
5 0.447358 0.48 0.448866 - 0.455924 9.41454 - 0.27755
6 0.448866 0.48 0.449757 - 0.27755 9.41454 - 0.167038
7 0.449757 0.48 0.450284 - 0.167038 9.41454 - 0.0998235
8 0.450284 0.48 0.450596 - 0.0998235 9.41454 - 0.0594023
9 0.450596 0.48 0.45078 - 0.0594023 9.41454 - 0.0352587
PRACTICAL-7
ARRAY

In[1]:= f = Array [h, n];


n = Input ["enter length of the array "];
For [i = 1, i ≤ n, i ++ , h[i] = Input ["enter f"];];
Print ["The array is:"]
Print [f]

The array is:

{ 56, 7, 34, 8, 5 }

FINDING THE AVERAGE OF THE ELEMENTS OF AN ARRAY

In[6]:= g = Array [a, p];


p = Input ["enter length of the array "];
sum = 0;
For [i = 1, i ≤ p, i ++ , sum = sum + a[i]; a[i] = Input ["enter g"]];
Print ["The array is:"]
Print [g]
Print ["The sum of the elements of the array is = ", sum ]
avg = sum / p;
Print ["The average of the elements of the array is = ", N[avg ]]

The array is:

{ 2, 4, 6, 8, 10 }

The sum of the elements of the array is = 30

The average of the elements of the array is = 6.

In[15]:= r = Array [b, q];


q = Input ["enter length of the array "];
sum = 0;
For [i = 1, i ≤ q, i ++ , sum = sum + b[i]; b[i] = Input ["enter r"]];
Print ["The array is:"]
Print [r]
Print ["The sum of the elements of the array is = ", sum ]
avg = sum / q;
Print ["The average of the elements of the array is = ", N[avg ]]
2

The array is:

{ 34, 45, 70, 6, 75, 90, 55, 63 }

The sum of the elements of the array is = 438

The average of the elements of the array is = 54.75

SORTING OF AN ARRAY

1) ARRANGING THE ELEMENTS OF AN ARRAY IN ASCENDING ORDER

In[24]:= s = Array [c, t];


t = Input ["enter length of the array "];
For [i = 1, i ≤ t, i ++ , c[i] = Input ["enter s"];]
For [i = 1, i ≤ t, i ++ ,
For [j = i + 1, j ≤ t, j ++ ,
If [c[j] < c[i], temp = c[i]; c[i] = c[j]; c[j] = temp , c[i] = c[i]]]]
Print ["The elements of the array in ascending order are :"]
Print [s]

The elements of the array in ascending order are :

{ 2, 6, 7, 18, 34 }

In[30]:= u = Array [d, m];


m = Input ["enter length of the array "];
For [i = 1, i ≤ m, i ++ , d[i] = Input ["enter u"];]
For [i = 1, i ≤ m, i ++ ,
For [j = i + 1, j ≤ m, j ++ ,
If [d[j] < d[i], temp = d[i]; d[i] = d[j]; d[j] = temp , d[i] = d[i]]]]
Print ["The elements of the array in ascending order are :"]
Print [u]

The elements of the array in ascending order are :

{ 0, 19, 29, 43, 45, 67, 72, 88, 90, 203 }


3

2) ARRANGING THE ELEMENTS OF AN ARRAY IN DESCENDING ORDER

In[36]:= v = Array [e, l];


l = Input ["enter length of the array "];
For [i = 1, i ≤ l, i ++ , e[i] = Input ["enter v"];]
For [i = 1, i ≤ l, i ++ ,
For [j = i + 1, j ≤ l, j ++ ,
If [e[j] > e[i], temp = e[i]; e[i] = e[j]; e[j] = temp , e[i] = e[i]]]]
Print ["The elements of the array in descending order are :"]
Print [v]

The elements of the array in descending order are :

{ 32, 15, 6, 5, 1 }

In[42]:= w = Array [k, z];


z = Input ["enter length of the array "];
For [i = 1, i ≤ z, i ++ , k[i] = Input ["enter w"];]
For [i = 1, i ≤ z, i ++ ,
For [j = i + 1, j ≤ z, j ++ ,
If [k[j] > k[i], temp = k[i]; k[i] = k[j]; k[j] = temp , k[i] = k[i]]]]
Print ["The elements of the array in descending order are :"]
Print [w]

The elements of the array in descending order are :

{ 500 , 422 , 215 , 101 , 77, 63, 50, 25, 12, 8 }


PRACTICAL-8
DEFINING FUNCTIONS

PRIMALITY BY DEFINING A FUNCTION

In[1]:= prime [n0_ ] :=


Module [{n = n0 },
m = Floor [n / 2];
If [n == 1, Print [" 1 is neither prime nor composite ."]; Return []] ×
For [i = 2, i ≤ m, i ++ , k = Mod [n, i];
If [k == 0, Print [n, " is composite "]; Break []] ×
If [i == m, Print [n, " is prime "];
Return []]];];

In[2]:= prime [56 ]


56 is composite

In[3]:= prime [101 ]


101 is prime

FINDING SUM OF SERIES BY DEFINING A FUNCTION

In[4]:= series [x0_ , n0_ ] :=


Module [{x = x0, n = n0 },
sum = 0;
For [i = 1, i ≤ n, i ++ , sum = sum + i ^ x;];
Print [sum ]];

In[5]:= series [3, 4]


100

In[6]:= series [5, 100 ]


171 708 332 500
2

BISECTION METHOD BY DEFINING A FUNCTION

1) STOPPING CONDITION IS NUMBER OF ITERATIONS

In[10]:= bisection [f _, a0_ , b0_ , n0_ ] :=


Module [{a = a0, b = b0, n = n0 },
i = 0;
c = (a + b) / 2;
OutputDetails = {{ i, N[a], N[b], N[c], N[f[a]], N[f[b]], N[f[c]]}} ;
For [i = 1, i ≤ n, i ++ ,
If [f[a] * f[c] < 0, b = c, a = c];
c = N[(a + b) / 2];
OutputDetails = Append [OutputDetails , { i, N[a], N[b], N[c], N[f[a]], N[f[b]], N[f[c]]}]] ×
Print [NumberForm [TableForm [OutputDetails ,
TableHeadings → { None , { "i", "a", "b", "c", "f[a]", "f[b]", "f[c]"}}], 6]];];

In[11]:= f[x_ ] = Sin [x];


bisection [f, 3, 4, 4]
i a b c f [a ] f [b ] f [c ]
0 3. 4. 3.5 0.14112 - 0.756802 - 0.350783
1 3. 3.5 3.25 0.14112 - 0.350783 - 0.108195
2 3. 3.25 3.125 0.14112 - 0.108195 0.0165919
3 3.125 3.25 3.1875 0.0165919 - 0.108195 - 0.0458912
4 3.125 3.1875 3.15625 0.0165919 - 0.0458912 - 0.0146568

2)STOPPING CONDITION IS ERRORBOUND

In[13]:= bisectioneb [g _, a0_ , b0_ , e0_ ] :=


Module [{a = a0, b = b0, e = e0 },
i = 0;
c = (a + b) / 2;
OutputDetails = {{ i, N[a], N[b], N[c], N[g[a]], N[g[b]], N[g[c]]}} ;
While [Abs [g[c]] > e, i ++ ;
If [g[a] * g[c] < 0, b = c, a = c];
c = N[(a + b) / 2];
OutputDetails = Append [OutputDetails , { i, N[a], N[b], N[c], N[g[a]], N[g[b]], N[g[c]]}]] ×
Print [NumberForm [TableForm [OutputDetails ,
TableHeadings → { None , { "i", "a", "b", "c", "g[a]", "g[b]", "g[c]"}}], 6]];];
3

In[14]:= g[x_ ] = 1 - Log [x];


bisectioneb [g, 2, 3, 0.0005 ]
i a b c g [a ] g [b ] g [c ]
0 2. 3. 2.5 0.306853 - 0.0986123 0.0837093
1 2.5 3. 2.75 0.0837093 - 0.0986123 - 0.0116009
2 2.5 2.75 2.625 0.0837093 - 0.0116009 0.0349191
3 2.625 2.75 2.6875 0.0349191 - 0.0116009 0.0113886
4 2.6875 2.75 2.71875 0.0113886 - 0.0116009 - 0.000172216

METHOD OF FALSE POSITION BY DEFINING A FUNCTION

1) STOPPING CONDITION IS NUMBER OF ITERATIONS

In[16]:= falseposition [h_ , a0_ , b0_ , n0_ ] :=


Module [{a = a0, b = b0, n = n0 },
i = 0;
c = ((a * h[b]) - (b * h[a])) / (h[b] - h[a]);
OutputDetails = {{ i, N[a], N[b], N[c], N[h[a]], N[h[b]], N[h[c]]}} ;
For [i = 1, i ≤ n, i ++ ,
If [h[a] * h[c] < 0, b = c, a = c];
c = N[((a * h[b]) - (b * h[a])) / (h[b] - h[a])];
OutputDetails = Append [OutputDetails , { i, N[a], N[b], N[c], N[h[a]], N[h[b]], N[h[c]]}]] ×
Print [NumberForm [TableForm [OutputDetails ,
TableHeadings → { None , { "i", "a", "b", "c", "h[a]", "h[b]", "h[c]"}}], 6]];];

In[17]:= h[x_ ] = Cos [x] - x;


falseposition [h, 0, 1, 4]
i a b c h [a ] h [b ] h [c ]
0 0. 1. 0.685073 1. - 0.459698 0.0892993
1 0.685073 1. 0.736299 0.0892993 - 0.459698 0.00466004
2 0.736299 1. 0.738945 0.00466004 - 0.459698 0.000233926
3 0.738945 1. 0.739078 0.000233926 - 0.459698 0.0000117192
4 0.739078 1. 0.739085 0.0000117192 - 0.459698 5.87047 × 10 -7
4

2) STOPPING CONDITION IS ERRORBOUND

In[23]:= falsepositioneb [r_ , a0_ , b0_ , e0_ ] :=


Module [{a = a0, b = b0, e = e0 },
i = 0;
c = ((a * r[b]) - (b * r[a])) / (r[b] - r[a]);
OutputDetails = {{ i, N[a], N[b], N[c], N[r[a]], N[r[b]], N[r[c]]}} ;
While [Abs [r[c]] > e, i ++ ;
If [r[a] * r[c] < 0, b = c, a = c];
c = N[((a * r[b]) - (b * r[a])) / (r[b] - r[a])];
OutputDetails = Append [OutputDetails , { i, N[a], N[b], N[c], N[r[a]], N[r[b]], N[r[c]]}]] ×
Print [NumberForm [TableForm [OutputDetails ,
TableHeadings → { None , { "i", "a", "b", "c", "r[a]", "r[b]", "r[c]"}}], 6]];];

In[24]:= r[x_ ] = x ^ 3 - 2 x - 5;
falsepositioneb [r, 2, 3, 0.005 ]
i a b c r [a ] r [b ] r [c ]
0 2. 3. 2.05882 - 1. 16. - 0.3908
1 2.05882 3. 2.08126 - 0.3908 16. - 0.147204
2 2.08126 3. 2.08964 - 0.147204 16. - 0.0546765
3 2.08964 3. 2.09274 - 0.0546765 16. - 0.0202029
4 2.09274 3. 2.09388 - 0.0202029 16. - 0.00745051
5 2.09388 3. 2.09431 - 0.00745051 16. - 0.00274567
PRACTICAL-9
SECANT METHOD

1) WHEN THE STOPPING CONDITION IS NUMBER OF ITERATIONS

In[1]:= secant [f _, a0_ , b0_ , n0_ ] :=


Module [{a = a0, b = b0, n = n0 },
i = 0;
c = ((a * f[b]) - (b * f[a])) / (f[b] - f[a]);
OutputDetails = {{ i, N[c], N[f[c]]}} ;
For [i = 1, i ≤ n, i ++ , a = b; b = c;
c = N[((a * f[b]) - (b * f[a])) / (f[b] - f[a])];
OutputDetails = Append [OutputDetails , { i, N[c], N[f[c]]}]] ×
Print [NumberForm [
TableForm [OutputDetails , TableHeadings → { None , { "i", "c", "f[c]"}}], 6]];];

In[2]:= f[x_ ] = Cos [x] - x;


secant [f, 0, 1, 4]
i c f [c ]
0 0.685073 0.0892993
1 0.736299 0.00466004
2 0.739119 - 0.000057286
3 0.739085 3.52926 × 10 -8
4 0.739085 2.66787 × 10 -13

In[4]:= f[x_ ] = x ^ 3 - 2 x - 5;
secant [f, 3, 2, 4]
i c f [c ]
0 2.05882 - 0.3908
1 2.09656 0.0224281
2 2.09451 - 0.000456805
3 2.09455 - 5.15785 × 10 -7
4 2.09455 1.18856 × 10 -11
2

2)WHEN THE STOPPING CONDITION IS ERRORBOUND

In[6]:= secanteb [g _, a0_ , b0_ , e0_ ] :=


Module [{a = a0, b = b0, e = e0 },
i = 0;
c = N[((a * g[b]) - (b * g[a])) / (g[b] - g[a])];
OutputDetails = {{ i, N[c], N[g[c]]}} ;
While [Abs [g[c]] > e, i ++ ; a = b; b = c;
c = N[((a * g[b]) - (b * g[a])) / (g[b] - g[a])];
OutputDetails = Append [OutputDetails , { i, N[c], N[g[c]]}]] ×
Print [NumberForm [
TableForm [OutputDetails , TableHeadings → { None , { "i", "c", "g[c]"}}], 6]];];

In[7]:= g[x_ ] = Cos [x] - x;


secanteb [g, 0, 1, 0.0005 ]
i c g [c ]
0 0.685073 0.0892993
1 0.736299 0.00466004
2 0.739119 - 0.000057286

In[9]:= g[x_ ] = x ^ 3 - 2 x - 5;
secanteb [g, 3, 2, 0.0005 ]
i c g [c ]
0 2.05882 - 0.3908
1 2.09656 0.0224281
2 2.09451 - 0.000456805
PRACTICAL-10
NEWTON'S METHOD

1) WHEN THE STOPPING CONDITION IS NUMBER OF ITERATIONS

In[1]:= newton [r_ , n0_ , a0_ , p0_ ] :=


Module [{n = n0, a = a0, p = p0 },
i = 0;
pn = a;
OutputDetails = {{ i, N[pn ], Abs [N[pn - p]], N[r[pn ]]}} ;
For [i = 1, i ≤ n, i ++ ,
pn = N[(a - (r[a] / r '[a]))];
a = pn;
OutputDetails = Append [OutputDetails , { i, N[pn ], Abs [N[pn - p]], N[r[pn ]]}]] ×
Print [NumberForm [TableForm [OutputDetails ,
TableHeadings → { None , { "i", "pn", "|pn - p|", "r[pn ]"}}], 6]];];

In[2]:= r[x_ ] = x ^ 7 - 3;
newton [r, 4, 1, 3 ^ (1 / 7)]
i pn | pn - p | r [pn ]
0 1. 0.169931 - 2.
1 1.28571 0.115783 2.8078
2 1.19692 0.026986 0.519231
3 1.17169 0.00175824 0.0317026
4 1.16994 7.89547 × 10 -6 0.000141725

In[4]:= r[x_ ] = (1 / x) - 37;


newton [r, 4, 0.01 , 1 / 37 ]
i pn | pn - p | r [pn ]
0 0.01 0.017027 63.
1 0.0163 0.010727 24.3497
2 0.0227695 0.00425756 6.91846
3 0.0263563 0.000670691 0.941541
4 0.0270104 0.0000166436 0.0227991
2

2) WHEN THE STOPPING CONDITION IS ERRORBOUND

In[6]:= newtoneb [v _, a0_ , e0_ ] :=


Module [{a = a0, e = e0 },
i = 0;
pn = a;
OutputDetails = {{ i, N[pn ], N[v[pn ]]}} ;
While [Abs [v[pn ]] > e, i ++ ;
pn = N[(a - (v[a] / v '[a]))];
a = pn;
OutputDetails = Append [OutputDetails , { i, N[pn ], N[v[pn ]]}]] ×
Print [NumberForm [
TableForm [OutputDetails , TableHeadings → { None , { "i", "pn", "v[pn ]"}}], 6]];];

In[7]:= v[x_ ] = x ^ 7 - 3;
newtoneb [v, 1, 0.00005 ]
i pn v [pn ]
0 1. - 2.
1 1.28571 2.8078
2 1.19692 0.519231
3 1.17169 0.0317026
4 1.16994 0.000141725
5 1.16993 2.86925 × 10 -9

In[9]:= v[x_ ] = (1 / x) - 37;


newtoneb [v, 0.01 , 0.005 ]
i pn v [pn ]
0 0.01 63.
1 0.0163 24.3497
2 0.0227695 6.91846
3 0.0263563 0.941541
4 0.0270104 0.0227991
5 0.027027 0.0000140314
PRACTICAL-11
GAUSS-JACOBI METHOD

1) WHEN STOPPING CONDITION IS NUMBER OF ITERATIONS

In[1]:= Gaussjacobi [A0_ , b0_ , X0_ , iter_ ] :=


Module [{A = N[A0 ], b = N[b0 ], xk = X0, xk1, i, j, k = 0, n, m, OutputDetails },
size = Dimensions [A];
n = size [[1]];
m = size [[2]];
OutputDetails = {xk };
xk1 = Table [0, {n}];
For [k = 1, k ≤ iter, k ++ ,
For [i = 1, i ≤ n, i ++ ,
xk1 [[i]] = N[(1 / A[[i, i]])
(b[[i]] - Sum [A[[i, j]] * xk [[j]], { j, 1, i - 1}] - Sum [A[[i, j]] * xk [[j]], { j, i + 1, n}])];];
OutputDetails = Append [OutputDetails , xk1 ];
xk = xk1;
];
Print [TableForm [OutputDetails , TableHeadings → {None, None }]];];

A = {{4, 2, - 1}, {2, 4, 1}, {- 1, 1, 4}};


c = {1, - 1, 1};
X0 = {0, 0, 0};
Gaussjacobi [A, b, X0, 10 ]
0 0 0
0.25 - 0.25 0.25
0.4375 - 0.4375 0.375
0.5625 - 0.5625 0.46875
0.648438 - 0.648438 0.53125
0.707031 - 0.707031 0.574219
0.74707 - 0.74707 0.603516
0.774414 - 0.774414 0.623535
0.793091 - 0.793091 0.637207
0.805847 - 0.805847 0.646545
0.81456 - 0.81456 0.652924
2

2) WHEN STOPPING CONDITION IS ERRORBOUND

In[1]:= Gaussjacobieb [H0_ , c0_ , X0_ , error_ ] :=


Module [{H = N[H0 ], b = N[c0 ], xk = X0, xk1, i, j, k = 0, n, m, norm, OutputDetails },
size = Dimensions [H];
n = size [[1]];
m = size [[2]];
OutputDetails = {xk };
norm = 10 000 ;
xk1 = Table [0, {n}];
While [norm > error ,
For [i = 1, i ≤ n, i ++ ,
xk1 [[i]] = N[(1 / H[[i, i]])
(c[[i]] - Sum [H[[i, j]] * xk [[j]], { j, 1, i - 1}] - Sum [H[[i, j]] * xk [[j]], { j, i + 1, n}])];];
norm = Max [Abs [xk1 - xk ]];
OutputDetails = Append [OutputDetails , xk1 ];
xk = xk1;
];
Print [TableForm [OutputDetails , TableHeadings → {None, None }]];];

In[2]:= H = {{4, 2, - 1}, {2, 4, 1}, {- 1, 1, 4}};


c = {1, - 1, 1};
X0 = {0, 0, 0};
Gaussjacobieb [H, c, X0, 0.0005 ]
0 0 0
0.25 - 0.25 0.25
0.4375 - 0.4375 0.375
0.5625 - 0.5625 0.46875
0.648438 - 0.648438 0.53125
0.707031 - 0.707031 0.574219
0.74707 - 0.74707 0.603516
0.774414 - 0.774414 0.623535
0.793091 - 0.793091 0.637207
0.805847 - 0.805847 0.646545
0.81456 - 0.81456 0.652924
0.820511 - 0.820511 0.65728
0.824575 - 0.824575 0.660255
0.827352 - 0.827352 0.662288
0.829248 - 0.829248 0.663676
0.830543 - 0.830543 0.664624
0.831427 - 0.831427 0.665271
0.832032 - 0.832032 0.665714
0.832444 - 0.832444 0.666016
PRACTICAL-12
GAUSS-SEIDEL METHOD

1) WHEN STOPPING CONDITION IS NUMBER OF ITERATIONS

In[1]:= Gaussseidel [A0_ , b0_ , X0_ , iter_ ] :=


Module [{A = N[A0 ], b = N[b0 ], xk = X0, xk1, i, j, k = 0, n, m, OutputDetails },
size = Dimensions [A];
n = size [[1]];
m = size [[2]];
OutputDetails = {xk };
xk1 = Table [0, {n}];
For [k = 1, k ≤ iter, k ++ ,
For [i = 1, i ≤ n, i ++ ,
xk1 [[i]] = N[(1 / A[[i, i]])
(b[[i]] - Sum [A[[i, j]] * xk1 [[j]], { j, 1, i - 1}] - Sum [A[[i, j]] * xk [[j]], { j, i + 1, n}])];];
OutputDetails = Append [OutputDetails , xk1 ];
xk = xk1;
];
Print [TableForm [OutputDetails , TableHeadings → {None, None }]];];

In[2]:= A = {{2, - 1, 0}, {- 1, 4, 2}, {0, 2, 6}};


b = {- 1, 3, 5};
X0 = {0, 0, 0};
Gaussseidel [A, b, X0, 10 ]
0 0 0
- 0.5 0.625 0.625
- 0.1875 0.390625 0.703125
- 0.304688 0.322266 0.725911
- 0.338867 0.302327 0.732558
- 0.348836 0.296512 0.734496
- 0.351744 0.294816 0.735061
- 0.352592 0.294321 0.735226
- 0.352839 0.294177 0.735274
- 0.352911 0.294135 0.735288
- 0.352933 0.294123 0.735292
2

2)WHEN STOPPING CONDITION IS ERRORBOUND

In[6]:= Gaussseideleb [H0_ , c0_ , X0_ , error_ ] :=


Module [{H = N[H0 ], b = N[c0 ], xk = X0, xk1, i, j, k = 0, n, m, norm, OutputDetails },
size = Dimensions [H];
n = size [[1]];
m = size [[2]];
OutputDetails = {xk };
norm = 10 000 ;
xk1 = Table [0, {n}];
While [norm > error ,
For [i = 1, i ≤ n, i ++ ,
xk1 [[i]] = N[(1 / H[[i, i]])
(c[[i]] - Sum [H[[i, j]] * xk1 [[j]], { j, 1, i - 1}] - Sum [H[[i, j]] * xk [[j]], { j, i + 1, n}])];];
norm = Max [Abs [xk1 - xk ]];
OutputDetails = Append [OutputDetails , xk1 ];
xk = xk1;
];
Print [TableForm [OutputDetails , TableHeadings → {None, None }]];];

In[7]:= H = {{2, - 1, 0}, {- 1, 4, 2}, {0, 2, 6}};


c = {- 1, 3, 5};
X0 = {0, 0, 0};
Gaussseideleb [H, c, X0, 0.0005 ]
0 0 0
- 0.5 0.625 0.625
- 0.1875 0.390625 0.703125
- 0.304688 0.322266 0.725911
- 0.338867 0.302327 0.732558
- 0.348836 0.296512 0.734496
- 0.351744 0.294816 0.735061
- 0.352592 0.294321 0.735226
- 0.352839 0.294177 0.735274
PRACTICAL-13
LAGRANGE INTERPOLATING POLYNOMIAL

In[1]:= LagrangePoly [x0_ , f0_ ] :=


Module [{xi = x0, fi = f0, n, m, polynomial },
n = Length [xi ];
m = Length [fi ];
If [n ≠ m, Print ["Polynomial can not be found out"]; Return [];];
For [i = 1, i ≤ n, i ++ ,
L[i, x_] = (Product [(x - xi [[j]]) / (xi [[i]] - xi [[j]]), {j, 1, i - 1}])
(Product [(x - xi [[j]]) / (xi [[i]] - xi [[j]]), { j, i + 1, n}]);];
polynomial [x_] = Sum [L[k, x] * fi [[k]], {k, 1, n}];
Return [polynomial [x]];]

In[2]:= data = {- 1, 0, 1, 2};


fun = {5, 1, 1, 11 };
Lagpoly [x_] = LagrangePoly [data, fun ];
poly = Simplify [Lagpoly [x]]

Out[5]= 1 - 3 x + 2 x2 + x3

CALCULATING ERROR

In[6]:= LagrangePolyeb [x0_ , g0_ , e0_ , h0_ ] :=


Module [{xi = x0, gi = g0, e = N[e0 ], n, m, polynomial },
n = Length [xi ];
m = Length [gi ];
If [n ≠ m, Print ["Polynomial can not be found out"]; Return [];];
For [i = 1, i ≤ n, i ++ ,
L[i, x_] = (Product [(x - xi [[j]]) / (xi [[i]] - xi [[j]]), {j, 1, i - 1}])
(Product [(x - xi [[j]]) / (xi [[i]] - xi [[j]]), { j, i + 1, n}]);];
polynomial [x_] = Sum [L[k, x] * gi [[k]], {k, 1, n}];
p[x_] = Simplify [polynomial [x]];
error = Abs [h[e] - p[e]];
Print ["Lagrange Polynomial is = ", p[x]];
Print ["Error is = ", error ];]
2

In[7]:= data = {0, Pi / 4, Pi / 2};


fun = {0, Sin [Pi / 4], Sin [Pi / 2]};
h[x_] = Sin [x];
Lagpoly [x_] = LagrangePolyeb [data, fun, Pi / 3, h];

2 x π - 2 2 π + 4 - 1 + 2  x
Lagrange Polynomial is = -
π2

Error is = 0.0152638
PRACTICAL-14
NEWTON INTERPOLATING POLYNOMIAL

In[1]:= divideddiff [x0_ , f0_ , sv _, ev _] :=


Module [{x = x0, f = f0, i = sv, j = ev, ans },
If [i == j, Return [f[[i]]],
ans = (divideddiff [x, f, i + 1, j] - divideddiff [x, f, i, j - 1]) / (x[[j]] - x[[i]]);
Return [ans ]];];

In[2]:= x = {- 7, - 5, - 4, - 1};
f = {10, 5, 2, 10 };
divideddiff [x, f, 1, 4]
19
Out[4]=
72

In[5]:= newtonpoly [x0_ , f0_ ] :=


Module [{x1 = x0, f1 = f0, n, np, k, j},
n = Length [x1 ];
np [y _] = 0;
For [i = 1, i ≤ n, i ++ ,
prod [y _] = 1;
For [k = 1, k ≤ i - 1, k ++ ,
prod [y _] = prod [y] * (y - x1 [[k]])];
np [y _] = np [y] + divideddiff [x1, f, 1, i] * prod [y]];
Return [np [y]];];

In[6]:= x = {- 7, - 5, - 4, - 1};
f = {10, 5, 2, 10 };
poly [y _] = Simplify [newtonpoly [x, f]]
1
1700 + 1253 y + 292 y + 19 y 
2 3
Out[8]=
72
2

CALCULATING ERROR

In[9]:= divideddifference [x0_ , g0_ , sv _, ev _] :=


Module [{x = x0, g = g0, i = sv, j = ev, ans },
If [i == j, Return [g[[i]]],
ans =
(divideddifference [x, g, i + 1, j] - divideddifference [x, g, i, j - 1]) / (x[[j]] - x[[i]]);
Return [
ans ]];];

In[10]:= x = {0, Pi / 4, Pi / 2};


g = {0, Sin [Pi / 4], Sin [Pi / 2]};
N[divideddifference [x, g, 1, 3]]

Out[12]= - 0.335749

In[13]:= newtonpolynomial [x0_ , g0_ , e0_ , h0_ ] :=


Module [{x1 = x0, g1 = g0, e = N[e0 ], n, np, k, j},
n = Length [x1 ];
np [y _] = 0;
For [i = 1, i ≤ n, i ++ ,
prod [y _] = 1;
For [k = 1, k ≤ i - 1, k ++ ,
prod [y _] = prod [y] * (y - x1 [[k]])];
np [y _] = np [y] + divideddifference [x1, g, 1, i] * prod [y]];
poly [y _] = Simplify [np [y]];
error = Abs [h[e] - poly [e]];
Print ["Newton Polynomial is = ", poly [y]];
Print ["Error is = ", error ];];

In[18]:= x = {0, Pi / 4, Pi / 2};


g = {0, Sin [Pi / 4], Sin [Pi / 2]};
h[y _] = Sin [y];
newtonp [y _] = newtonpolynomial [x, g, Pi / 3, h]

2 y π - 2 2 π + 4 - 1 + 2  y
Newton Polynomial is = -
π 2

Error is = 0.0152638
PRACTICAL-15
CALCULATING 1st DERIVATIVE

USING FORMULA FOR FIRST ORDER FORWARD DIFFERENCE APPROXIMATION FOR


1st DERIVATIVE

In[1]:= forwarddiffderivative [f _, x0_ , h0_ ] :=


Module [{x = x0, h = h0, y, g},
i = 0;
g = N[(f[x0 + h] - f[x0 ]) / h];
y = N[f '[x0 ]];
e = N[Abs [g - y]];
OutputDetails = {{i, h, g, e}};
For [i = 1, i ≤ 4, i ++ ,
h = N[h / 10 ];
g = N[(f[x0 + h] - f[x0 ]) / h];
e = N[Abs [g - y]];
OutputDetails = Append [OutputDetails , {i, h, g, e}];];
Print [TableForm [OutputDetails ,
TableHeadings → {None, {"i", "h", "Approximate value ", "error "}}]];];

In[2]:= f[x_] = Log [x];


forwarddiffderivative [f, 2, 1]
i h Approximate value error
0 1 0.405465 0.0945349
1 0.1 0.487902 0.0120984
2 0.01 0.498754 0.00124585
3 0.001 0.499875 0.000124958
4 0.0001 0.499988 0.0000124996

In[4]:= f[x_] = 1 + x + x ^ 3;
forwarddiffderivative [f, 1, 1]
i h Approximate value error
0 1 8. 4.
1 0.1 4.31 0.31
2 0.01 4.0301 0.0301
3 0.001 4.003 0.003001
4 0.0001 4.0003 0.00030001
PRACTICAL-16
CALCULATING 2nd DERIVATIVE

USING FORMULA FOR SECOND ORDER CENTRAL DIFFERENCE APPROXIMATION


FOR 2nd DERIVATIVE

In[1]:= centraldiffderivative [f _, x0_ , h0_ ] :=


Module [{x = x0, h = h0, y, g},
i = 0;
g = N[(f[x0 + h] - 2 f[x0 ] + f[x0 - h]) / h ^ 2];
y = N[f '' [x0 ]];
e = N[Abs [g - y]];
OutputDetails = {{i, h, g, e}};
For [i = 1, i < 4, i ++ ,
h = N[h / 10 ];
g = N[(f[x0 + h] - 2 f[x0 ] + f[x0 - h]) / h ^ 2];
e = N[Abs [g - y]];
OutputDetails = Append [OutputDetails , {i, h, g, e}];];
Print [TableForm [OutputDetails ,
TableHeadings → {None, {"i", "h", "Approximate value ", "error "}}]];];

In[2]:= f[x_] = Log [x];


centraldiffderivative [f, 2, 1]
i h Approximate value error
0 1 - 0.287682 0.0376821
1 0.1 - 0.250313 0.000313022
2 0.01 - 0.250003 3.12505 × 10 -6
3 0.001 - 0.25 3.11212 × 10 -8

In[4]:= f[x_] = E ^ x;
centraldiffderivative [f, 0, 1]
i h Approximate value error
0 1 1.08616 0.0861613
1 0.1 1.00083 0.000833611
2 0.01 1.00001 8.33336 × 10 -6
3 0.001 1. 8.34065 × 10 -8
PRACTICAL-17
INTEGRATION

TRAPEZOIDAL RULE

In[1]:= trapezoidalrule [f _, a0_ , b0_ ] :=


Module [{a = a0, b = b0, c},
c = N[((b - a) * (f[a] + f[b])) / 2];
Print ["The value of the itegral is = ", c];];

In[2]:= f[x_] = 1 / x;
trapezoidalrule [f, 1, 2]

The value of the itegral is = 0.75

In[4]:= f[x_] = x ^ 2;
trapezoidalrule [f, 3, 4]

The value of the itegral is = 12.5

SIMPSON'S RULE

In[6]:= simpsonsrule [g _, a0_ , b0_ ] :=


Module [{a = a0, b = b0, d},
d = N[((b - a) * (g[a] + 4 g[(a + b) / 2] + g[b])) / 6];
Print ["The value of the itegral is = ", d];];

In[7]:= g[x_] = 1 / x;
simpsonsrule [g, 1, 2]

The value of the itegral is = 0.694444

In[9]:= g[x_] = x ^ 2;
simpsonsrule [g, 3, 4]

The value of the itegral is = 12.3333


PRACTICAL-18
EULER'S METHOD

In[1]:= euler [f _, w0_ , a0_ , b0_ , h0_ ] :=


Module [{w = w0, a = a0, b = b0, h = h0 },
i = 0;
p = w;
t = a;
OutputDetails = {{N[t], N[w]}};
For [i = 1, i ≤ (b - a) / h, i ++ ,
w = p + h * f[t, w];
t = a + i * h;
p = w;
OutputDetails = Append [OutputDetails , {N[t], N[w]}]] ×
Print [
NumberForm [TableForm [OutputDetails , TableHeadings → {None, {"ti", "wti"}}], 6]];];

In[2]:= f[t_ , w _] := 1 + w / t;
euler [f, 1, 1, 6, 0.5 ]
ti wti
1. 1.
1.5 2.
2. 3.16667
2.5 4.45833
3. 5.85
3.5 7.325
4. 8.87143
4.5 10.4804
5. 12.1448
5.5 13.8593
6. 15.6193
2

In[4]:= f[t_ , w _] := t / w;
euler [f, 1, 0, 5, 0.5 ]
ti wti
0. 1.
0.5 1.
1. 1.25
1.5 1.65
2. 2.10455
2.5 2.57971
3. 3.06426
3.5 3.55377
4. 4.04621
4.5 4.5405
5. 5.03604

TO CALCULATE ERROR

In[10]:= euler [z_ , w0_ , a0_ , b0_ , h0_ ] :=


Module [{w = w0, a = a0, b = b0, h = h0 },
i = 0;
p = w;
t = a;
e = N[Abs [w - g[t]]];
OutputDetails = {{N[t], N[w], N[g[t]], N[e]}};
For [i = 1, i ≤ (b - a) / h, i ++ ,
w = p + h * z[t, w];
t = a + i * h;
p = w;
e = N[Abs [w - g[t]]];
OutputDetails = Append [OutputDetails , {N[t], N[w], N[g[t]], N[e]}]] ×
Print [NumberForm [TableForm [OutputDetails ,
TableHeadings → {None, {"ti", "wti", "xti", "error "}}], 6]];];
3

In[11]:= z[t_ , w _] := 1 + w / t;
g[t_] := t * (1 + Log [t]);
euler [z, 1, 1, 6, 0.5 ]
ti wti xti error
1. 1. 1. 0.
1.5 2. 2.1082 0.108198
2. 3.16667 3.38629 0.219628
2.5 4.45833 4.79073 0.332393
3. 5.85 6.29584 0.445837
3.5 7.325 7.88467 0.55967
4. 8.87143 9.54518 0.673749
4.5 10.4804 11.2683 0.787991
5. 12.1448 13.0472 0.902348
5.5 13.8593 14.8761 1.01679
6. 15.6193 16.7506 1.13129

In[14]:= z[t_ , w _] := t / w;
g[t_] := Sqrt [t ^ 2 + 1];
euler [z, 1, 0, 5, 0.5 ]
ti wti xti error
0. 1. 1. 0.
0.5 1. 1.11803 0.118034
1. 1.25 1.41421 0.164214
1.5 1.65 1.80278 0.152776
2. 2.10455 2.23607 0.131523
2.5 2.57971 2.69258 0.112875
3. 3.06426 3.16228 0.0980191
3.5 3.55377 3.64005 0.0862816
4. 4.04621 4.12311 0.0768979
4.5 4.5405 4.60977 0.0692745
5. 5.03604 5.09902 0.0629814

You might also like