MP Lab File 02
MP Lab File 02
THEORY—
The Taylor series or Taylor expansion of a function is an infinite sum of terms that are
expressed in terms of the function's derivatives at a single point. A one-dimensional Taylor
series is an expansion of a real function f(x) about a point x=a is given by-
The partial sum formed by the first n + 1 terms of a Taylor series is a polynomial of degree n
that is called the nth Taylor polynomial of the function. Taylor polynomials are
approximations of a function, which become generally better as n increases.
If the Taylor series of a function is convergent , its sum is the limit of the infinite sequence
of the Taylor polynomials.
Truncation error– The difference between the true or analytical derivative of the value of a
function and the value obtained from numerical approximation is the truncation error.
INPUT:-
i) exp(x)
ALGORITHM–
result 0.0
for i in range(n):
coeff=1/np.math.factorial(1)
term=coeff* x**i
result += term
return result
Compute the exponential values using the Taylor series jpproximation with different orders
y_values = [ ]
for n in n_values:
y=[taylor_exp(val, n) for val in x]
y_values.append(y)
plt.figure(figsize(10, 6))
for i, y in enumerate(y_values):
plt.plot(x, y, label f'Taylor Series (Order (n_values[i]}}')
plt.xlabel('x')
plt.ylabel('y')
plt.legend() plt.grid(True)
plt.show()
OUTPUT–
ii) sin(x)
ALGORITHM–
Input–
import numpy as np
result = 0.0
for i in range(n):
coeff = (-1)**1/np.math.factorial(2*1 + 1)
term = coeff * x**(2*1+1)
result += term
return result
#Compute the sine values using the Taylor series approximation with different order
y_values = []
for n in n_values:
plt.figure(figsize=(10, 6))
for i, y in enumerate(y_values):
plt.xlabel('x')
plt.ylabel('y')
plt.title('Taylor Series Approximation of sin(x)')
plt.legend()
plt.grid(True)
plt.show()
OUTPUT–
iii) cosx
ALGORITHM–
Import numpy and matplotlib. 2)Define a function taylor_sin
Use loop to implement the taylor series for sin 4)Use linspace to create a range
of x values
Create a list for various orders of taylor series to be obtained
Use looping and list comprehension to create a separate list of corresponding y
values 7)Plot the results using the grid function
Adjust the figure size accordingly
INPUT–
import numpy as np
for i in range(n):
return result
#Compute the cosine values using the Taylor series approximation with different orders
y_values = []
for n in n_values:
plt.figure(figsize=(10,6))
plt.plot(x, np.cos(x), label='cos(x)') # True cosine function
for i, y in enumerate(y_values):
plt.xlabel('x ')
plt.ylabel('y')
plt.title('Taylor Series Approximation of cos(x)')
plt.legend()
plt.grid(True)
plt.show()
OUTPUT–
UNIT 5–
THEORY—-
Trapezoidal method—
Trapezoidal Rule is a rule of numerical integration that evaluates the area under the curves
by dividing the total area into smaller trapezoids rather than using rectangles. This
integration works by approximating the region under the graph of a function as a trapezoid,
and it calculates the area. This rule takes the average of the left and the right sum.
The integration of a function y for an interval from x0 to xn can be given by the trapezoidal
rule as
The geometrical significance of this rule is that the curve y = f (x) is replaced by n straight
lines joining the points (x0, y0) and (x1, y1); (x1, y1) and (x2, y2), >, (xn−1, yn−1) and (xn,
yn). The area bounded by the curve y = f (x), the ordinates x = x0 and x = xn, and the x-axis is
then approximately equivalent to the sum of the areas of the n trapeziums obtained.
Simpson’s 1/3rd rule is an extension of the trapezoidal rule in which the integrand is
approximated by a second-order polynomial.Simpson’s rule methods are more accurate
than the other numerical approximations.
It should be noted that this rule requires the division of the whole range into an even
number of subintervals of width h.
ALGORITHM–
4) Create an array with constant acceleration values using numpy ones function
5) Define a function for velocity and implement the trapezoidal formula for numerical
integration.
7) Define a function for position and implement the trapezoidal formula for numerical
integration.
INPUT CODE–
import numpy as np
list=[]
x=0
list.append(x)
x=x+0.1
t=np.array(list)
n=len(t)
a=9.8*np.ones(n)
def v(i):
if i<0:
return 0
else:
return ((0.05%(a[i-1]+a[i]))+v(i-1))
list2 = []
for i in range(0,101):
list2.append(v(i))
v=np.array(list2)
def p(1):
if i<0:
return 0
else:
return ((0.05*(v[i-1]+v[i]))+p(1-1))
list3=[]
for i in range(0,101):
list3.append(p(i))
p=np.array(list3)
print(v)
print(p)
plt.plot(t,v,label="Velocity", c="r")
plt.plot(t,p, label="posiiton",c="b")
plt.show()
PROGRAMME—14
AIM– Verify the properties of Delta Dirac Function using the representation as a sequence of
functions.
Theory—
Delta Dirac Function
The Dirac delta function is a generalized function that is best used to model behaviors similar
to probability distributions and impulse graphs.
It is represented as–
1) Property 1— the dirac delta function is equal to 0 when x is not equal to xo.
2) Property 2— By integrating the Dirac delta function, we can show that the function is
equal to 1 within the allowed interval.
3) Property 3— We can extend the second property to account for instances when we
multiply delta (x) with a function, f(x)--
ALGORITHM–
lis=np.linspace(0.1,0,100)
res=[]
for s in lis:
a=1.9
b=2.1
result, error=spi.quad(integrand,a,b)
res.append(result)
plt.plot(lis, res)
plt.show()
OUTPUT CODE–