Experiment No 5 : Numerical Differentiation and Integration

 Objectives

  • Realize the functions sin t, cos t, sin ht and cos ht for the vector t = [0; 10] with increment 0:01

  • Compute the rst and second derivatives of these functions using built in tools such as grad.

  • Plot the derivatives over the respective functions and appreciate.

  • Familiarize the numerical integration tools in the language you use.

  • Realize the function

f(t) = 4t2 + 3

and plot it for the vector t = [-5; 5] with increment 0:01

  • Use general integration tool to compute


Theory

Trigonometric and angular functions are discussed here

To work the functions of trigonometry we have to import the numpy library and to plot the figures we have to import the matplotlib library

1. sin() :- This function returns the sine of value passed as argument. The value passed in this function should be in radians.

2. cos() :- This function returns the cosine of value passed as argument. The value passed in this function should be in radians.


Realizing the sin, cos,sinh,cosh functions

Program  for plotting sine t signal

# program for plotting the sine t signal

#importing libraries

from numpy import *

from matplotlib.pyplot import *

 

# Get x values of the sine wave

t = arange(0, 10, 0.01);

 

# calculating the amplitude

y = sin(t)

 

# Plot a sine wave using time and amplitude obtained for the sine wave

plot(t,y)

title('Sine wave') # Give a title for the sine wave plot

xlabel('Time') # Give x axis label for the sine wave plot

ylabel('Amplitude = sin(time)') # Give y axis label for the sine wave plot

grid(True, which='both') # enabling the grid

show() # showing the figure


Output 1

Program for plotting cos t wave

# program for plotting the Cose t signal

#importing libraries

from numpy import *

from matplotlib.pyplot import *

 

# Get x values of the wave

t = arange(0, 10, 0.01);

 

# calculating the amplitude

y = cos(t)

 

# Plot a wave using time and amplitude obtained for the sine wave

plot(t,y)

title('cose t wave') # Give a title for the  wave plot

xlabel('Time') # Give x axis label for the  wave plot

ylabel('Amplitude = cos(t)') # Give y axis label for the wave plot

grid(True, which='both') # enabling the grid

show() # showing the figure




Output 2



Program for plotting Sin ht signal

# program for plotting the Sine ht signal

#importing libraries

from numpy import *

from matplotlib.pyplot import *

 

# Get x values of the wave

t = arange(0, 10, 0.01);

 

# calculating the amplitude

y = sinh(t)

 

# Plot a wave using time and amplitude obtained for the sine wave

plot(t,y)

 

title('Sine ht wave') # Give a title for the  wave plot

xlabel('Time') # Give x axis label for the  wave plot

ylabel('Amplitude = Sinh(t)') # Give y axis label for the wave plot

grid(True, which='both') # enabling the grid

show() # showing the figure

 


Output 

Program for plotting Cos ht signal

# program for ploting the Cose ht signal

#importing libraries

from numpy import *

from matplotlib.pyplot import *

 

# Get x values of the wave

t = arange(0, 10, 0.01);

 

# calculating the amplitude

y = cosh(t)

 

# Plot a wave using time and amplitude obtained for the sine wave

plot(t,y)

 

title('Cos ht wave') # Give a title for the  wave plot

xlabel('Time') # Give x axis label for the  wave plot

ylabel('Amplitude = Cosh(t)') # Give y axis label for the wave plot

grid(True, which='both') # enabling the grid

show() # showing the figure

Output

Program

# program for plotting the Sineht & cosht signal

#importing libraries

from numpy import *

from matplotlib.pyplot import *

 

# Get x values of the wave

t = arange(-5, 5, 0.01);

 

# calculating the amplitude

y = sinh(t)

y1= cosh(t)

 

# Plot a wave using time and amplitude obtained for the sine wave

plot(t,y,label="sinht")

plot(t,y1,label="cosht")

 

title('Sineht & Cosht wave') # Give a title for the  wave plot

xlabel('Time') # Give x axis label for the  wave plot

ylabel('Amplitude') # Give y axis label for the wave plot

grid(True, which='both') # enabling the grid

legend()

show() # showing the figure

Output

Computing and plotting first and second derivative of sine and cos functions

Program 

# program for plotting the Sin t signal

#importing libraries

from numpy import *

from matplotlib.pyplot import *

rcParams["figure.figsize"] = (15,10) # changing the figure size

 

# Get x values of the wave

t = arange(0, 10, 0.01);

 

# calculating the amplitude

y1 = sin(t) # calculating sine wave

sfd1= gradient(y1) # calculating first derivative

sfd2= gradient(sfd1)  # calculating second derivative

 

# calculating the amplitude

y2 = cos(t) # calculating cos wave

cfd1= gradient(y2) # calculating first derivative

cfd2= gradient(cfd1)  # calculating second derivative

 

 

# plotting sine and cos signals in subplot

 

fig, axs = subplots(3,2

axs[0,0].plot(t,y1) # sin signal potting

axs[0,0].set_title("Sin wave")

axs[1,0].plot(t,sfd1)

axs[1,0].set_title("first sin derivative")

axs[2,0].plot(t,sfd2)

axs[2,0].set_title("second sin derivative")

 

axs[0,1].plot(t,y1)  # cose signal potting

axs[0,1].set_title("cose wave")

axs[1,1].plot(t,cfd1)

axs[1,1].set_title("first cos derivative")

axs[2,1].plot(t,cfd2)

axs[2,1].set_title("second cos derivative")

 

show() # showing the figure

 

 


Output



Program 2

# program for plotting the Sinh t signal

#importing libraries

from numpy import *

from matplotlib.pyplot import *

rcParams["figure.figsize"] = (15,10) # changing the figure size

 

# Get x values of the wave

t = arange(0, 10, 0.01);

 

# calculating the amplitude

y1 = sinh(t) # calculating sine wave

sfd1= gradient(y1) # calculating first derivative

sfd2= gradient(sfd1)  # calculating second derivative

 

# calculating the amplitude

y2 = cosh(t) # calculating cose wave

cfd1= gradient(y2) # calculating first derivative

cfd2= gradient(cfd1)  # calculating second derivative

 

 

# ploting sine and cose signals in subplot

 

fig, axs = subplots(3,2

axs[0,0].plot(t,y1) # sin signal potting

axs[0,0].set_title("Sinh wave")

axs[1,0].plot(t,sfd1)

axs[1,0].set_title("first sinh derivative")

axs[2,0].plot(t,sfd2)

axs[2,0].set_title("second sinh derivative")

 

axs[0,1].plot(t,y1)  # cose signal potting

axs[0,1].set_title("cosh wave")

axs[1,1].plot(t,cfd1)

axs[1,1].set_title("first cosh derivative")

axs[2,1].plot(t,cfd2)

axs[2,1].set_title("second cosh rivative")

 

show() # showing the figure

Output


Realize the function f(t)=4t2+3

Progame

#importing libraries

from numpy import *

from matplotlib.pyplot import *

 

# realizing the function

t = arange(-5, 5, 0.01); #assigning the vector t

ans = (4*t**2)+3

plot(t,ans)

title("function f(t)=4t^2 + 3")

xlabel("vector t")

ylabel("function f(t)")

show()


Output

Familiarization of Numerical integration 

SciPy has a number of routines for performing numerical integration. Most of them are found in the same scipy.integrate library. The scipy.integrate sub-package provides several integration techniques including an ordinary differential equation integrator. An overview of the module is provided by the help command: try the below command in colab

integrate?

Problem statement, find the integral of a function f(t) given below 

-22f(t) dt 

where f(t) = f(t)=4t2+3

Programe

from scipy.integrate import quad

def integrand(t):

    return 4*t**2+3

ans, err = quad(integrand, -2, 2)

print("value of integral is : ",ans)

print("estimate of the absolute error is : ",err)

 

Output

value of integral is :  33.333333333333336

estimate of the absolute error is :  3.7007434154171886e-13


Inference 

  • Realized the functions sin t, cos t, sin ht and cos ht for the vector t = [0; 10] with increment 0:01

  • Computed the second derivatives of these functions using built in tools such as grad.

  • Plotted the derivatives over the respective functions.

  • Familiarized the numerical integration tools in python.

No comments:

Post a Comment

Signals & System Lect 15 | Stable & Unstable Systems | Solved examples

  ECT 204 SIGNALS AND SYSTEMS Topics covered  00:00 - Introduction to Stable & unstable Systems 01:08 - BIBO criteria 03:38 Problem No 1...