0% found this document useful (0 votes)
296 views114 pages

Assignment #1

The document is a lab assignment for Soheera Jadoon from Jinnah University for Women. It contains questions on calculus topics including finding derivatives and integrals of various functions, and limits of functions. It also includes programming problems involving finding roots of equations numerically using the Bisection and Newton's methods. The assignment contains solutions for the questions.

Uploaded by

Yumna Khalid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
296 views114 pages

Assignment #1

The document is a lab assignment for Soheera Jadoon from Jinnah University for Women. It contains questions on calculus topics including finding derivatives and integrals of various functions, and limits of functions. It also includes programming problems involving finding roots of equations numerically using the Bisection and Newton's methods. The assignment contains solutions for the questions.

Uploaded by

Yumna Khalid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JINNAH UNIVERSITY FOR WOMEN

DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING


LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

Question 1:

(a)Find the derivative of the following functions.


1. 2x + 3x2
SOLUTION

2. (x2 – 5) (x3 – 2x +3)


SOLUTION

3. (√x + 2x) (4x2 – 1)


SOLUTION

4. (x2 +1) / (5x – 3)


JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
SOLUTION

5. (x - √(x)) / (5x2 – 3)
SOLUTION

6. √x (2x -1) (x3 – x)


SOLUTION

7. (x3 + 4)4
SOLUTION
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

8. ((x -1) / (x + 3))3


SOLUTION

(b)Find the integration of the following functions


1. x3 +2x2 – x + 3
SOLUTION

2. cos x + x
SOLUTION
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

3. 8z + 4z3 − 6z2
SOLUTION

4. x √(x+1)
SOLUTION

5. √(x^3+1)
SOLUTION

6. x / x2 +1
SOLUTION
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

7.

SOLUTION

(c) Find the limit of the following functions


x 5−25
1. lim
n→ 0 x−2
SOLUTION

x 2 x −1
2. lim
n→ 0 x
SOLUTION

e 5 x −1
3. lim
n→ 0 x
SOLUTION

1
4. lim (1−5) x
n→ 0
SOLUTION

8x
log(1+ )
5. 3
lim
n→ 0 x
SOLUTION
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

Question 2:
(a) Implement the following using Bisection Method.

1. Write a program to find the root of the equation by using x3 -5x+3.


SOURCE CODE:
import [Link]; public class Main

public static void main(String[] args) {


int i = 1;

double x = 0;
double E = 0.0001;

double f_x = [Link](x,3) - 5*x + 3;

double x1, x2; x1 =


1; x2 = 2; double
abs_val, root; double
a , b, xn, f_xn;

double f_x1 = [Link](x1,3) - 5*x1 + 3;


double f_x2 = [Link](x2,3) - 5*x2 + 3;

if (f_x1 * f_x2 > 0)

[Link]("Root does not lie between "+f_x1+" and "+f_x2);

}
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
if (f_x1 < 0){
a = x1; b=
x2;

[Link]("a = "+a+" b = "+b);

} else
{ a=
x2; b=
x1;

[Link]("a = "+a+" b = "+b);

}
do{

[Link]("*************************\n Iteration number :

"+i+"\n*************************");

xn = (a + b)/2;

[Link]("xn = "+xn);
f_xn = [Link](xn,3) - 5*xn + 3;
[Link]("f_xn = "+f_xn);

if (f_xn > 0){


b = xn;

[Link]("b = "+b);

} else{
a = xn;

[Link]("a = "+a);
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
} abs_val = (b - a)/b; abs_val =
[Link](abs_val);
[Link]("abs_val = "+abs_val);
i++;

while ((abs_val < E) == false);


root = (a+b)/2;

[Link]("\n\nThe value of root: "+root);

}
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
Output
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

2. Find the 4th approximation to the solution of the equation below.


2
x −x−2=x
SOURCE CODDING

import [Link];
public class Main

public static void main(String[] args) {


int i = 1;

double x = 0;

double E = 0.0001;

double f_x = [Link](x,2) - 2*x - 2;

double x1, x2;


x1 = 2; x2 = 3;

double abs_val, root;


double a , b, xn, f_xn;
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
double f_x1 = [Link](x1,2) - 2*x1 - 2;
double f_x2 = [Link](x2,2) - 2*x2 - 2;

if (f_x1 * f_x2 > 0)

[Link]("Root does not lie between "+f_x1+" and "+f_x2);

if (f_x1 < 0){


a = x1; b=
x2;

[Link]("a = "+a+" b = "+b);

} else
{ a=
x2; b=
x1;

[Link]("a = "+a+" b = "+b);

}
do{

[Link]("\n*************************\n Iteration number :

"+i+"\n*************************");

xn = (a + b)/2;

[Link]("xn = "+xn);
f_xn = [Link](xn,2) - 2*xn - 2;
[Link]("f_xn = "+f_xn);

if (f_xn > 0){


b = xn;

[Link]("b = "+b);
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
}
else{ a
= xn;

[Link]("a = "+a);

abs_val = (b - a)/b;

abs_val = [Link](abs_val);

[Link]("abs_val = "+abs_val); i+
+;

while ((abs_val < E) == false);

root = (a+b)/2;

[Link]("\n\nThe value of root: "+root);

}
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
OUTPUT
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

(b) Implement the following using Newton's Method.

1. Find the root between 11 and 22 of 2x3−3x−6 correct to 5 decimal places.


SOURCE CODDING
import [Link];
import [Link].*; public
class Main

public static
void main(String[] args)
{ double x = 1;
int N; int i
= 1; double error;

double f_x = 2*[Link](x,3) - 3*x - 6;


double g_x = 6*[Link](x,2) - 3; double
x0, x1, abs_fx1, f_x1;

Scanner sc= new Scanner([Link]); //[Link] is a standard input stream

[Link]("\tEnter initial guess (x0): ");


x0 = [Link]();

[Link]("\tEnter tolerable error (e): ");


error = [Link]();

[Link]("\tEnter maximum iteration (N): ");

N = [Link]();
int step = 1;

do {

[Link]("\n\tIteration number:
"+i); double f_x0 = 2*[Link](x0,3) - 3*x0 - 6;
double g_x0 = 6*[Link](x0,2) - 3; if
(g_x0 == 0){

[Link]("\tMathematical Error!");
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
[Link](0);
} x1 = x0 - (f_x0/g_x0);
x0 = x1;

[Link]("\tThe value of x: "+x0);


step = step + 1; if (step > N){

[Link]("\tNot Convergent");

f_x1 = 2*[Link](x1,3) - 3*x1 - 6;


abs_fx1 = [Link](f_x1); i++;

while (abs_fx1 > error);

[Link]("\n\n\tThe root is: "+x1);

OUTPUT
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

2. Let f(x) = 3x2 – 72x – 220. Given that f(x) = 0 has a root near x = 12, compute x 1 ,starting
with x0 = 26.
SOURCE CODDING

import [Link];

import [Link].*; public

class Main

{ public static void main(String[] args) {

double x = 1; int N; int i

= 1;

double error; double f_x =

3*[Link](x,2) - 72*x - 220; double g_x = 6*x -

72; double x0, x1, abs_fx1, f_x1;

Scanner sc= new Scanner([Link]); //[Link] is a standard input stream

[Link]("\tEnter initial guess (x0): "); x0 = [Link]();

[Link]("\tEnter tolerable error (e): ");

error = [Link]();

[Link]("\tEnter maximum iteration (N): ");

N = [Link]();

int step = 1;

do {
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
[Link]("\n\tIteration number: "+i);

double f_x0 = 3*[Link](x0,2) - 72*x0 - 220;

double g_x0 = 6*x0 - 72; if (g_x0 == 0){

[Link]("\tMathematical Error!");

[Link](0);

} x1 = x0 - (f_x0/g_x0);

x0 = x1;

[Link]("\tThe value of x: "+x0);

step = step + 1; if (step > N){

[Link]("\tNot Convergent");

} f_x1 = 3*[Link](x1,2) -

72*x1 - 220; abs_fx1 = [Link](f_x1);

i++;

while (abs_fx1 > error);

[Link]("\n\n\tThe root is: "+x1);

OUTPUT
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

Question 3:

(a) Implement the following using false position method.


1. Find the root between (2, 3) of x3 - 2x - 5 = 0
SOURCE CODDING
# Defining Function
def f(x):
return x**3-2*x-5

# Implementing False Position Method


def falsePosition(x0,x1,e):
step = 1
print('\n\n*** FALSE POSITION METHOD IMPLEMENTATION ***')
condition = True
while condition:
x2 = x0 - (x1-x0) * f(x0)/( f(x1) - f(x0) )
print('Iteration-%d, x2 = %0.6f and f(x2) = %0.6f' % (step, x2, f(x2)))

if f(x0) * f(x2) < 0:


x1 = x2
else:
x0 = x2

step = step + 1
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
condition = abs(f(x2)) > e

print('\nRequired root is: %0.8f' % x2)


# Input Section
x0 = input('First Guess: ')
x1 = input('Second Guess: ')
e = input('Tolerable Error: ')

# Converting input to float


x0 = float(x0)
x1 = float(x1)
e = float(e)

#Note: You can combine above two section like this


# x0 = float(input('First Guess: '))
# x1 = float(input('Second Guess: '))
# e = float(input('Tolerable Error: '))

# Checking Correctness of initial guess values and false positioning


if f(x0) * f(x1) > 0.0:
print('Given guess values do not bracket the root.')
print('Try Again with different guess values.')
else:
falsePosition(x0,x1,e)
OUTPUT
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

2. Find the real root of the equation x3 - 4x + 1 =0


SOURCE CODDING
# Defining Function
def f(x):
return x**3-4*x+1

# Implementing False Position Method


def falsePosition(x0,x1,e):
step = 1
print('\n\n*** FALSE POSITION METHOD IMPLEMENTATION ***')
condition = True
while condition:
x2 = x0 - (x1-x0) * f(x0)/( f(x1) - f(x0) )
print('Iteration-%d, x2 = %0.6f and f(x2) = %0.6f' % (step, x2, f(x2)))

if f(x0) * f(x2) < 0:


x1 = x2
else:
x0 = x2

step = step + 1
condition = abs(f(x2)) > e

print('\nRequired root is: %0.8f' % x2)


JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

# Input Section
x0 = input('First Guess: ')
x1 = input('Second Guess: ')
e = input('Tolerable Error: ')

# Converting input to float


x0 = float(x0)
x1 = float(x1)
e = float(e)

#Note: You can combine above two section like this


# x0 = float(input('First Guess: '))
# x1 = float(input('Second Guess: '))
# e = float(input('Tolerable Error: '))

# Checking Correctness of initial guess values and false positioning


if f(x0) * f(x1) > 0.0:
print('Given guess values do not bracket the root.')
print('Try Again with different guess values.')
else:
falsePosition(x0,x1,e)
OUTPUT

(b) Implement the following using secant method.

1. A trunnion has to be cooled before it is shrink fitted into a steel hub. The equation that gives
the temperature x to which the trunnion has to be cooled to obtain the desired contraction is
given by the following equation.
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

f(x)= x2 – 2x – 15 =0
SOURCE CODDING
if f(a)*f(b) >= 0:
print("Secant method fails.")
return None
a_n = a
b_n = b
for n in range(1,N+1):
m_n = a_n - f(a_n)*(b_n - a_n)/(f(b_n) - f(a_n))
f_m_n = f(m_n)
if f(a_n)*f_m_n < 0:
a_n = a_n
b_n = m_n
elif f(b_n)*f_m_n < 0:
a_n = m_n
b_n = b_n
elif f_m_n == 0:
print("Found exact solution.")
return m_n
else:
print("Secant method fails.")
return None
return a_n - f(a_n)*(b_n - a_n)/(f(b_n) - f(a_n))
p = lambda x: x**2 - 2*x - 15
print(p(1))
print(p(2))
print(p(3))

OUTPUT
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
2. Finding roots of equation. To find the temperature x to which the trunnion has to be cooled.
conduct three iterations to estimate the root of the above equation.

SOLUTION

Iteration 1
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

Iteration 2
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

Iteration 2
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

Question 4:

(a) Implement the following using Jacobi’s method.

5 1 2 x 1 1
1. Let A= 1
[ ] [] []
2 2 5 z 3
to approximate solution to the system Ax=b.
[]
4 2 , x= y and let b= 2 . with the initial approximation x0 = 1 with n iterations
1

CODDING
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

2. Determine whether the Jacobi Iteration method will converge to the solution.

3. Solve the system of linear equations by Jacobi’s method.


x 1+ x2 + 4 x 3=9

8 x 1−3 x 2+2 x 3=20

4 x1 +11 x 2−x 3=33

SOURCE CODDING
import numpy as np
B=[]
n = int(input("Enter the number of rows in a matrix: "))
a = [[0] * n for i in range(n)]
t=len(a)
x=[1]*len(a)
c=[0]*len(a)
for i in range(n):
for j in range(n):
a[i][j] = int(input("Enter Coefficient:"))
print("Matrix of A:\n",[Link](a))
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
for i in range(t):
b=int(input("Enter Coefficient For b:"))
[Link](b)
print("Matrix of B:\n",[Link](B))
print("Matrix of X:\n",[Link](x))
print("\n**** GAUSS JACOBI METHOD ****\n")
for i in range(0,n):
c[i]=B[i]
for j in range(0,n):
if(i != j):
c[i] =c[i]- a[i][j] * x[j]
x[i] = c[i] / a[i][i]
print(x)

Question 5:
(a) Implement the following using fixed point iteration.
1 1 −x
1. Let’s take g(x) = e –x as our example. Will the iteration x n+1= e ?
n

2 2
SOURCE CODE:
#include <stdio.h>
#include <math.h>
double f(double x)
{
return x*x*x*x-3*x*x-3; //change equation for each problem
}

double g(double x)
{
return pow(3*x*x+3,.25);
}

int main()
{
double p, p0, Tol;
int i=1;
int No;
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

printf("Enter approximate p: ");


scanf ("%lf", &p0);
printf("Desired Tolerance: ");
scanf ("%lf", &Tol);
printf("Maximum Iterations: ");
scanf ("%d", &No);

while (i<=No)
{
p = g(p0);

if((fabs(p-p0))<Tol)
{
//printf("%lf", &p);
break;
}
printf("Iteration %d: Current value = %lf\n", i, p);
i++; //i=i+1
p0=p;

if (i>No)
{
printf("Method Failed after %d", No);
printf(" iterations");
}
}
}
OUTPUT
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

1
2. Let g(u) = 1+ u - u 3. Find all fixed point of g(u).
8
SOURCE CODDING
#include <stdio.h>
#include <math.h>

double f(double x)
{
return x*x*x*x-3*x*x-3; //change equation for each problem
}

double g(double x)
{
return pow(3*x*x+3,.25);
}

int main()
{
double p, p0, Tol;
int i=1;
int No;

printf("Enter approximate p: ");


scanf ("%lf", &p0);
printf("Desired Tolerance: ");
scanf ("%lf", &Tol);

printf("Maximum Iterations: ");


scanf ("%d", &No);
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

while (i<=No)
{
p = g(p0);

if((fabs(p-p0))<Tol)
{
//printf("%lf", &p);
break;
}
printf("Iteration %d: Current value = %lf\n", i, p);

i++; //i=i+1
p0=p;

if (i>No)
{
printf("Method Failed after %d", No);
printf(" iterations");
}

}
OUTPUT
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

3. Find a root of the function (𝑥) = cos(𝑥) − 𝑥 when the initial guess x0 = 0 and 𝑔(𝑥) = cos(𝑥).
SOURCE CODDING
#include <stdio.h>
#include <math.h>

double f(double x)
{
return x*x*x*x-3*x*x-3; //change equation for each problem
}

double g(double x)
{
return pow(3*x*x+3,.25);
}

int main()
{
double p, p0, Tol;
int i=0;
int No;

printf("Enter approximate p: ");


scanf ("%lf", &p0);

printf("Desired Tolerance: ");


scanf ("%lf", &Tol);

printf("Maximum Iterations: ");


scanf ("%d", &No);

while (i<=No)
{
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
p = g(p0);

if((fabs(p-p0))<Tol)
{
//printf("%lf", &p);
break;
}
printf("Iteration %d: Current value = %lf\n", i, p);

i++; //i=i+1
p0=p;

if (i>No)
{
printf("Method Failed after %d", No);
printf(" iterations");
}

}
Output

4. Let f(x) = x^3 + x^2 -1, find all fixed point of g(x).
SOURCE CODDING
#include <stdio.h>
#include <math.h>

double f(double x)
{
return x*x*x*x-3*x*x-3; //change equation for each problem
}

double g(double x)
{
return pow(3*x*x+3,.25);
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
}

int main()
{
double p, p0, Tol;
int i=0;
int No;

printf("Enter approximate p: ");


scanf ("%lf", &p0);

printf("Desired Tolerance: ");


scanf ("%lf", &Tol);

printf("Maximum Iterations: ");


scanf ("%d", &No);

while (i<=No)
{
p = g(p0);

if((fabs(p-p0))<Tol)
{
//printf("%lf", &p);
break;
}
printf("Iteration %d: Current value = %lf\n", i, p);

i++; //i=i+1
p0=p;

if (i>No)
{
printf("Method Failed after %d", No);
printf(" iterations");
}

OUTPUT
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

Question 6:
(a) Implement the following using Newton’s interpolation formula.

1. Find the value of tan 0.12 using below table.


x y = tan x

0.10 0.1003

0.15 0.1511

0.20 0.2027

0.25 0.2553

0.30 0.3093

CODDING
#include<stdio.h>

#define MAXN 100


#define ORDER 4

main()

{
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

float ax[MAXN+1], ay [MAXN+1], diff[MAXN+1][ORDER+1], nr=1.0, dr=1.0,x,p,h,yp;

int n,i,j,k;

printf("\nEnter the value of n:\n");

scanf("%d",&n);

printf("\nEnter the values in form x,y:\n");

for (i=0;i<=n;i++)

scanf("%f %f",&ax[i],&ay[i]);

printf("\nEnter the value of x for which the value of y is wanted: \n");

scanf("%f",&x);

h=ax[1]-ax[0];

//now making the difference table

//calculating the 1st order of differences

for (i=0;i<=n-1;i++)

diff[i][1] = ay[i+1]-ay[i];

//now calculating the second and higher order differences

for (j=2;j<=ORDER;j++)

for(i=0;i<=n-j;i++)

diff[i][j] = diff[i+1][j-1] - diff[i][j-1];

//now finding x0
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
i=0;

while (!(ax[i]>x))

i++;
//now ax[i] is x0 and ay[i] is y0

i--;

p = (x-ax[i])/h;

yp = ay[i];

//now carrying out interpolation

for (k=1;k<=ORDER;k++)

nr *=p-k+1;

dr *=k;

yp +=(nr/dr)*diff[i][k];

printf("\nWhen x = %6.1f, corresponding y = %6.2f\n",x,yp);

}
SOLUTION
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

2 Find f(-1/6) using below details.


x f(x)

-0.75 -0.0718125

-0.5 -0.02475

-0.25 0.3349375

0 1.101

SOURCE CODDING

#include<stdio.h>

#define MAXN 100

#define ORDER 4

main()

{
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

float ax[MAXN+1], ay [MAXN+1], diff[MAXN+1][ORDER+1], nr=1.0, dr=1.0,x,p,h,yp;

int n,i,j,k;

printf("\nEnter the value of n:\n");

scanf("%d",&n);

printf("\nEnter the values in form x,f(x):\n");

for (i=0;i<=n;i++)

scanf("%f %f",&ax[i],&ay[i]);

printf("\nEnter the value of x for which the value of f(x) is wanted: \n");

scanf("%f",&x);

h=ax[1]-ax[0];

//now making the difference table

//calculating the 1st order of differences

for (i=0;i<=n-1;i++)

diff[i][1] = ay[i+1]-ay[i];

//now calculating the second and higher order differences

for (j=2;j<=ORDER;j++)

for(i=0;i<=n-j;i++)
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
diff[i][j] = diff[i+1][j-1] - diff[i][j-1];

//now finding x0

i=0;

while (!(ax[i]>x))

i++;

//now ax[i] is x0 and ay[i] is y0

i--;

p = (x-ax[i])/h;

yp = ay[i];

//now carrying out interpolation

for (k=1;k<=ORDER;k++)

nr *=p-k+1;

dr *=k;

yp +=(nr/dr)*diff[i][k];

printf("\nWhen x = %6.1f, corresponding y = %6.2f\n",x,yp);

}
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

3. Estimate the population for the year 1925 using given below year and population details.
Year(x) Population (y)

1891 46,000

1901 66,000

1911 81,000

1921 93,000

1931 1,01,000

SOURCE CODDING

// CPP Program to interpolate using


// newton backward interpolation
#include <bits/stdc++.h>
using namespace std;

// Calculation of u mentioned in formula


float u_cal(float u, int n)
{
float temp = u;
for (int i = 1; i < n; i++)
temp = temp * (u + i);
return temp;
}

// Calculating factorial of given n


int fact(int n)
{
int f = 1;
for (int i = 2; i <= n; i++)
f *= i;
return f;
}

int main()
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
{
// number of values given
int n = 5;
float x[] = { 1891, 1901, 1911,
1921, 1931 };

// y[][] is used for difference


// table and y[][0] used for input
float y[n][n];
y[0][0] = 46,000;
y[1][0] = 66,000;
y[2][0] = 81,000;
y[3][0] = 93,000;
y[4][0] = 1,01,000;

// Calculating the backward difference table


for (int i = 1; i < n; i++) {
for (int j = n - 1; j >= i; j--)
y[j][i] = y[j][i - 1] - y[j - 1][i - 1];
}

// Displaying the backward difference table


for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++)
cout << setw(4) << y[i][j]
<< "\t";
cout << endl;
}

// Value to interpolate at
float value = 1925;

// Initializing u and sum


float sum = y[n - 1][0];
float u = (value - x[n - 1]) / (x[1] - x[0]);
for (int i = 1; i < n; i++) {
sum = sum + (u_cal(u, i) * y[n - 1][i]) /
fact(i);
}
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

cout << "\n Value at " << value << " is "
<< sum << endl;
return 0;
}
OUTPUT

Question 7:
Implement the following using Newton’s divided difference polynomial method.

1. Find the polynomial of the lowest possible degree which assumes the values -21, 15, 12, 3
when x has the values -1, 1, 2, 3, respectively. Hence find f(0).
SOURCE CODDING
# Python3 program for implementing
# Newton divided difference formula

# Function to find the product term


def proterm(i, value, x):
pro = 1;
for j in range(i):
pro = pro * (value - x[j]);
return pro;

# Function for calculating


# divided difference table
def dividedDiffTable(x, y, n):

for i in range(1, n):


for j in range(n - i):
y[j][i] = ((y[j][i - 1] - y[j + 1][i - 1]) /
(x[j] - x[i + j]));
return y;
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
# Function for applying Newton's
# divided difference formula
def applyFormula(value, x, y, n):
sum = y[0][0];

for i in range(1, n):


sum = sum + (proterm(i, value, x) * y[0][i]);

return sum;

# Function for displaying divided


# difference table
def printDiffTable(y, n):

for i in range(n):
for j in range(n - i):
print(round(y[i][j], 4), "\t",
end = " ");
print("");

# Driver Code

# number of inputs given


n = 4;
y = [[0 for i in range(10)]
for j in range(10)];
x = [ -1 , 1 , 2 , 3];

# y[][] is used for divided difference


# table where y[][0] is used for input
y[0][0] = -21;
y[1][0] = 15;
y[2][0] = 12;
y[3][0] = 3;
# calculating divided difference table
y=dividedDiffTable(x, y, n);

# displaying divided difference table


printDiffTable(y, n);

# value to be interpolated
value = 0;
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

# printing the value


print("\nValue at", value, "is",
round(applyFormula(value, x, y, n), 2))
OUTPUT

2. The observed values of a function are respectively 168, 120, 72 and 63 at the four positions 3,
7, 9 and 10 of the independent variable. What is the best estimated value of the function at the
position 6 of the independent variable?
SOURCE CODDING
# Python3 program for implementing
# Newton divided difference formula

# Function to find the product term


def proterm(i, value, x):
pro = 1;
for j in range(i):
pro = pro * (value - x[j]);
return pro;

# Function for calculating


# divided difference table
def dividedDiffTable(x, y, n):

for i in range(1, n):


for j in range(n - i):
y[j][i] = ((y[j][i - 1] - y[j + 1][i - 1]) /
(x[j] - x[i + j]));
return y;

# Function for applying Newton's


# divided difference formula
def applyFormula(value, x, y, n):

sum = y[0][0];
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
for i in range(1, n):
sum = sum + (proterm(i, value, x) * y[0][i]);
return sum;

# Function for displaying divided


# difference table
def printDiffTable(y, n):

for i in range(n):
for j in range(n - i):
print(round(y[i][j], 4), "\t",
end = " ");

print("");

# Driver Code

# number of inputs given


n = 4;
y = [[0 for i in range(10)]
for j in range(10)];
x = [ 3, 7, 9, 10];

# y[][] is used for divided difference


# table where y[][0] is used for input
y[0][0] = 168;
y[1][0] = 120;
y[2][0] = 72;
y[3][0] = 63;

# calculating divided difference table


y=dividedDiffTable(x, y, n);
# displaying divided difference table
printDiffTable(y, n);

# value to be interpolated
value = 6;

# printing the value


print("\nValue at", value, "is",
round(applyFormula(value, x, y, n), 2))
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
# displaying divided difference table
printDiffTable(y, n);

# value to be interpolated
value = 6;

# printing the value


print("\nValue at", value, "is",
round(applyFormula(value, x, y, n), 2))

OUTPUT

3. Construct the Newton Divided Difference Table for generating Newton interpolation
polynomial with the following data set:
xi yi = f(xi)

0 0

1 1

2 8

3 27

4 64

SOURCE CODDING
# Python3 program for implementing
# Newton divided difference formula

# Function to find the product term


def proterm(i, value, x):
pro = 1;
for j in range(i):
pro = pro * (value - x[j]);
return pro;
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
# Function for calculating
# divided difference table
def dividedDiffTable(x, y, n):

for i in range(1, n):


for j in range(n - i):
y[j][i] = ((y[j][i - 1] - y[j + 1][i - 1]) /
(x[j] - x[i + j]));
return y;

# Function for applying Newton's


# divided difference formula
def applyFormula(value, x, y, n):

sum = y[0][0];

for i in range(1, n):


sum = sum + (proterm(i, value, x) * y[0][i]);

return sum;

# Function for displaying divided


# difference table
def printDiffTable(y, n):

for i in range(n):
for j in range(n - i):
print(round(y[i][j], 4), "\t",
end = " ");

print("");

# Driver Code

# number of inputs given


n = 5;
y = [[0 for i in range(10)]
for j in range(10)];
x = [ 0,1,2,3,4];

# y[][] is used for divided difference


# table where y[][0] is used for input
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
y[0][0] = 0;
y[1][0] = 1;
y[2][0] = 8;
y[3][0] = 27;
y[4][0] = 64;
# calculating divided difference table
y=dividedDiffTable(x, y, n);

# displaying divided difference table


printDiffTable(y, n);
OUTPUT

4. The upward velocity of a rocket is given as a function of time in Table 1.

Table 1: Velocity as a function of time


t(s) v(t) (m/s)

0 0

10 227.04

15 362.78

20 517.35

22.5 602.97

30 901.67

Determine the value of the velocity at t = 16 seconds using first order polynomial interpolation.
SOURCE CODDING
# divided difference table

def dividedDiffTable(x, y, n):

for i in range(1, n):


JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
for j in range(n - i):

y[j][i] = ((y[j][i - 1] - y[j + 1][i - 1]) /

(x[j] - x[i + j]));

return y;

# Function for applying Newton's

# divided difference formula

def applyFormula(value, x, y, n):

sum = y[0][0];

for i in range(1, n):

sum = sum + (proterm(i, value, x) * y[0][i]);

return sum;

# Function for displaying divided

# difference table

def printDiffTable(y, n):

for i in range(n):

for j in range(n - i):

print(round(y[i][j], 4), "\t",

end = " ");


JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
print("");

# Driver Code

# number of inputs given

n = 6;

y = [[0 for i in range(10)]

for j in range(10)];

x = [ 0, 10, 15, 20, 22.5, 30];

# y[][] is used for divided difference

# table where y[][0] is used for input

y[0][0] = 0;

y[1][0] = 227.04;

y[2][0] = 362.78;

y[3][0] = 517.35;

y[4][0] = 602.97;

y[5][0] = 901.67;

# calculating divided difference table

y=dividedDiffTable(x, y, n);

# displaying divided difference table

printDiffTable(y, n);
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
# value to be interpolated

value = 16;

# printing the value

print("\nValue at", value, "is",

round(applyFormula(value, x, y, n), 2))

OUTPUT

Question 8:
(a)Implement the following using Gauss’s forward interpolation formula.

1. Given a set of points for the function y=f(x), evaluate f(33)


X 25 30 35 40

f(x) 0.25 0.3 0.33 0.37

SOURCE CODDING
# Python3 code for Gauss's Forward Formula
# importing library
import numpy as np

# function for calculating coefficient of Y


def p_cal(p, n):

temp = p;
for i in range(1, n):
if(i%2==1):
temp * (p - i)
else:
temp * (p + i)
return temp;
# function for factorial
def fact(n):
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
f=1
for i in range(2, n + 1):
f *= i
return f

# storing available data


n = 4;
x = [ 25, 30, 35, 40 ];

y = [[0 for i in range(n)]


for j in range(n)];
y[0][0] = 0.25;
y[1][0] = 0.3;
y[2][0] = 0.33;
y[3][0] = 0.37;

# Genrating Gauss's triangle


for i in range(1, n):
for j in range(n - i):
y[j][i] = [Link]((y[j + 1][i - 1] - y[j][i - 1]),4);

# Printing the Triangle


for i in range(n):
print(x[i], end = "\t");
for j in range(n - i):
print(y[i][j], end = "\t");
print("");

# Value of Y need to predict on


value = 33;

# implementing Formula
sum = y[int(n/2)][0];
p = (value - x[int(n/2)]) / (x[1] - x[0])

for i in range(1,n):
# print(y[int((n-i)/2)][i])
sum = sum + (p_cal(p, i) * y[int((n-i)/2)][i]) / fact(i)
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

print("\nValue at", value,


"is", round(sum, 4));

OUTPUT

2. Compute e1.09 from the following table.


x 1.00 1.05 1.10 1.15 1.20

ex 2.7183 2.8577 3.0042 3.1582 3.3201

SOURCE CODDING
import numpy as np

def p_cal(p, n):

temp = p;
for i in range(1, n):
if(i%2==1):
temp * (p - i)
else:
temp * (p + i)
return temp;
# function for factorial
def fact(n):
f=1
for i in range(2, n + 1):
f *= i
return f

# storing available data


n = 5;
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
x = [ 1.00, 1.05, 1.10, 1.15, 1.20 ];

y = [[0 for i in range(n)]


for j in range(n)];
y[0][0] = 2.7183;
y[1][0] = 2.8577;
y[2][0] = 3.0042;
y[3][0] = 3.1582;
y[4][0] = 3.3201;

# Genrating Gauss's triangle


for i in range(1, n):
for j in range(n - i):
y[j][i] = [Link]((y[j + 1][i - 1] - y[j][i - 1]),4);

# Printing the Triangle


for i in range(n):
print(x[i], end = "\t");
for j in range(n - i):
print(y[i][j], end = "\t");
print("");

# Value of Y need to predict on


value = 1.09;
# implementing Formula
sum = y[int(n/2)][0];
p = (value - x[int(n/2)]) / (x[1] - x[0])

for i in range(1,n):
# print(y[int((n-i)/2)][i])
sum = sum + (p_cal(p, i) * y[int((n-i)/2)][i]) / fact(i)

print("\nValue at", value,


"is", round(sum, 4));
OUTPUT
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

3. Given a set of points for the function y=f(x), evaluate f(27).


X 21 25 29 33

f(x) 18.478 17.814416.3432 15.5154

CODDING
// CPP Program to interpolate using
// newton backward interpolation
#include <bits/stdc++.h>
using namespace std;

// Calculation of u mentioned in formula


float u_cal(float u, int n)
{
float temp = u;
for (int i = 1; i < n; i++)
temp = temp * (u + i);
return temp;
}

// Calculating factorial of given n


int fact(int n)
{
int f = 1;
for (int i = 2; i <= n; i++)
f *= i;
return f;
NG

// CPP Program to interpolate using


// newton backward interpolation
#include <bits/stdc++.h>
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
using namespace std;

// Calculation of u mentioned in formula


float u_cal(float u, int n)
{
float temp = u;
for (int i = 1; i < n; i++)
temp = temp * (u + i);
return temp;
}

// Calculating factorial of given n


int fact(int n)
{
int f = 1;
for (int i = 2; i <= n; i++)
f *= i;
return f;
}

int main()
{
// number of values given
int n = 4;
float x[] = { 21, 25, 29,
33};

// y[][] is used for difference


// table and y[][0] used for input
float y[n][n];
y[0][0] = 18.478;
y[1][0] = 17.8144;
y[2][0] = 16.3432;
y[3][0] = 15.5154;

// Calculating the backward difference table


for (int i = 1; i < n; i++) {
for (int j = n - 1; j >= i; j--)
y[j][i] = y[j][i - 1] - y[j - 1][i - 1];
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
}

// Displaying the backward difference table


for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++)
cout << setw(4) << y[i][j]
<< "\t";
cout << endl;
}

// Value to interpolate at
float value = 27;

// Initializing u and sum


float sum = y[n - 1][0];
float u = (value - x[n - 1]) / (x[1] - x[0]);
for (int i = 1; i < n; i++) {
sum = sum + (u_cal(u, i) * y[n - 1][i]) /
fact(i);
}

cout << "\n Value at " << value << " is "
<< sum << endl;
return 0;
}
OUTPUT

Question 9:
(a)Implement the following using Stirling’s central difference interpolation formula.
1. Evaluate f(1.22) from the following table.
x 1.0 1.1 1.2 1.3

f(x) 8.403 8.781 9.129 9.451

SOURCE CODDING
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
# Python3 program for implementing
# Newton divided difference formula

# Function to find the product term


def proterm(i, value, x):
pro = 1;
for j in range(i):
pro = pro * (value - x[j]);
return pro;

# Function for calculating


# divided difference table
def dividedDiffTable(x, y, n):

for i in range(1, n):


for j in range(n - i):
y[j][i] = ((y[j][i - 1] - y[j + 1][i - 1]) /
(x[j] - x[i + j]));
return y;

# Function for applying Newton's


# divided difference formula
def applyFormula(value, x, y, n):
sum = y[0][0];

for i in range(1, n):


sum = sum + (proterm(i, value, x) * y[0][i]);

return sum;

# Function for displaying divided


# difference table
def printDiffTable(y, n):

for i in range(n):
for j in range(n - i):
print(round(y[i][j], 4), "\t",
end = " ");
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
print("");

# Driver Code

# number of inputs given


n = 4;
y = [[0 for i in range(10)]
for j in range(10)];
x = [ 1.0, 1.1, 1.2, 1.3 ];

# y[][] is used for divided difference


# table where y[][0] is used for input
y[0][0] = 8.403;
y[1][0] = 8.781;
y[2][0] = 9.129;
y[3][0] = 9.451;

# calculating divided difference table


y=dividedDiffTable(x, y, n);

# displaying divided difference table


printDiffTable(y, n);

# value to be interpolated
value = 1.22;

# printing the value


print("\nValue at", value, "is",
round(applyFormula(value, x, y, n), 2))
OUTPUT

2. Compute f(12.2) from the following table.


JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
x 10 11 12 13 14

f(x) 23968 28060 31788 35200 38368

SOURCE CODDING
import math

# Function to calculate value using


# Stirling formula
def Stirling(x, fx, x1, n):

y1 = 0; N1 = 1; d = 1;
N2 = 1; d2 = 1;
temp1 = 1; temp2 = 1;
k = 1; l = 1;
delta = [[0 for i in range(n)]
for j in range(n)];

h = x[1] - x[0];
s = [Link](n / 2);
a = x[s];
u = (x1 - a) / h;

# Preparing the forward difference


# table
for i in range(n - 1):
delta[i][0] = fx[i + 1] - fx[i];
for i in range(1, n - 1):
for j in range(n - i - 1):
delta[j][i] = (delta[j + 1][i - 1] -
delta[j][i - 1]);

# Calculating f(x) using the Stirling formula


y1 = fx[s];

for i in range(1, n):


if (i % 2 != 0):
if (k != 2):
temp1 *= (pow(u, k) - pow((k - 1), 2));
else:
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
temp1 *= (pow(u, 2) - pow((k - 1), 2));
k += 1;
d *= i;
s = [Link]((n - i) / 2);
y1 += (temp1 / (2 * d)) * (delta[s][i - 1] +
delta[s - 1][i - 1]);
else:
temp2 *= (pow(u, 2) - pow((l - 1), 2));
l += 1;
d *= i;
s = [Link]((n - i) / 2);
y1 += (temp2 / (d)) * (delta[s][i - 1]);
print(round(y1, 3));

# Driver Code
n = 5;
x = [10, 11, 12, 13, 14 ];
fx = [ 23968, 28060, 31788, 35200, 38368];

# Value to calculate f(x)


x1 = 1.22;
Stirling(x, fx, x1, n);
OUTPUT

3. Compute e1.3 from the following table.

x 0.1 0.6 1.1 1.6 2.1

y=ex 1.1052 1.8221 3.0042 4.9530 8.1662

CODDING

# Python3 code for Gauss's Forward Formula


# importing library
import numpy as np
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
# function for calculating coefficient of Y
def p_cal(p, n):

temp = p;
for i in range(1, n):
if(i%2==1):
temp * (p - i)
else:
temp * (p + i)
return temp;
# function for factorial
def fact(n):
f=1
for i in range(2, n + 1):
f *= i
return f

# storing available data


n = 5;
x = [ 0.1, 0.6, 1.1, 1.6, 2.1 ];

y = [[0 for i in range(n)]


for j in range(n)];
y[0][0] = 1.1052;
y[1][0] = 1.8221;
y[2][0] = 3.0042;
y[3][0] = 4.9530;
y[4][0] = 8.1662;

# Genrating Gauss's triangle


for i in range(1, n):
for j in range(n - i):
y[j][i] = [Link]((y[j + 1][i - 1] - y[j][i - 1]),4);

# Printing the Triangle


for i in range(n):
print(x[i], end = "\t");
for j in range(n - i):
print(y[i][j], end = "\t");
print("");
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

# Value of Y need to predict on


value = 1.3;

# implementing Formula
sum = y[int(n/2)][0];
p = (value - x[int(n/2)]) / (x[1] - x[0])

for i in range(1,n):
# print(y[int((n-i)/2)][i])
sum = sum + (p_cal(p, i) * y[int((n-i)/2)][i]) / fact(i)

print("\nValue at", value,


"is", round(sum, 4));
OUTPUT

Question 10:
(a)Implement the following using Everett’s interpolation.
1. Complete the following table and find f(1.7489).

x 1.72 1.73 1.74 1.75 1.76 1.77 1.78

SOURCE CODDING

#include<stdio.h>
main()
{
float x[100],y[100],a,s=1,t=1,k=0;
int n,i,j,d=1;
printf("\n\n Enter the number of the terms of the
table: ");
scanf("%d",&n);
printf("\n\n Enter the respective values of the
variables x and y: \n");
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
for(i=0; i<n; i++)
{
scanf ("%f",&x[i]);
scanf("%f",&y[i]);
}
printf("\n\n The table you entered is as follows
:\n\n");
for(i=0; i<n; i++)
{
printf("%0.3f\t%0.3f",x[i],y[i]);
printf("\n");
}
while(d==1)
{
printf(" \n\n\n Enter the value of the x to find the
respective value of y\n\n\n");
scanf("%f",&a);
for(i=0; i<n; i++)
{
s=1;
t=1;
for(j=0; j<n; j++)
{
if(j!=i)
{
s=s*(a-x[j]);
t=t*(x[i]-x[j]);
}
}
k=k+((s/t)*y[i]);
}
printf("\n\n The respective value of the variable y
is: %f",k);
printf("\n\n Do you want to continue?\n\n Press 1
to continue and any other key to exit");
scanf("%d",&d);
}
}
OUTPUT
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

(b)Implement the following using Bessel’s


central difference interpolation formula.

1. Given y20=24, y24=32, y28=35 and


y32=40. Find y25
SOURCE CODDING
#include <bits/stdc++.h>
using namespace std;

// calculating u mentioned in the formula


float ucal(float u, int n)
{
if (n == 0)
return 1;

float temp = u;
for (int i = 1; i <= n / 2; i++)
temp = temp * (u - i);

for (int i = 1; i < n / 2; i++)


temp = temp * (u + i);

return temp;
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
}

// calculating factorial of given number n


int fact(int n)
{
int f = 1;
for (int i = 2; i <= n; i++)
f *= i;
return f;
}

int main()
{
// Number of values given
int n = 4;
float x[] = {20, 24, 28, 32};

// y[][] is used for difference table


// with y[][0] used for input
float y[n][n];
y[0][0] = 24;
y[1][0] = 32;
y[2][0] = 35;
y[3][0] = 40;

// Calculating the central difference table


for (int i = 1; i < n; i++)
for (int j = 0; j < n - i; j++)
y[j][i] = y[j + 1][i - 1] - y[j][i - 1];

// Displaying the central difference table


for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++)
cout << setw(4) << y[i][j] << "\t";
cout << endl;
}

// value to interpolate at
float value = 25;

// Initializing u and sum


float sum = (y[2][0] + y[3][0]) / 2;
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

// k is origin thats is f(0)


int k;
if (n % 2) // origin for odd
k = n / 2;
else
k = n / 2 - 1; // origin for even

float u = (value - x[k]) / (x[1] - x[0]);

// Solving using bessel's formula


for (int i = 1; i < n; i++) {
if (i % 2)
sum = sum + ((u - 0.5) *
ucal(u, i - 1) * y[k][i]) /
fact(i);
else
sum = sum + (ucal(u, i) *
(y[k][i] + y[--k][i]) / (fact(i) *
2));
}

cout << "Value at " << value << " is " << sum
<< endl;

return 0;
}
OUTPUT

2. Find the values of y at x = 1.55 from


the following table.

X 1.0 1.5 2.0 2.5 3.0


JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

17.541
Y=ex 10.240012.345215.2312 19.3499
2

SOURCE CODDING

#include <bits/stdc++.h>
using namespace std;

// calculating u mentioned in the formula


float ucal(float u, int n)
{
if (n == 0)
return 1;

float temp = u;
for (int i = 1; i <= n / 2; i++)
temp = temp * (u - i);

for (int i = 1; i < n / 2; i++)


temp = temp * (u + i);

return temp;
}

// calculating factorial of given number n


int fact(int n)
{
int f = 1;
for (int i = 2; i <= n; i++)
f *= i;

return f;
}

int main()
{
// Number of values given
int n = 5;
float x[] = {1.0, 1.5, 2.0, 2.5,3.0};
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
// y[][] is used for difference table
// with y[][0] used for input
float y[n][n];
y[0][0] = 10.2400;
y[1][0] = 12.3452;
y[2][0] = 15.2312;
y[3][0] = 17.5412;
y[4][0] = 19.3499;

// Calculating the central difference


table
for (int i = 1; i < n; i++)
for (int j = 0; j < n - i; j++)
y[j][i] = y[j + 1][i - 1]
- y[j][i - 1];

// Displaying the central difference


table
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++)
cout << setw(4) <<
y[i][j] << "\t";
cout << endl;
}

// value to interpolate at
float value = 1.55;

// Initializing u and sum


float sum = (y[2][0] + y[3][0]) / 2;

// k is origin thats is f(0)


int k;
if (n % 2) // origin for odd
k = n / 2;
else
k = n / 2 - 1; // origin for even

float u = (value - x[k]) / (x[1] - x[0]);

// Solving using bessel's formula


for (int i = 1; i < n; i++) {
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
if (i % 2)
sum = sum + ((u -
0.5) *
ucal(u, i - 1)
* y[k][i]) / fact(i);
else
sum = sum + (ucal(u,
i) *
(y[k][i] + y[--
k][i]) / (fact(i) * 2));
}

cout << "Value at " << value << " is "


<< sum << endl;

return 0;
}
OUTPUT

3 . From the following table.

X 20 25 30 35 40

y=f(x) 11.499 12.7834 13.7648 14.4982 15.0463

Find f(34) using Everett’s formula


SOURCE CODDING
#include<stdio.h>
main()
{
float x[100],y[100],a,s=1,t=1,k=0;
int n,i,j,d=1;
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
printf("\n\n Enter the number of the terms of the table: ");
scanf("%d",&n);
printf("\n\n Enter the respective values of the variables x and y: \n");
for(i=0; i<n; i++)
{
scanf ("%f",&x[i]);
scanf("%f",&y[i]);
}
printf("\n\n The table you entered is as follows :\n\n");
for(i=0; i<n; i++)
{
printf("%0.3f\t%0.3f",x[i],y[i]);
printf("\n");
}
while(d==1)
{
printf(" \n\n\n Enter the value of the x to find the respective value of y\n\n\n");
scanf("%f",&a);
for(i=0; i<n; i++)
{
s=1;
t=1;
for(j=0; j<n; j++)
{
if(j!=i)
{
s=s*(a-x[j]);
t=t*(x[i]-x[j]);
}
}
k=k+((s/t)*y[i]);
}
printf("\n\n The respective value of the variable y is: %f",k);
printf("\n\n Do you want to continue?\n\n Press 1 to continue and any other key to exit");
scanf("%d",&d);
}
}
OUTPUT
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

Question 11:
(a)Implement the following using newton’s interpolation formula.

dy
1. Find at x= 1.5 from the following data.
dx
X 1.5 2.0 2.5 3.0 3.5 4.0

Y 3.375 8.0 13.625 24.0 38.875 59

SOURCE CODDING
x=[1.5 2.0 2.5 3.0 3.5 4.0]; % inputting values of x
fx=[3.375 8.0 13.625 24.0 38.875 59]; % inputting values of y
dt=zeros(6,10); % function
for i=1:6 dt(i,1)=x(i);% for loop
dt(i,2)=fx(i); % calling function
end
n=5; % number of iterations
for j=3:10
for i=1:n
dt(i,j)=dt(i+1,j-1)-dt(i,j-1)
end
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
n=n-1;
end
h=x(2)-x(1) % finding the value of h
xp=1.5; % defining the value of xp
for i=1:5
q=(xp-x(i))/h; % calculating number of intervals
if (q>0&&q<1)
p=q;
end
end
p
l=xp-(p*h)
for i=1:5
if(l==x(i))
r=i;
end
end % calculating different value of y
f0=fx(r);
f01=dt(r,3);
f02=dt(r,(3+1));
f03=dt((r),(3+2));
f04=dt((r),(3+3));
% using the forward interpolation formula
 
fp=(f0)+((p*f01)+(p*(p-1)*f02)/(2)) + ((p*(p-1)*(p-2)*f03)/(6))+((p*(p-1)*(p-2)*(p-3)*f04)/(24))

OUTPUT:
fp =-50.2500

dy
[Link] at x= 45 from the following data using newton’s interpolation formula.
dx

X 20 25 30 35 40 45

Y 354 332 291 260 231 204

SOURCE CODDING
x=[20 25 30 35 40 45]; % inputting the values of x
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
fx=[354332 291 260 231 204]; % inputting the value of y
dt=zeros(6,7); % declaring function
for i=1:6 % stating loop
dt(i,1)=x(i);
dt(i,2)=fx(i);
end
n=5;
for j=3:7
for i=1:n % using for loop
dt(i,j)=dt(i+1,j-1)-dt(i,j-1) % defining dt
end n=n-1;
end
h=x(2)-x(1) % finding the value of h
xp=27; % defining xp
for i=1:6
q=(xp-x(i))/h;
if (q>0&&q<1)
p=q;
end
end
p
l=xp-(p*h)
for i=1:6
if(l==x(i))
r=i;
end
end
% finding different value of y
 
f0=fx(r);
f01=dt((r-1),3);
f02=dt((r-2),(3+1));
f03=dt((r-3),(3+2));
f04=dt((r-4),(3+3));
%using backward difference formula
 
fp=(f0)+((p*f01)+(p*(p+1)*f02)/(2)) + ((p*(p+1)*(p+2)*f03)/(6))
OUTPUT:
fp=-4.800

3. The distance covered by athlete for the 50-meter race is given in the following table:
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

X 0 1 2 3 4 5

Y 0 2.5 8.5 15.5 24.5 36.5

SOURCE CODDING
x=[0 1 2 3 4 5]; % inputting the values of x
fx=[0 2.5 8.5 15.5 24.5 36.5]; % inputting the value of y
dt=zeros(6,7); % declaring function
for i=1:6 % stating loop
dt(i,1)=x(i);
dt(i,2)=fx(i);
end
n=5;
for j=3:7
for i=1:n % using for loop
dt(i,j)=dt(i+1,j-1)-dt(i,j-1) % defining dt
end n=n-1;
end
h=x(2)-x(1) % finding the value of h
xp=27; % defining xp
for i=1:6
q=(xp-x(i))/h;
if (q>0&&q<1)
p=q;
end
end
p
l=xp-(p*h)
for i=1:6
if(l==x(i))
r=i;
end
end
% finding different value of y
 
f0=fx(r);
f01=dt((r-1),3);
f02=dt((r-2),(3+1));
f03=dt((r-3),(3+2));
f04=dt((r-4),(3+3));
%using backward difference formula
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
fp=(f0)+((p*f01)+(p*(p+1)*f02)/(2)) + ((p*(p+1)*(p+2)*f03)/(6))

OUTPUT
13.8333

4. A slider in machine moves along a fixed straight rod. Its distance x(in cm) along the rod is given
at various time t(in secs).

X 0 0.1 0.2 0.3 0.4 0.5 0.6

Y 30.28 31.43 32.98 33.54 33.97 33.48 32.13

dy
Evaluate at t=0.1 and t=0.5.
dx
SOURCE CODDING
%Newton’s Backward Difference Formula MATLAB Program
 
x=[0 0.1 0.2 0.3 0.4 0.5 0.6]; % inputting the values of x
fx=[30.28 31.43 32.98 33.54 33.97 33.48 32.13]; % inputting the value of y
dt=zeros(6,7); % declaring function
for i=1:6 % stating loop
dt(i,1)=x(i);
dt(i,2)=fx(i);
end
n=5;
for j=3:7
for i=1:n % using for loop
dt(i,j)=dt(i+1,j-1)-dt(i,j-1) % defining dt
end n=n-1;
end
h=x(2)-x(1) % finding the value of h
xp=27; % defining xp
for i=1:6
q=(xp-x(i))/h;
if (q>0&&q<1)
p=q;
end
end
p
l=xp-(p*h)
for i=1:6
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
if(l==x(i))
r=i;
end
end
% finding different value of y
f0=fx(r);
f01=dt((r-1),3);
f02=dt((r-2),(3+1));
f03=dt((r-3),(3+2));
f04=dt((r-4),(3+3));
%using backward difference formula
 
fp=(f0)+((p*f01)+(p*(p+1)*f02)/(2)) + ((p*(p+1)*(p+2)*f03)/(6))
OUTPUT:
-16.25

Question 12:
Implement the following using Stirling’s interpolation formula and Bessel’s formula.

1. The distance covered by an athlete for the 50-metre race is given in the following table:

Time (sec) 0 1 2 3 4 5 6

Distance(metre) 0 2.5 8.5 15.5 24.5 36.5 50

Determine dy/dx at t=3.3 sce.

SOURCE CODDING

## Python3 Program to interpolate

# using Bessel's interpolation

# calculating u mentioned in the

# formula

def ucal(u, n):


JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
if (n == 0):

return 1;

temp = u;

for i in range(1, int(n / 2 + 1)):

temp = temp * (u - i);

for i in range(1, int(n / 2)):

temp = temp * (u + i);

return temp;

# calculating factorial of

# given number n

def fact(n):

f = 1;

for i in range(2, n + 1):

f *= i;

return f;

# Number of values given

n = 7;

Time = [0, 1, 2, 3, 4, 5,6];


JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
# y[][] is used for difference

# table with y[][0] used for input

Distance = [[0 for i in range(n)]

for j in range(n)];

Distance[0][0] = 0;

Distance[1][0] = 2.5;

Distance[2][0] = 8.5;

Distance[3][0] = 15.5;

Distance[4][0] = 24.5;

Distance[5][0] = 36.5;

Distance[6][0] = 50;

# Calculating the central

# difference table

for i in range(1, n):

for j in range(n - i):

Distance[j][i] = Distance[j + 1][i - 1] - Distance[j][i - 1];

# Displaying the central

# difference table

for i in range(n):

for j in range(n - i):

print(Distance[i][j], "\t", end = " ");

print("");
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
# value to interpolate at

value = 3.3;

# Initializing u and sum

sum = (Distance[2][0] + Distance[3][0]) / 2;

# k is origin thats is f(0)

k = 0;

if ((n % 2) > 0): # origin for odd

k = int(n / 2);

else:

k = int(n / 2 - 1); # origin for even

u = (value - Time[k]) / (Time[1] - Time[0]);

# Solving using bessel's formula

for i in range(1, n):

if (i % 2):

sum = sum + ((u - 0.5) *

ucal(u, i - 1) *

Distance[k][i]) / fact(i);

else:

sum = sum + (ucal(u, i) * (Distance[k][i] +


JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
Distance[k - 1][i]) / (fact(i) * 2));

k -= 1;

print("Value at", value, "is", round(sum, 5));

# This code is contributed by mits

OUTPUT:

2. From the following table of values of x and y, obtain dy/dx and d 2y/dx2 for x=1.7.

X 1.0 1.2 1.4 1.6 1.8 2.0 2.2

Y 2.7183 3.3201 4.0552 4.9530 6.0496 7.3891 9.0250

SOURCE CODDING

# Python3 Program to interpolate

# using Bessel's interpolation

# calculating u mentioned in the

# formula

def ucal(u, n):


JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

if (n == 0):

return 1;

temp = u;

for i in range(1, int(n / 2 + 1)):

temp = temp * (u - i);

for i in range(1, int(n / 2)):

temp = temp * (u + i);

return temp;

# calculating factorial of

# given number n

def fact(n):

f = 1;

for i in range(2, n + 1):

f *= i;

return f;

# Number of values given

n = 7;
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
x = [1.0, 1.2, 1.4, 1.6, 1.8, 2.0,2.2];

# y[][] is used for difference

# table with y[][0] used for input

y = [[0 for i in range(n)]

for j in range(n)];

y[0][0] = 2.7183;

y[1][0] = 3.3201;

y[2][0] = 4.0552;

y[3][0] = 4.9530;

y[4][0] = 6.0496;

y[5][0] = 7.3891;

y[6][0] = 9.0250;

# Calculating the central

# difference table

for i in range(1, n):

for j in range(n - i):

y[j][i] = y[j + 1][i - 1] - y[j][i - 1];

# Displaying the central

# difference table

for i in range(n):

for j in range(n - i):

print(y[i][j], "\t", end = " ");


JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
print("");

# value to interpolate at

value = 1.7;

# Initializing u and sum

sum = (y[2][0] + y[3][0]) / 2;

# k is origin thats is f(0)

k = 0;

if ((n % 2) > 0): # origin for odd

k = int(n / 2);

else:

k = int(n / 2 - 1); # origin for even

u = (value - x[k]) / (x[1] - x[0]);

# Solving using bessel's formula

for i in range(1, n):

if (i % 2):

sum = sum + ((u - 0.5) *

ucal(u, i - 1) *

y[k][i]) / fact(i);
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
else:

sum = sum + (ucal(u, i) * (y[k][i] +

y[k - 1][i]) / (fact(i) * 2));

k -= 1;

print("Value at", value, "is", round(sum, 5));

# This code is contributed by mits

OUTPUT

3. Find f’(7.5) from the following table:

X 7.47 7.48 7.49 7.50 7.51 7.51 7.52

f(x) 0.193 0.195 0.198 0.201 0.203 0.206 0.208

SOURCE CODDING

# Python3 Program to interpolate

# using Bessel's interpolation

# calculating u mentioned in the

# formula

def ucal(u, n):

if (n == 0):

return 1;
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
temp = u;

for i in range(1, int(n / 2 + 1)):

temp = temp * (u - i);

for i in range(1, int(n / 2)):

temp = temp * (u + i);

return temp;

# calculating factorial of

# given number n

def fact(n):

f = 1;

for i in range(2, n + 1):

f *= i;

return f;

# Number of values given

n = 7;

x = [7.47,7.48, 7.49,7.50,7.51,7.51,7.52];

# y[][] is used for difference

# table with y[][0] used for input


JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
y = [[0 for i in range(n)]

for j in range(n)];

y[0][0] =0.193;

y[1][0] = 0.195;

y[2][0] = 0.198;

y[3][0] = 0.201;

y[4][0] = 0.203;

y[5][0] = 0.206;

y[6][0] = 0.208;

# Calculating the central

# difference table

for i in range(1, n):

for j in range(n - i):

y[j][i] = y[j + 1][i - 1] - y[j][i - 1];

# Displaying the central

# difference table

for i in range(n):

for j in range(n - i):

print(y[i][j], "\t", end = " ");

print("");

# value to interpolate at

value = 7.5;
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
# Initializing u and sum

sum = (y[2][0] + y[3][0]) / 2;

# k is origin thats is f(0)

k = 0;

if ((n % 2) > 0): # origin for odd

k = int(n / 2);

else:

k = int(n / 2 - 1); # origin for even

u = (value - x[k]) / (x[1] - x[0]);

# Solving using bessel's formula

for i in range(1, n):

if (i % 2):

sum = sum + ((u - 0.5) *

ucal(u, i - 1) *

y[k][i]) / fact(i);

else:

sum = sum + (ucal(u, i) * (y[k][i] +

y[k - 1][i]) / (fact(i) * 2));

k -= 1;

print("Value at", value, "is", round(sum, 5));


JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

# This code is contributed by mits

OUTPUT:

4. Find f’(28) given that y20 = 48234, y25 = 47354, y30 = 44978 and y35 = 43389.
SOURCE CODDING

# Python3 program to demonstrate Stirling

# Approximation

import math

# Function to calculate value using

# Stirling formula

def Stirling(x, fx, x1, n):

y1 = 0; N1 = 1; d = 1;

N2 = 1; d2 = 1;

temp1 = 1; temp2 = 1;

k = 1; l = 1;

delta = [[0 for i in range(n)]

for j in range(n)];

h = x[1] - x[0];

s = [Link](n / 2);

a = x[s];
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
u = (x1 - a) / h;

# Preparing the forward difference

# table

for i in range(n - 1):

delta[i][0] = fx[i + 1] - fx[i];

for i in range(1, n - 1):

for j in range(n - i - 1):

delta[j][i] = (delta[j + 1][i - 1] -

delta[j][i - 1]);

# Calculating f(x) using the Stirling formula

y1 = fx[s];

for i in range(1, n):

if (i % 2 != 0):

if (k != 2):

temp1 *= (pow(u, k) - pow((k - 1), 2));

else:

temp1 *= (pow(u, 2) - pow((k - 1), 2));

k += 1;

d *= i;

s = [Link]((n - i) / 2);

y1 += (temp1 / (2 * d)) * (delta[s][i - 1] +

delta[s - 1][i - 1]);


JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
else:

temp2 *= (pow(u, 2) - pow((l - 1), 2));

l += 1;

d *= i;

s = [Link]((n - i) / 2);

y1 += (temp2 / (d)) * (delta[s][i - 1]);

print(round(y1, 3));

# Driver Code

n = 4;

x = [20, 25, 30, 35];

fx = [ 48234, 47354, 44978, 43389];

# Value to calculate f(x)

x1 = 28;

Stirling(x, fx, x1, n);

# This code is contributed by mits

OUTPUT:

Question 13:
Implement the following using Numerical Differentiation using Lagrange’s Interpolation.
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
[Link] from following table the value of the derivative of y = f(x) at  

X 0.4 0.6 0.7

Y 3.3836494 4.2442376 4.7275054

SOURCE CODDING

# Lagrange Interpolation
# Importing NumPy Library
import numpy as np

# Reading number of unknowns


n = int(input('Enter number of data points: '))

# Making numpy array of n & n x n size and initializing


# to zero for storing x and y value along with differences of y
x = [Link]((n))
y = [Link]((n))

# Reading data points


print('Enter data for x and y: ')
for i in range(n):
x[i] = float(input( 'x['+str(i)+']='))
y[i] = float(input( 'y['+str(i)+']='))

# Reading interpolation point


xp = float(input('Enter interpolation point: '))

# Set interpolated value initially to zero


yp = 0

# Implementing Lagrange Interpolation


for i in range(n):

p=1

for j in range(n):
if i != j:
p = p * (xp - x[j])/(x[i] - x[j])

yp = yp + p * y[i]

# Displaying output
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
print('Interpolated value at %.3f is %.3f.' % (xp, yp))

OUTPUT:

[Link] from following table the value of the derivative of y=f(x) at x=1.7489.

X 1.73 1.74 1.75 1.76 1.77

Y 1.772844100 1.155204006 1.737739435 1.720448638 1.703329888

SOURCE CODDING

# Lagrange Interpolation
# Importing NumPy Library
import numpy as np

# Reading number of unknowns


n = int(input('Enter number of data points: '))

# Making numpy array of n & n x n size and initializing


# to zero for storing x and y value along with differences of y
x = [Link]((n))
y = [Link]((n))

# Reading data points


print('Enter data for x and y: ')
for i in range(n):
x[i] = float(input( 'x['+str(i)+']='))
y[i] = float(input( 'y['+str(i)+']='))

# Reading interpolation point


xp = float(input('Enter interpolation point: '))
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

# Set interpolated value initially to zero


yp = 0

# Implementing Lagrange Interpolation

for i in range(n):

p=1

for j in range(n):
if i != j:
p = p * (xp - x[j])/(x[i] - x[j])

yp = yp + p * y[i]

# Displaying output

print('Interpolated value at %.3f is %.3f.' % (xp, yp))


OUTPUT:

[Link] the value of y’ at x = 0 given some set of values (-2, 5), (1, 7), (3, 11), (7, 34)?

SOURCE CODDING

# Lagrange Interpolation
# Importing NumPy Library
import numpy as np
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
# Reading number of unknowns
n = int(input('Enter number of data points: '))

# Making numpy array of n & n x n size and initializing


# to zero for storing x and y value along with differences of y
x = [Link]((n))
y = [Link]((n))

# Reading data points


print('Enter data for x and y: ')
for i in range(n):
x[i] = float(input( 'x['+str(i)+']='))
y[i] = float(input( 'y['+str(i)+']='))

# Reading interpolation point


xp = float(input('Enter interpolation point: '))

# Set interpolated value initially to zero


yp = 0

# Implementing Lagrange Interpolation


for i in range(n):

p=1

for j in range(n):
if i != j:
p = p * (xp - x[j])/(x[i] - x[j])

yp = yp + p * y[i]

# Displaying output
print('Interpolated value at %.3f is %.3f.' % (xp, yp))

OUTPUT:
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

Question 14:
Implement the following using trapezoidal rule.

1. To estimate the below integral of f(x) with n= 8

∫ √1+ x 2 dx
1

Source Code:
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define f(x) (√(1+x^2 ))

void trapezoidalRule (){


double a, b, h, ifx = 0.0, i;
int n;
printf("Enter the lower and upper limits of the integral: ");
scanf("%lf", &a);
scanf("%lf", &b);
printf("Enter the number of subintervals you want: ");
scanf("%d", &n);
h = fabs(b - a) / n;
ifx = ifx + f(a) + f(b);
for (i = a+h; i < b;){
ifx = ifx + (2 * f(i));
i = i + h;
}
ifx = ifx * h / 2;
printf("\nThe integral of the equation using Trapezoidal Rule is %lf\n", ifx);
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
}

int main (){


trapezoidalRule();
}

Output:

2. Approximate the integral of f(x) = e-x on [0, 10]

SOURCE CODDING.

#include<iostream>
#include<cmath>
using namespace std;

float mathFunc(float x) {
return (exp(-x));
}

float integrate(float a, float b, int n) {


float h, sum;
int i;
h = (b-a)/n; //calculate the distance between two interval
sum = (mathFunc(a)+mathFunc(b))/2; //initial sum using f(a) and f(b)

for(i = 1; i<n; i++) {


sum += mathFunc(a+i*h);
}
return (h*sum); //The result of integration
}

main() {
float result, lowLim, upLim;
int interval;
cout << "Enter Lower Limit, Upper Limit and interval: "; cin >>lowLim >>upLim >>interval;
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
result = integrate(lowLim, upLim, interval);
cout << "The answer is: " << result;
}
OUTPUT:

3. A solid of revolution is formed by rotating about x-axis, the area between x-axis,
the line and a curve through the points with the following coordinates:

X 0 0.25 0.50 0.75 1

y=ex 1 0.5846 0.5586 0.5085 0.7328

Estimate the volume of solid formed, giving the answer up to 3 decimal places.

Question 15:
Implement the following using Simpson’s 1/3rd Rule and 3/8 rule.
1. Find the approximate value of x and the distance covered by a rocket from t=8 to t=30 is given by

30
  140000  
x    2000 ln    9.8t dt
8 140000  2100t  

SOURCE CODDING

public class GfG{


JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
// Function to calculate f(x)

static float func(float x)

return (float)[Link](x);

// Function for approximate integral

static float simpsons_(float a, float b,

int n)

{// Calculating the value of h

float h = (b - a) / n;

// Array for storing value of x

// and f(x)

float[] x = new float[10];

float[] fx= new float[10];

// Calculating values of x and f(x)


JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
for (int i = 0; i <= n; i++) {

x[i] = ((b-a/6)*(f(8)+4*(f(19))+f(30)));

fx[i] = func(x[i]);

// Calculating result

float res = 0;

for (int i = 0; i <= n; i++) {

if (i == 0 || i == n)

res += fx[i];

else if (i % 2 != 0)

res += 4 * fx[i];

else

res += 2 * fx[i];

res = res * (h / 2);

return res;

}
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

// Driver Code

public static void main(String s[])

// Lower limit

float lower_limit = 8;

// Upper limit

float upper_limit = 30;

// Number of interval

int n = 4;

[Link](simpsons_(lower_limit,

upper_limit, n));

OUTPUT

//1106.72
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

2. A company advertises that every roll of toilet paper has at least 250 sheets. The probability that
there are 250 or more sheets in the toilet paper is given by:


P y  250    0.3515 e 0.3881 y  252.2  dy
2

250

SOURCE CODDING

public class GfG{

// Function to calculate f(x)

static float func(float x)

return (float)[Link](x);

// Function for approximate integral

static float simpsons_(float a, float b,

int n)

// Calculating the value of h


JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
float h = (b - a) / n;

// Array for storing value of x

// and f(x)

float[] x = new float[10];

float[] fx= new float[10];

// Calculating values of x and f(x)

for (int i = 0; i <= n; i++) {

x[i] = a + i * h;

fx[i] = func(x[i]);

// Calculating result

float res = 0;

for (int i = 0; i <= n; i++) {

if (i == 0 || i == n)

res += fx[i];

else if (i % 2 != 0)
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
res += 4 * fx[i];

else

res += 2 * fx[i];

res = res * (h / 3);

return res;

// Driver Code

public static void main(String s[])

// Lower limit

float lower_limit = 250;

// Upper limit

float upper_limit = 270;

// Number of interval
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
int n = 4;

[Link](simpsons_(lower_limit,

upper_limit, n));

OUTPUT

2 0.17907
4 0.20133
6 1.0090
8 1.2042
10 1.0954

Question 16:
(a) Implement the following using Richardson Extrapolation
1. Evaluate numerically the derivative of

f(x) = xcos(x) at x = 0.6 ,with h=0.1.

SOURCE CODDING

from math import *


def zeros(n,m): # Zeros matrix for preallocation
Z=[]
for i in range(n):
[Link]([0]*m)
return Z
def D(Func,a,h):
return (Func(a+h)-Func(a-h))/(2*h)
def Richardson_dif(func,a):
k=9
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
L=zeros(k,k)

for I in range(k):
L[I][0]=D(func,a,1/(2**(I+1)))
for j in range(1,k):
for i in range(k-j):
L[i][j]=((4**(j))*L[i+1][j-1]-L[i][j-1])/(4**(j)-1)
return L[0][k-1]

print('Numerical differentiation of Func=x^cox(x) at x=0.6 is:')


print('%04.20f'%Richardson_dif(lambda x:pow(x,cos(x)) ,0.6))
print('diff(cox(x)-xsin(x)) is equal to = %04.20f'%Richardson_dif(lambda x:
2**cos(x)-x*sin(x),pi/3))

OUTPUT:

1.09

-2.23

2. Evaluate the following integral.

8
I =∫ (−0.055 x 4+ 0.86 x3 −4.2 x 2+6.3 x +2)dx
0

SOURCE CODDING

import [Link];
import static [Link];
public class richardinte {
static double fun(float x)
{
double temp = -0.055*[Link](x,4)+0.8*[Link](x,3)-
4.2*[Link](x,2)+6.3*x+2;
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
return temp;
}
public static void main(String[]args)
{
Scanner in=new Scanner([Link]);
[Link]("Write lower limit a and upper limit b:");
float a = [Link]();
float b = [Link]();
[Link]("Write four subintervals: ");
float[] n=new float[4];
double[] av=new double[4];
float n1 = [Link]();
float n2 = [Link]();
float n3 = [Link]();
float n4 = [Link]();
float h=0;
h=(b-a)/n1;
double sum=0;
sum=fun(a)+fun(b);
for (int i=1; i<n1; i++)
{sum +=2*fun(a+i);
}
double result1 =sum*h/2;
h=(b-a)/n2;
double sum2=0;
sum2=fun(a)+fun(b);
for (int i=1; i<n2; i++){
sum2 +=2*fun(a+i);
}
double result2 =sum2*h/2;
h=(b-a)/n3;
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
double sum3=0;
sum3=fun(a)+fun(b);
for (int i=1; i<n3; i++)
{sum3 +=2*fun(a+i);
}
double result3 =sum3*h/2;
h=(b-a)/n4;
double sum4=0;
sum4=fun(a)+fun(b);
for (int i=1; i<n4; i++)
{sum4 +=2*fun(a+i);
}
double result4 =sum4*h/2;
double tv=2000*[Link](b)-((2000*[Link](b)*14000)/2100)*[Link](b)-
(9.8/2)*b*b;
if(tv<result1 || tv==result1)
{
[Link]("True Value of the given function is "+round(tv,3)+"\n Which is
very close to actual value"+round(result1,3));
}
else if(tv<result2 || tv==result2)
{
[Link]("True Value of the given function is "+round(tv,3)+"\n Which is
very close to actual value"+round(result2,3));
}
else if(tv<result3 || tv==result3)
{
[Link]("True Value of the given function is "+round(tv,3)+"\n Which is
very close to actual value"+round(result3,3));
}
else if(tv<result4 || tv==result4)
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018
{
[Link]("True Value of the given function is "+round(tv,3)+"\n Which is
very close to actual value"+round(result4,3));
}
}}

OUTPUT:

3.0

3. Consider the following data

T(sec.) 0 0.1 0.2 0.3 0.4 0.5

Dist(m) 0 1 2 2.5 3 4

Use Richardson extrapolation method to obtain the highest accurate value of estimating the speed at
t=0.2 seconds.
JINNAH UNIVERSITY FOR WOMEN
DEPARTMENT of COMPUTER SCIENCE & SOFTWARE ENGINEERING
LAB ASSIGNMENT
SOHEERA JADOON
23004
BSSE 2018

You might also like