0% found this document useful (0 votes)
15 views4 pages

Practical 4

Uploaded by

vitthal.3454
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)
15 views4 pages

Practical 4

Uploaded by

vitthal.3454
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
You are on page 1/ 4

Practical 4: Numerical Differentiation

PRO4: write R program to find first order derivatives from the given data

Definition :

Numerical differentiation involves the computation of a derivative of a function f (x) from given
values of f (x) . Such formulas are basic to the numerical solution of differential equations.

Case 1 : if x 0=x

( )
dy
dx x =x
0
1
h [
= Δ y 0−
Δ 2 y 0 Δ3 y 0 Δ4 y 0
2
+
3

4
+… ]
When the point x (point to be differentiate) is the first point of given data then we use this forward
interpolation method. Where ‘h’ is the height of the given values of x.

Program in R language :

n <- 6
x <- c(1.5, 2, 2.5, 3, 3.5, 4)
y <- c(3.375, 7, 13.625, 24, 38.875, 59)

A <- matrix(c(rep(0, (n-1)^2)), nrow = (n-1), ncol= (n-1), byrow


= TRUE)

for (j in 1:(n-1)) {
for (i in 1:(n-j)) {
if (j==1)
A[i, j] <- (y[i+1] - y[i])
else
A[i, j] <- (A[i+1, j-1] - A[i, j-1])
}
}

print(A)

h <- x[2] - x[1]

sum <- A[1, 1]

for (j in 2:(n-1)) {
sum <- sum - (-1)^j * (A[1, j]/j)
}
sum <- 1/h * sum
print(sum)
Here,

As same as previous programs we calculate the forward difference table using given x and y vectors.

Sum is the output variable firstly initialized with first value of forward difference that is Δ y 0

Using for loop we iterate through the first row of forward difference table and using some
calculations we calculate all terms of the formula one by one and then add / subtract them to/from
the sum variable.

After that we divide the sum variable by h which is calculated by subtracting any two values of x

i.e. x [ 2 ]−x [ 1 ] and thus we get our output.

Output :

> print(A)

[,1] [,2] [,3] [,4] [,5]


[1,] 3.625 3.00 0.75 0 0
[2,] 6.625 3.75 0.75 0 0
[3,] 10.375 4.50 0.75 0 0
[4,] 14.875 5.25 0.00 0 0
[5,] 20.125 0.00 0.00 0 0

> print(sum)

[1] 4.75

Case 2 : if x n=x

[ ]
2 3 4

( )
dy
dx x n=x
=
1
h
∇ yn+
∇ yn ∇ yn ∇ yn
2
+
3
+
4
+…

When the point x (point to be differentiate) is the last point of given data then we use this backward
interpolation method. Where ‘h’ is the height of the given values of x.

Program in R language :

n <- 7
x <- c(1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6)
y <- c(7.989, 8.403, 8.781, 9.129, 9.451, 9.750, 10.031)

A <- matrix(c(rep(0, (n-1)^2)), nrow = (n-1), ncol= (n-1), byrow


= TRUE)
for (j in 1:(n-1)) {
for (i in 1:(n-j)) {
if (j==1)
A[i, j] <- (y[i+1] - y[i])
else
A[i, j] <- (A[i+1, j-1] - A[i, j-1])
}
}

print(A)

no <- 1.6
h <- x[2] - x[1]

sum <- A[(n-1), 1]


j <- 2

for (i in seq((n-2), 1, -1)) {


sum = sum + (A[i, j] / j)
j = j + 1
}
sum <- (1/h) * sum
print(sum)

Here,

as same as previous programs we have first calculate the backward difference table using given x and
y vectors.

Sum is the output variable firstly initialized with last value of first backward difference that is ∇ y n

Using for loop we iterate through the backward difference table diagonally (bottom left to top right)
and using some calculations we calculate all terms of the formula one by one and then add them to
the sum variable.

After that we divide the sum variable by h which is calculated by subtracting any two values of x

i.e. x [ 2 ]−x [ 1 ] and thus we get our output.


Output :

> print(A)

[,1] [,2] [,3] [,4] [,5] [,6]


[1,] 0.414 -0.036 0.006 -0.002 0.001 0.002
[2,] 0.378 -0.030 0.004 -0.001 0.003 0.000
[3,] 0.348 -0.026 0.003 0.002 0.000 0.000
[4,] 0.322 -0.023 0.005 0.000 0.000 0.000
[5,] 0.299 -0.018 0.000 0.000 0.000 0.000
[6,] 0.281 0.000 0.000 0.000 0.000 0.000
> print(sum)
[1] 2.751

You might also like