GOVERNMENT OF KERALA
DEPARTMENT OF TECHNICAL EDUCATION
Rajiv Gandhi Institute of Technology
([Link] COLLEGE)
KOTTAYAM-686501 Tel: 0481-2507763, 2506153
SCIENTIFIC COMPUTING LAB
Name: ………………………………………………………………...
Branch: ELECTRONICS & COMM. ENGG…………….
Semester: 3 Roll No:
Certified bonafide record work done by
[Link]: …………..
Faculty in Charge Internal Examiner External Examiner
INDEX
Sl Name of Experiment Date Page Invigilat
No: No or sign
1. FAMILIARISATION OF THE
COMPUTING TOOL
2. FAMILIARISATION OF THE
SCIENTIFIC COMPUTING
3. REALIZATION OF ARRAYS &
MATRICES
4. NUMERICAL
DIFFERENTIATION &
INTEGRATION
5. SOLUTION OF ORDINARY
DIFFERENTIAL EQUATION
6. SIMPLE DATA VISUALIZATION
7. CONVERGENCE OF FOURIER
SERIES
8. COIN TOSS AND LEVEL
CROSSING
1
EXPERIMENT: 1
FAMILIARISATION OF THE COMPUTING TOOL
Needs and requirements in scientific computing:
Need: Problem solving Apply “numerical” methods
to given problem
Requirement: implement the method. (Code) Input
gives required result. (According to the method)
Understanding the method is important to
implement It
Different tools that can be used:
o Python
o R
o Programming MATLAB
o SciLab
PYTHON:
o Developed is 1980’s as a scripting language.
o Object oriented language.
o Uses interpreter.
o Faster, but applications are not stand alone. i.e.
Needs an interpreter
Variables:
Variables are assigned types dynamically.
eg:
Strings
Sequence of characters enclosed in single or double
quotes Strings are immutable. Thus individual
2
characters can’t be modified with an assignment
statement.
Concatenation using ’+ ’
Tuple:
It’s a sequence of arbitrary objects separated by
commas and enclosed in parentheses. Tuple with a
single object requires a final comma. e.g. x = (2,)
Supports same operations as strings. Immutable
3
List:
Similar to tuple, but mutable. Identified by enclosing
in brackets
Matrices can be represented as nested lists, with
each row being an element of the list.
4
Arithmetic Operators:
o + : Addition
o - : Subtraction
o * : Multiplication
o / : Division
o ** : Exponentiation
o % : Modular division
Conditionals:
if (condition):
block
elif (condition):
block
else:
block
Loops:
while (condition):
block
for target in sequence:
5
block
Loop control statements:
o break: Terminates any loop.
o continue: Skips a portion of the loop.
6
EXPERIMENT 2
FAMILIARISATION OF THE SCIENTIFIC COMPUTING
AIM: Familiarization of the scientific computing
Type Conversion:
If an arithmetic operation involves numbers of mixed
types, the numbers are automatically converted to a
common type before the operation is carried out.
int(a) #converts a to integer
float(a) #converts a to floating
point
complex(a) #converts a to complex a
+ 0j
complex(a,b) #converts t complex a + bj
7
Functions:
Functions are the primary method of code organization
and reuse. RESULT of a function is communicated by the
return statement. If the return statement or return values
are omitted, the function returns the null object. A
parameter can be any Python object, including functions.
Parameters can be given default values, in which case the
parameter in the function call is optional.
e.g.
Following function computes the first two derivatives of f
(x) by finite differences:
8
If a mutable object, such as a list, is passed to a function
where it is modified, the changes will also appear in the
calling program. An example follows:
Lambda Function:
If the function has the form of an expression, it can be
defined with the lambda statement. e.g.
funct_name = lambda parameter1, parameter2: expression
Multiple statements are not allowed.
Modules:
A module is a simple file where the functions reside; the
name of the module is the name of the file. A module can
be loaded into a program by the statement: from
module_name import * Python comes with a large number
of modules containing functions and methods for various
tasks.
Mathematics Module: math
9
The cmath module provides many of the functions found
in the math module, but these functions accept complex
numbers.
Numpy module:
The numpy module is not a part of the standard Python
release. - 6 - | P a g e This module introduces array
objects that are similar to lists, but can be manipulated by
numerous functions contained in the module. The size of
10
an array is immutable, and no empty elements are
allowed. Most commonly used numpy functions are:
complex, float, append, arcos cos, cosh, diag, diagonal,
dot, exp log, pi, max, min
Arithmetic operators work differently on arrays than they
do on tuples and lists; the operation is broadcast to all the
elements of the array; i.e. the operation is applied to each
element in the array.
11
Functions imported from the math module will work on
the individual elements, but not on the array as a whole.
Vectoring algorithm
Sometime the broadcasting properties of the
mathematical functions in the numpy module can be
used to replace loops in the code. This procedure is
known as vectorisation.
Eg:
√
100
iπ iπ
s=∑ sin
i=0 100 100
The 1st algorithm uses the scalar version of sqrt and
sin functions in the math module, whereas the 2nd
algorithm imports these functions from numpy. The
12
vectorized algorithm executes much faster, but uses
more memory.
EXPERIMENT: 3
REALIZATION OF ARRAYS & MATRICES
EXPERIMENT 3.1:
AIM: Realize one dimensional array of real and complex
numbers
ALGORITHM:
1. Start
2. Make an array of real numbers
3. Make an array of complex numbers
4. Print the real and complex number array
5. For verification print it’s type too using ‘type’
6. Stop
PROGRAM:
13
RESULT:
EXPERIMENT 3.2:
AIM: Stem and continuous plots of real arrays using matplotlib
ALGORITHM:
1. Start
2. Create 20 evenly spaced numbers b/w 0 & 2π(x-axis
values)
3. Plot X vs Sine(continuous plot)
4. Plot X vs Cosine(stem plot)
5. Display plots
6. Stop
PROGRAM:
14
RESULT:
EXPERIMENT 3.3
AIM: Realization of two dimensional arrays and matrices and
their visualizations with matshow
ALGORITHM:
1. Start
2. Create a 2 –D array and convert it to matrix
3. Plot the matrix using matshow
4. Display the matrix
5. Stop
PROGRAM:
15
RESULT:
EXPERIMENT 3.4
AIM: Inverse of square matrix and the solution of the matrix
equation |A||X|=|b| where A is an N x N matrix and X and b are
N x 1 vectors.
ALGORITHM:
1. Start
2. Create a 2-D array(A) with co-efficient of the equations
3. Create array with the result(b)
4. Inverse of [A] (A X = B So, X=inv(A).B)
16
5. Matrix multiplication of inverse[A]& b
6. Display the product matrix
7. Stop
PROGRAM:
RESULT:
EXPERIMENT NO: 3.5
AIM: Computation of rank () and eigen values (1) of A
ALGORITHM:
1. Start
2. Create a 2-D array(A) with desired values
3. Calculate eigen values and eigen vectors
4. Print eigen values and vectors
17
5. Calculate rank of the matrix using single value
decomposition function. By taking the no of non-zero
values in singular matrix obtained by svd
6. Print s (for verification)and the rank
7. Stop
PROGRAM:
RESULT:
EXPERIMENT NO: 3.6
AIM: Approximate A for N = 1000 with the help of singular
value decomposition of A as
r
T
A=∑ ❑i U i Vi
i=0
18
where Ui and Vi are the singular vectors and are the eigen
values with ❑i< ❑ j and i > j. One may use built-in functions for
singular value decomposition.
ALGORITHM:
1. Start
2. Create a 2D array with size 1000 x1000
3. Do the svd of matrix A and save it to U,S,V
4. Print the rank of A using svd(just for verification)
5. Calculate the approximation of using svd method and
save it to approx_abhijith_array
6. Print approx_abhijith_array
7. Stop
PROGRAM:
RESULT:
19
EXPERIMENT NO: 3.7
AIM:
Plot the absolute error between A and A as =∑ ∑ aij − aij ❑
N N
2
against r for r = 10, 50, 75, 100, 250, 500, 750 and appreciate
i=1 j=1
the plot.
ALGORITHM:
1. Start
2. Create a random 1000x1000 2-D array (arr_abhi) using
random function (arr_abhi=> A)
3. Use single value decomposition to find Ui, Vi and λi
4. Add elements of s diagonally to S(matrix)
5. Create array(rank) with the given values of ranks
6. Use a for LOOP
7. Make an identity matrix
8. Make an zero matrix of size ((1000 - rank[i]) x 1000)
9. Create array that is formed by stacking up identity & zeros
matrix to make a array
10. Create array say z that stores the product of diagonal array
& vertical array
11. Create matrix arr_abhi_2 = >A; matrix product of U, z & V
20
12. Create an array say abhi_arr_sum to store the square of
absolute value of arr_abhi - arr_abhi_2
13. Calculate the sum of all elements in abhi_arr_sum & store
it as a abhi_err_list
14. Create a stem plot of errors v/s Rank(rank) and label the
plot
15. END
PROGRAM:
21
RESULT:
22
EXPERIMENT: 4
NUMERICAL DIFFERENTIATION & INTEGRATION
EXPERIMENT 4.1:
AIM: Realize the function sin t, cos(t), sinh(t) and cosh(t) for
the vector t=[0, 10] with increment 0.01
ALGORITHM:
1. Start
2. Create an array with range 0 to 10 and an increment of
0.01 for time t
3. Assign sin, cos, sinh & cosh functions to variables
4. Create a 2x2 subplot
5. Define a function for plotting and labeling the graphs and
plpot the graphs t vs functions
6. Display the plots
7. Stop
PROGRAM:
23
RESULT:
24
EXPERIMENT 4.2:
AIM:
Compute the first and second derivatives of these functions
using built in tools such as grad.
ALGORITHM:
1. Start
2. Create an array with range 0 to 10 and an increment of
0.01 for time t
3. Assign sin, cos, sinh & cosh functions to variables
4. Define a function(just for simplicity) and calculate 1st &
2nd derivatives using gradient function and return both
5. Call the function and pass the function and spacing into
the function
6. If you want use it for plotting the function or print the
values
7. Stop
PROGRAM:
25
RESULT:
THE RESULT THE ABOVE EXPERIMENT IS PLOTTED IN EXP
4.3
EXPERIMENT 4.3:
AIM: Plot the derivatives over the respective functions and
appreciate.
ALGORITHM:
1. Start
2. Create an array with range 0 to 10 and an increment of
0.01 for time t
3. Assign sin, cos, sinh & cosh functions to variables
4. Define a function(just for simplicity) and calculate 1st &
2nd derivatives using gradient function and return both
5. Call the function and pass the function and spacing into
the function
6. Create a 2x2 subplot
26
7. Define a function and call it and plot the function and and
its derivatives on the same graph
8. Display the plots
9. Stop
PROGRAM:
RESULT:
27
EXPERIMENT 4.4:
AIM: Familiarize the numerical integration tools in the
language you see.
ALGORITHM:
1. Start
2. Define the function to be integrated
3. Integrate the function using the quad function
4. Print the result , error
5. Stop
PROGRAM:
28
RESULT:
EXPERIMENT 4.5:
AIM: Realize the function
2
f ( t )=4 t +3
and plot it for the vector t = [−5, 5] with increment 0.01
ALGORITHM:
1. Start
2. Define the function to be realized
3. Make an array from -5 to 5 with increment 0.01(vector t)
4. Plot the function
5. Display the plot
6. Stop
PROGRAM:
29
RESULT:
EXPERIMENT 4.6:
AIM: Use general integration tool to compute
2
∫ f (t ) dt
ALGORITHM:
−2
1. Start
2. Define the function to be integrated
30
3. Integrate the function using the quad function pass the
function ,lower limit ,upper limit
4. Print the result , error
5. Stop
PROGRAM:
RESULT:
EXPERIMENT 4.7:
AIM: Repeat the above steps with trapezoidal and Simpson
method and compare the results
ALGORITHM:
1. Start
2. Define the function to be integrated
3. Make an array from -5 to 5 with increment 0.01(vector t)
4. Integrate the function using the quad function
5. Integrate using the trapz and simps functions
6. Print the result and compare the variations
31
7. Stop
PROGRAM:
RESULT:
EXPERIMENT 4.8:
AIM: Compute using the above three methods
∞
1
∫
2
−x /2
e dx
√2 π 0
ALGORITHM:
1. Start
2. Define the function to be integrated
3. Make an array from 0 to 40000 with increment
0.01(vector t)
4. Integrate the function using the quad function
5. Integrate using the trapz and simps functions
32
6. Print the result and compare the variations
7. Stop
PROGRAM:
RESULT:
33
EXPERIMENT: 5
SOLUTION OF ORDINARY DIFFERENTIAL EQUATION
EXPERIMENT NO: 5.1
AIM: Solve the 1st order differential equation
dx
+2 x=0
dy
with initial condition x(0) = 1
ALGORITHM:
1. Start
2. Define the function to be solved
3. Range from 0-5 with increment of 0.25(time t)
4. Solve the 1st order ODE
5. Plot a graph t vs differential and label the axes
6. Display the plot
7. Stop
PROGRAM:
34
RESULT:
EXPERIMENT NO: 5.2(A)
35
AIM: Solve for the current transient through an RC network
(RC =3) that is driven by
A 5 V DC
B The signal and plot the solutions
ALGORITHM:
1. Start
2. Define the function to be solved
3. Range from 0-5 with increment of 0.25(time t)
4. Solve the 1st order ODE
5. Plot a graph t vs differential and label the axes
6. Display the plot
7. Stop
ANALYTICAL SOLUTION:
PROGRAM:
36
RESULT:
EXPERIMENT NO: 5.2(B)
37
ANALYTICAL SOLUTION:
PROGRAM:
38
RESULT:
EXPERIMENT NO: 5.3
39
AIM: Solve the 2nd order differential equation
ALGORITHM:
1. Start
1. Define the function that returns 1st and 2nd derivatives
2.
3. Set the initial condition U=0,time points 0 to 10
4. Solve the ODE
5. Plot a graph for the solution
6. Display the plot
7. Stop
PROGRAM:
40
RESULT:
EXPERIMENT NO: 5.4 (A)
AIM:
Solve the current transient through a series RLC circuit with R
= 1Ω, L = 1 mH and C = 1 μF
5V DC
The signal
ALGORITHM:
2. Start
3. Define the function that returns 1st and 2nd derivatives
4. Set the initial condition U=[0,5000],time points (t)
5. Solve the ODE
6. Plot a graph for the solution
7. Display the plot
8. Stop
41
PROGRAM:
Vs=5v
RESULT:
42
43
EXPERIMENT NO: 5.4(B)
Vs =5e^(-t)
PROGRAM:
RESULT:
44
45
46
EXPERIMENT: 6
SIMPLE DATA VISUALIZATION
EXPERIMENT NO: 6.1
AIM: Draw stem plots, line plots, box plots, bar plots and
scatter plots with random data.
ALGORITHM:
1. Start
2. Make random data and assign it to a variable
3. Create 2x3 subplots
4. Plot the random data using different plots (stem plots,
line plots, box plots, bar plots and scatter plots)
5. Define a function for labeling and adding title. Call it
label the axis
6. Display the plots
7. Stop
47
PROGRAM:
48
RESULT:
49
EXPERIMENT NO:6.2
AIM:
Plot the histogram of a random data.
ALGORITHM:
1. Start
2. Make random data and assign it to a variable
3. Create subplots for showing 2 graphs
4. Create random samples from Gaussian distribution
5. Plot the histogram using the random data
6. Label the graph
7. Display the plots
8. Stop
50
PROGRAM:
RESULT:
51
EXPERIMENT NO: 6.3
AIM: Create legends in plots.
ALGORITHM:
1. Start
2. Make an array ranging from 0 to 4 with an increment of
0.01(x)
3. Define a few function(desired)
4. Plot the function vs range(x)
5. Label the axis
6. Display the legends for all functions
7. Display the plot
8. Stop
PROGRAM:
52
RESULT:
EXPERIMENT NO:6.4
AIM:
Realize a vector t = [−10, 10] with increment 0.01 as an array
ALGORITHM:
1. Start
2. Create a list with range -10 to 10 with increment of 0.01
3. Print the list as an array
4. Stop
PROGRAM:
RESULT:
53
EXPERIMENT NO: 6.5
AIM: Implement and plot the functions
• f(t) = cost • f(t) = cost . cos 5t + cos 5t
ALGORITHM:
1. Start
2. Create a subplot to display both the functions side by side
3. Define the functions
4. Range from -10 to 10 with an increment of 0.01
5. Plot the graphs for functions vs range
6. Label the graphs
7. Display the plots
8. Stop
PROGRAM:
54
RESULT:
55
EXPERIMENT NO: 7
CONVERGENCE OF FOURIER SERIES
EXPERIMENT NO: 7.1&7.2
AIM: 7.1. The experiment aims to understand the lack of
convergence of Fourier series
7.2. Realize the Fourier series
[ 1
f ( t )=4 π 1− cos
2π 3t 1
+ cos
2π 5t 1
− cos
2 π7t
+ ·· ·
]
ALGORITHM:
3 T 5 T 7 T
1. Start
2. Define a function for the general term in Fourier series
3. Give the value for time period as 20
4. Create an array for t (vector) from 0 to 100 with
increment as say 0.01
5. Use a for loop to loop 4 times through the term and sum
up the terms
6. Plot the function v/s t
7. Display the plot
8. Stop
PROGRAM:
56
RESULT:
EXPERIMENT NO: 7.3
AIM:
Realize the vector t = [0, 100] with an increment of 0.01 and
keep T = 20.
57
ALGORITHM:
1. Start
2. Define a function for the general term in Fourier series
3. Give the value for time period as 20
4. Create an array for t(vector ) from 0 to 100 with
increment 0.01
5. Use a for loop to loop (desired number of times) through
the term and sum up the terms
6. Plot the function vs t
7. Display the plot
8. Stop
PROGRAM:
58
RESULT:
EXPERIMENT NO: 7.4
59
AIM: Plot the first 3 or 4 terms on the same graphic window
and understand how the smooth sinusoids add up to a
discontinuous square function.
ALGORITHM:
1. Start
2. Define a function for the general term in Fourier series
3. Give the value for time period as 20
4. Create an array for t (vector) from 0 to 100 with
increment as say 0.01
5. Use a for loop to loop 3 times through the term and sum
up the terms .Plot the function vs t each time
6. Display the plot
7. Stop
PROGRAM:
RESULT:
60
EXPERIMENT NO: 7.5
AIM: Compute and plot the series for the first 10, 20, 50 and
100 terms and understand the lack of convergence at the
points of discontinuity.
ALGORITHM:
1. Start
2. Define a function for the general term in Fourier
series
3. Give the value for time period as 20
4. Create an array for t (vector) from 0 to 100 with
increment as0.01
5. Create an array for n terms [10, 20, 50,100]
6. Create subplots for 4 plots
7. Create an array with axis
8. Define a function that plots different subplots
9. Use a for loop to loop 4 times inside that Use a for
loop to loop (access n from n terms) n no of times
through the term and sum up the terms, Plot the
function v/s t for each n
10. Display the plot
11. Stop
61
PROGRAM:
RESULT:
62
EXPERIMENT NO: 7.6
AIM: With t made a zero vector, f (0) = 1, resulting in the
Madhava series for π as π=4 1 – 3 + 5 – 7 + ·· ·
[ ]
1 1 1
ALGORITHM:
1. Start
2. Define the function for MADHAVA SERIES to obtain
pi
3. In the function loop for the (desired) n no of times
and calculate each term and sum up the terms and
return the value of the series
4. Call the function pass the value of n and print the
value
5. Stop
PROGRAM:
RESULT:
63
EXPERIMENT NO: 7.7
AIM: Use this to compute π for the first 10, 20, 50 and 100
terms.
ALGORITHM:
1. Start
2. Define the function for MADHAVA SERIES to obtain
pi
3. In the function loop for the n no of times and
calculate each term and sum up the terms and
return the value of the series
4. Call the function pass the value of n and print the
value (value of n =10, 20, 50,100)
5. Stop
PROGRAM:
RESULT:
64
EXPERIMENT NO: 8.
COIN TOSS AND LEVEL CROSSING
EXPERIMENT NO: 8.1
AIM: Simulate a coin toss that maps a head as 1 and tail as 0
ALGORITHM:
1. Start
2. Use random function to choose between 0 &1
3. Print the outcome as :
Head if 1
else Tail
4. Stop
PROGRAM:
RESULT:
65
EXPERIMENT NO: 8.2
AIM: Toss the coin N = 100, 500, 1000, 5000, 500000 times
and compute the probability of head in each case.
ALGORITHM:
1. Start
2. Use random function to choose between 0 &1
3. Create a function to increment Head by 1 each time a
head appears
4. Run the function for each N
5. Print the % of success results(total head appeared in the
toss/N)
6. Stop
PROGRAM:
66
RESULT:
EXPERIMENT NO: 8.3
AIM: Compute the absolute error |0.5 - p| in each case and plot
against N and understand the law of larger numbers
ALGORITHM:
1. Start
2. Use random function to choose between 0 &1
3. Create a function to increment Head by 1 each time a
head appears
4. Run the function for each N
5. Calculate the % of success results(total head appeared in
the toss/N)
6. Plot a scatter plot for absolute error of each N(0.5-H/N)
7. Display the plot
8. Stop
PROGRAM:
67
RESULT:
68
EXPERIMENT NO: 8.4
AIM: Create a uniform random vector with maximum
magnitude 10, plot and observe.
ALGORITHM:
1. Start
2. Use random function to choose 5 values from 0 to 10
3. Plot a stem graph for the random values
4. Display the graph
5. Stop
PROGRAM:
RESULT:
69
EXPERIMENT NO: 8.5
AIM: Set a threshold (VT = 2) and count how many times the
random function has crossed VT
ALGORITHM:
1. Start
2. Use random function to choose 5 values from 0 to 10
3. Set threshold value as 2
4. Use a loop to check whether the value has crossed VT
5. Plot a stem graph for the random values
6. Display the graph
7. Print number of times the graph has crossed VT
8. Stop
PROGRAM:
70
RESULT:
EXPERIMENT NO: 8.6
71
AIM:
Count how many times the function has gone above or below
the threshold.
ALGORITHM:
1. Start
2. Use random function to choose 5 values from 0 to 10
3. Set threshold value as 2
4. Use a loop to check whether the value has crossed
VT(above)
5. Plot a stem graph for the random values
6. Display the graph
7. Print number of times the graph has crossed VT(above
and below)
8. Stop
PROGRAM:
72
RESULT: