problemSetSemI 1
problemSetSemI 1
1. Write a Python function for factorial, where an interger n is the argument. Use this function to calculate
factorial for a user given integer (maximum value 10). Print the value of the factorial.
2. User has given a DNA sequence: a string consisting of characters A, T, G and C. Your task is to print
the character with longest repetition in the sequence. Also print the number of repetation.
Example
User given sequence → ATGGGTCCG. Answer → G
3. Take an integer n as user input. If n is even, then n will be devided by 2. If n is odd, then n will
be multiplied by 3 and adds 1. This process will continue till n becomes 1. Print all the intermediate
numbers. Take input numbers as n = 201, 537.
Example
3 → 10 → 5 → 16 → 8 → 4 → 2 → 1, 12 → 6 → 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1
4. Get all the intermediate numbers obtained from above program as an list. Print the list.
5. Obtain a string with at least 9 characters as user input. Print the string in reverse order.
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1
7. Construct the number in reverse order of a user given number. Example : If the given number is 98452
then you have to construct the number 25489.
8. Write a Pyhton function for calculating the factorial of a user given integer. Test your function writing
a main program for calculating the factorial of 7, 9, 12.
f (x) = a + bx2
Write a three column data file separated by commas in the following manner. In first column the x-values
to be written for −2 ≤ x ≤ 2 with increment 0.1. In 2nd column write the y-values corresponding to
each of the x-values for a = 0.2, b = −0.5. In 3rd column write the y-values for a = −2.2, b = −0.5.
10. Read the data file created in the above problem and store the three columns in three different lists. Let
us say the 1st column x-values list is x, 2nd column y-values list is y1 and 3rd column y-values list is y2 .
Now, write another data file with the columns x and y1 + y2 .
1
(b) range function.
(c) for loop starting from a blank list.
12. Create a list with elements ’p’, ’r’, ’o’, ’g’, ’r’, ’a’, ’m’, ’i’, ’z’.
13. Create two lists with elements 1, 3, 5, 7, 9 and 2, 4, 6, 8, 10. Print the list created with the concatenation
of these two lists
15. Print the list [[−1, 2, 5], [2, −6, 9], [12, −11, −25]] in the following way
−1 2 5
2 −6 9
12 −11 −25
16. Write a Python program to sort a given set of numbers. Check your program for the following set of
numbers, 2.07, −3.29, 5.83, 7.29, −2.63, −8.28, 4.61, 0.94, −8.29, −1.38, −4.69.
17. For a given matrices Am×n write a Python function to obtain kAm×n . Where k is a scalar. Check your
function with the following values.
−12.4 3.37 −22.83
A= and k = 2.95
14.94 −26.28 −41.28
18. For two given matrices Am×n and Bm×n write a Python function for matrix addition and substraction.
Check your programs for
−2.4 3.27 −2.86 4.72 2.39 −0.89
A= and B =
1.64 −6.28 −4.28 −7.27 −1.67 7.92
19. For two given matrices Am×p and Bp×n write a Python function for matrix multiplication. Check your
function for these matrices
4.72 2.39
−2.4 3.27 −2.86
A= and B = −7.27 −1.67
1.64 −6.28 −4.28
−2.81 1.56
2
21. Use the Python functions 17, 18, 19 to calculate the following series
A2 A3 A4 A5
I +A+ + + +
2! 3! 4! 5!
for
−2.4 3.27
A=
1.64 −6.28
for
−2.4 3.27 −0.4 7.27
A= and B =
1.64 −6.28 5.64 −3.48
23. Write two Python functions to calculate permutation and combination defined as
n n!
Pk =
(n − k)!
n n!
Ck =
k!(n − k)!
Here, take n and k are the arguments of those Python functions. Check your functions by calculating
5
P 3 and 5C 3 .
(a)
n
X n(n + 1)
1 + 2 + 3 + 4 + ... = k=
2
k=1
(b)
n
2 2 2
X n(n + 1)(2n + 1)
1 + 2 + 3 + 4 + ... = k2 =
6
k=1
(c)
n 2
X n(n + 1)
1 + 23 + 33 + 43 + ... = k3 =
2
k=1
25. Get the sum of the following series correct upto 5 decimal places.
(a)
∞
1 1 1 1 X 1
1+ + + + + ... =
2 4 8 16 2n
n=0
3
(d)
∞
1 1 1 1 X (−1)n
− + − + ... =
1.2 2.3 3.4 4.5 (n + 1)(n + 2)
n=0
Comapare your result with the exact value 2 ln 2 − 1.
(e)
∞
1 1 1 1 X 1
+ 2
+ 3
+ 4
+ ... =
1.2 2.2 3.2 4.2 n2n
n=1
Comapare your result with the exact value ln 2.
(f)
X (−1)n ∞
1 1 1 1
1− + − + + ... =
1! 2! 3! 4! n!
n=0
26. Determine the sum of the following series correct upto 5 decimal places. From that value determine the
value of π correct upto 5 decimal palces.
(a)
X (−1)n ∞
1 1 1 1 π
−1 + − + − + ... = =−
3 5 7 9 2n − 1 4
n=1
(b)
∞
1 1 1 1 X 1 π2
1 + 2 + 2 + 2 + 2 + ... = =
2 3 4 5 n2 6
n=1
(c)
∞
1 1 1 1 X 1 π4
1 + 4 + 4 + 4 + 4 + ... = =
2 3 4 5 n4 90
n=1
(d)
∞
1 1 1 1 X 1 π6
1 + 6 + 6 + 6 + 6 + ... = =
2 3 4 5 n6 945
n=1
27. Write Python functions to calculate the sum of the following series correct upto 5 decimal places. Verify
your result for x = 0.75 with value calculated from the appropriate mathematical function from the math
module.
(a)
∞
X xk
ex =
k!
k=0
(b)
∞
X xk
xex = k
k!
k=0
(c)
∞
X (−1)k x2k+1
sin x =
(2k + 1)!
k=0
(d)
∞
X x2k+1
sinh x =
(2k + 1)!
k=0
4
(e)
∞
X (−1)k x2k
cos x =
(2k)!
k=0
(f)
∞
X x2k
cosh x =
(2k)!
k=0
(g)
∞
X (−1)k+1 xk
ln(1 + x) =
k
k=1
(h)
∞
X (2k)!x2k+1
sin−1 x =
22k (k!)2 (2k + 1)
k=0
(i)
∞
−1
X (−1)k x2k+1
tan x=
2k + 1
k=0
29. Create a data file to store the data given in two rows (upper row is x-values and lower row is y-values) of
each of the following tables. After that, read each data file to obtain two lists for x-values and y-values
respectively. Fit x-vlaues and y-values according to the curve y(x) = f (x; a, b) given. Print the values of
(a, b) and compare with the expected values.
1.00 1.10 1.20 1.30 1.40 1.50 1.60 1.70 1.80 1.90
(a)
0.718 0.525 0.406 0.205 0.038 -0.147 -0.300 -0.471 -0.645 -0.887
y = a + bx with expected a = 2.5, b = −1.76
3.00 3.50 4.00 4.50 5.00 5.50 6.00 6.50 7.00 7.50
(b)
-5.156 -6.869 -8.037 -10.337 -12.435 -13.699 -17.196 -18.771 -21.267 -24.301
y = a + bx2 with expected a = −1.5, b = −0.42
0.50 1.00 1.50 2.00 2.50 3.00 3.50 4.00 4.50 5.00
(c)
0.087 0.046 0.019 0.009 0.005 0.002 0.001 0.000 0.000 0.000
y = aebx ⇒ ln y = ln a + bx = A + bx (a = eA ) with expected a = 0.2, b = −1.53
0.50 1.00 1.50 2.00 2.50 3.00 3.50 4.00 4.50 5.00
(d)
0.522 5.473 18.110 47.656 96.828 193.936 310.719 438.571 644.893 1003.880
y = axb ⇒ ln y = ln a + b ln x = A + b ln x (a = eA ) with expected a = 5.25, b = 3.23
0.50 0.70 0.90 1.10 1.30 1.50 1.70 1.90 2.10 2.30
(e)
2.946 4.033 4.810 5.650 5.938 6.650 7.158 7.526 7.786 7.733
y = a + b ln x with expected a = 5.25, b = 3.23
5
-2.29 -1.78 -1.27 -0.76 -0.25 0.25 0.76 1.27 1.78 2.29
(f)
0.000 1.115 1.533 1.742 1.805 1.737 1.714 1.534 1.161 0.000
Fit according to
x2 y 2
+ 2 =1
a2 b
b2
⇒ y 2 = b2 − 2 x2
a !
√
r
A
⇒ = A + Bx2 b= A, a = −
B
-1.50 -1.17 -0.83 -0.50 -0.17 0.17 0.50 0.83 1.17 1.50
(g)
0.003 0.034 0.217 0.692 1.339 1.272 0.724 0.206 0.033 0.003
2
y = ae−bx ⇒ ln y = ln a − bx2 = A + Bx2 (a = eA , b = −B) with expected a = 1.41, b = 2.73
-1.70 -1.32 -0.94 -0.57 -0.19 0.19 0.57 0.94 1.32 1.70
(h)
-5.760 -4.272 -2.141 0.246 2.577 4.750 6.221 6.464 6.293 4.947
y = a cos x + b sin x ⇒ cosy x = a + b tan x with expected a = 5.41, b = 3.73
-2.50 -2.14 -1.79 -1.43 -1.08 -0.72 -0.37 -0.01 0.34 0.70
(i)
4.720 6.528 8.079 7.961 6.850 5.367 3.428 2.157 1.418 0.906
y = ae−b sin x ⇒ ln y = ln a − b sin x = A + B sin x (a = eA , b = −B) with expected a = 2.18, b = 1.32
-0.50 -0.33 -0.17 0.00 0.17 0.33 0.50 0.67 0.83 1.00
(j)
0.967 1.006 1.054 1.052 1.097 1.094 1.176 1.353 1.569 1.858
y 2 = a + bx3 with expected a = 1.15, b = 2.13
-0.60 -0.42 -0.24 -0.07 0.11 0.29 0.47 0.64 0.82 1.00
(k)
-2.318 -3.951 -7.222 -31.960 19.770 8.259 5.876 4.593 3.695 3.241
y = xa + b ⇒ xy = a + bx with expected a = 2.13, b = 1.15
-0.70 -0.51 -0.32 -0.13 0.06 0.24 0.43 0.62 0.81 1.00
(l)
2.083 2.447 3.557 8.847 20.933 4.669 2.796 2.113 1.809 1.584
y 2 = xa2 + b ⇒ x2 y 2 = a + bx2 with expected a = 1.25, b = 1.53
0.00 0.13 0.27 0.40 0.53 0.67 0.80 0.93 1.07 1.20
(m)
0.000 0.022 0.083 0.162 0.235 0.289 0.296 0.281 0.242 0.201
2
y = ae−bx x2 ⇒ ln xy2 = ln a − bx2 = A + Bx2 (a = eA , b = −B) with expected a = 1.25, b = 1.53
0.12 0.38 0.65 0.91 1.18 1.44 1.71 1.97 2.24 2.50
(n)
0.408 0.272 0.172 0.102 0.059 0.033 0.018 0.010 0.006 0.003
1
y= aebx +1
⇒ ln y1 − 1 = ln a + bx = A + bx (a = eA ) with expected a = 1.16, b = 2.25
f (x) = 0
One root within the bound [a, b] of this equation could be obtained with a tolerance by bisection
method. Write a Python function to solve a algebraic equation by bisection method taking f, a, b, as
6
arguments. Verify your function by determining roots with tolerance 10−5 of the following equations
within the ranges given.
(a)
f (x) = x2 − 1 = 0 within the ranges [−5, 0] and [0, 5]
(b)
f (x) = x3 − 3x2 − 6x + 8 = 0 within the ranges [−5, 0], [0, 3] and [3, 5]
(c) π
sin(2x) − 2 sin x − −1=0 within the ranges [−2π, 0] and [0, 2π]
4
(d)
tan2 x − 3 = 0 within the ranges [−5, 0] and [0, 5]
(e)
tan x − x = 0 ⇒ sin x − x cos x = 0 within the ranges [−π, π], [π, 2π] and [2π, 3π]
(a)
x2 − 9x + 8 = 0 for x0 = 0.0 and 10.0
(b)
sin 2x = 0 for x0 = − 1.82, 0.45 and 1.2
(c)
sin2 x − sin x − 2 = 0 for x0 = π and 3π
(d)
3π 11π
tan 2x − 1 = 0 for x0 = and
16 16
32. The first order initial value problem is defined as
dy
= f (x, y) with y(x0 ) = y0 .
dx
Numerically the solution could be obtained by Euler method. Numerical solution is given by discrete
y-values y0 , y1 , ..., yn for dicrete x-values x0 , x1 , ....xn . Write a Python function for solving the above
differential equation by Euler method. In this Python function put f (x, y), x0 , y0 and x0 , x1 , ....xn as
arguments. Obviously, the return is the array y0 , y1 , ..., yn . Use the above Python function to obtain the
solution of the following initial value problems. Write a data-file containing the x-values and y-values in
two columns.
(a)
dy
= x2 + 1 with y(1) = 4
dx
(b)
dy x
= 2 with y(0) = 0
dx y
(c)
dy x2 + 2
= with y(0) = 0
dx y
7
(d)
dy
= 2x(y 2 + 9) with y(0) = 0
dx
(e)
dy
= y 2 − 2x + 2 with y(0) = 0
dx
(f)
dy ex
= with y(0) = 1
dx y
(g)
dy x cos x
=− with y(π) = 0
dx 1 − 6y 5
Gnuplot
1. Write gnuplot script to plot the following functions in same x-y axes with corresponding decorations.
Also save the plot in eps format.
2
(a) f (x) = x2 e−2x
(b) f (x) = x2 sin(2x)
0.00 0.13 0.27 0.40 0.53 0.67 0.80 0.93 1.07 1.20
(a)
0.000 0.022 0.083 0.162 0.235 0.289 0.296 0.281 0.242 0.201
-2.29 -1.78 -1.27 -0.76 -0.25 0.25 0.76 1.27 1.78 2.29
(b)
0.000 1.115 1.533 1.742 1.805 1.737 1.714 1.534 1.161 0.000
Now write gnuplot script to plot the above two data files with points having same x-y axes with following
decorations. Save the plot in jpeg format.
(a) First one with point type l, point color ’red’, point size ’1.2’.
(b) Second one with point type t, point color ’blue’, point size ’1.3’.
3. Write gnuplot script to plot the following pair of polar functions in same r − θ axes with default setting.
Save the plots in png format. (Total 3 graphs each of which contains 2 plots)
4. Create data file according to following table. Write a gnuplot script to plot the data file with point s,
1
color ’brown’ and f (x) = 1.16 e2.25 x +1 with line ’....’, color ’black’ in same x-y axes . Save the plot in eps
format.
0.12 0.38 0.65 0.91 1.18 1.44 1.71 1.97 2.24 2.50
0.408 0.272 0.172 0.102 0.059 0.033 0.018 0.010 0.006 0.003
8
(b) Create a data file corresponding to the following table.
-2.00 -1.64 -1.27 -0.91 -0.55 -0.18 0.18 0.55 0.91 1.27 1.64 2.00
0.50 0.68 0.86 1.03 1.17 1.24 1.24 1.17 1.03 0.86 0.68 0.50
2
Write a gnuplot script to fit f (x) = aebx with the above data file where a, b are the ajustable
parameters. Plot the data file along with fitted curve in the same x-y axes with default setting.
Save the plot in eps format.
(c) Create a data file corresponding to the following table.
-2.00 -1.64 -1.27 -0.91 -0.55 -0.18 0.18 0.55 0.91 1.27 1.64 2.00
0.40 0.64 0.22 -0.68 -1.16 -0.58 0.58 1.16 0.68 -0.22 -0.64 -0.40
2
Write a gnuplot script to fit f (x) = a sin(bx)e−cx with the above data file where a, b, c are the
ajustable parameters. Plot the data file along with fitted curve in the same x-y axes with default
setting. Save the plot in jpeg format.
2. Write a python program to solve the following differential equations with Euler method for the given
x-range and write the x-values and y-values in a data file. The exact solutions are also provided.
Now write gnuplot script to plot the data file and the exact solution in the same x-y axes with default
settings.