Sunteți pe pagina 1din 6

NATIONAL INSTITUTE OF TECHNOLOGY ROURKELA

Department of Electrical Engineering


Soft Computing Techniques (EE6243/EE637)
Session: 2019 - 2020 (Autumn)
M.Tech. 1st Semester and Dual Degree 7th Semester

Assignment 1A: MATLAB Programming

1. Print all integers within a given range.


2. Write a program to get the sum of first 20 natural numbers.
3. Write a program for calculation of sum of square of all odd integers between 1 to 100.
4. Write a program which will take an integer from user and determine whether the number is even or
odd.
5. Write a program which will take three integers as inputs and find the largest among them. While entering
the numbers, take
(a) Two negative integers and one positive integers.
(b) One negative, one positive and 0.
Find the largest of the absolute value of the integers taken in (a).
6. Given an array [2, 8, 10, 683, 45, 1, 996, 139, 190, 58, 730, 9, 800], split it into three different arrays with different
ranges: [0, 10), [10, 100), [100, 1000].
7. Admission to a professional course is subject to the following conditions:
(a) Marks in mathematics ≥ 60.
(b) Marks in physics ≥ 50.
(c) Marks in chemistry ≥ 40.
(d) Total in all these subjects ≥ 120 or Total in mathematics and physics ≥ 150.
Given the marks in three subjects, write a program to process the applications to list the eligible candidates.
8. Print the numbers between 0 and 100 which are multiples of 7.
9. Write a program to check whether a number is divisible by 5 and 11.
10. Write a program which will check whether a year is leap year or not.
11. Given an array of real numbers x̄ ∈ Rn , find their mean, standard deviation and variance. (Do not use
inbuilt functions for the same).

Recall:
1∑
n
Mean(µ) = xi
n i=1
1 ∑
n
Variance = (xi − µ)2
n − 1 i=1

S.D. = Variance

12. Declare two arrays. First array will store weights of 10 boys (select random numbers between 10 to 60).
Second array will store heights of those 10 boys (select random numbers between 100 to 200). Write a
program which will count the numbers of boys whose weights is less than 40 kg and height is greater
than 170 cm.
EE6243/EE637 Assignment 1A Page 2 of 2

13. Write a program to count number of digits in a given integer.

14. Write a program to print the multiplication table of 23.


15. Write a program to display the following pattern:

1
2 2
3 3 3
4 4 4 4
..
.
10 10 10 10 ··· 10

16. Write a program to display the following pattern using while loop:

1
2 3
4 5 6
7 8 9 10

17. Given a date, write a program that computes the day number in a year.
18. Implement the factorial function (do not use inbulit factorial function).
19. Write a program that generates an n-dimensional array of random values between 30 to 65 and sorts in
(a) ascending (b) descending order (do not use inbulit function for sorting).

20. Write a program that tabulates the first 100 prime numbers.

Some of the useful MATLAB inbuilt functions:

abs : returns the absolute value. If complex input, then returns magnitude
rand : to generate random number between 0 to 1.0
randi : generates random integers within the range
rem : rem(X,Y) returns the remainder after division of X by Y
sqrt : for square-root
input : displays the text in prompt and waits for the user to input a value and to press the Return key
display : for displaying the value of a variable in the command window
fprintf : writes data to text file or command window
NATIONAL INSTITUTE OF TECHNOLOGY ROURKELA
Department of Electrical Engineering
Soft Computing Techniques (EE6243/EE637)
Session: 2019 - 2020 (Autumn)
M.Tech. 1st Semester and Dual Degree 7th Semester

Assignment 1B: MATLAB Programming (contd.)

1. Generate a random matrix A(∈ R6×5 ) where all element ∈ [−1, 1]. Count the number of sign change in
A. Move row-wise.
2. Write a function that takes x and y as inputs from the main file and returns z, where,

z = 3x2 + (x2 + y 2 ) + eln(x)

3. Write a function that prints the binary equivalent of a positive integer number.
4. Differentiate the function f (x) = 3x2 − 1 at x = 5 using numerical differentiation techniques:

f (x + h) − f (x)
(a) Method 1: f ′ (x) =
h
f (x + h) − f (x − h)
(b) Method 2: f ′ (x) =
2h

Take h = 10−2 . Compare accuracy.


5. Write a program that stores the tabulated values of y versus x, as

x0 x1 x2 ··· xn
y0 y1 y2 ··· yn

where, yi = 2e−0.1xi sin(0.5xi ), x0 = 0 , xn = 60 with 100 equal divisions between x0 to xn . Write a


function that takes an arbitrary value of x from [x0 , xn ] and determines y by linear interpolation. Check
percentage error involved in estimation.
6. Solve the following equation numerically

x5 + 3x2 − 10 = 0

[Hints: Rearrange the equation as


( )1/5
x = 10 − 3x2

Start with a random guess. Update x iteratively until the new value is nearly equal to the old value.
Terminate if the difference is less than 10−5 or number of iterations is more than 50.]
7. Write a program that will read the value of x and evaluate the following function:


1, if x > 0
y = 0, if x = 0


−1, if x < 0

8. Plot y1 = sin(x) and y2 = cos(x) with x ∈ [0, 10π] on the same graph. Use two different colors. Take step
size as 0.1.
EE6243/EE637 Assignment 1B Page 2 of 4

9. Plot a circle with diameter 10 unit, center at (4,5) (do not use inbulit function for drawing the circle).
10. Plot ρ = θ2 with 0 ≤ ρ ≤ 5π in polar coordinates.
11. Step response of a standard second order system is given by
( √ )
e−ζωn t 1 − ζ 2
c(t) = 1 − √ sin ωd t + tan−1
1 − ζ2 ζ

(a) Plot the response for t ∈ [0, 30]s. Assume ωn = 1 rad/s, ζ = 0.2.
( )
e−ζωn t

(b) Indicate the exponential envelopes 1 ± 2
on the same figure.
1−ζ

12. Plot the probability density function of Normal Distribution.

1 (x−µ)2
f (x|µ, σ 2 ) = √ e− 2σ 2
2πσ 2
Where µ = mean; σ 2 variance. Take, (a) µ = 0, σ 2 = 1; (b) µ = 0, σ 2 = 0.2; (c) µ = 2, σ 2 = 0.5;
(d) µ = −2, σ 2 = 0.5. Plot all the above on same graph.
13. Plot the following functions for v ∈ [−5, 5].
(a) Saturating linear function: 

0, if v < −0.5
ϕ(v) = v + 0.5, if − 0.5 ≤ v ≤ 0.5


1, if v > 0.5
(b) Symmetric (bipolar) saturating linear function:

−1,
 if v < −1
ϕ(v) = v, if − 1 ≤ v ≤ 1


1, if v > 1

(c) Binary sigmoid function:


1
ϕ(v) =
1 + e−αv
Take α = 0.1, 0.5, 1.0 and plot all three functions on the same figure.
(d) Hyperbolic tangent sigmoid function:
eαv − e−αv
ϕ(v) = tanh(αv) =
eαv + e−αv
Take α = 0.1, 0.5, 1.0 and plot all three functions on the same figure.
14. Find the approximated value of cos(x) as


n
cos(x) ≈ C(x, n) = ci
i=0

where,
x2
ci = −ci−1 ; i = 0, 1, 2, . . . , n and c0 = 0
2i(2i − 1)
and x is expressed in radians. Write a program that will take the value of x from user and then calculate
its cosine. Write the program in two different ways:

Question 14 continues on the next page…


EE6243/EE637 Assignment 1B Page 3 of 4

(a) Sum of the first n terms, where n is a positive integer that is read into the computer along with the
numerical value for x.
(b) Continue adding successive terms in the series until the value of the next term becomes smaller than
10−5 .
15. Find the value of sin(x) from the Taylor polynomial approximation


n
x2i+1
sin(x) ≈ S(x, n) = (−1)i
i=0
(2i + 1)!

Compare the result. Use the termination criteria as stated in Problem No. 14.
16. The following sequences all converge to π:


n
(−1)k+1
(an )∞
0 , an =4
2k − 1
k=1
( ) 12

n
(bn )∞
0 , bn = 6 k −2

k=1
( ) 14

n
(cn )∞
0 , cn = 90 k −4

k=1

6 ∑ (−1)k
n
(dn )∞
0 , dn =√
3 k=0 3k (2k + 1)


n
(−1)k ∑ (−1)k
n
(en )∞
0 , en = 16 2k+1
−4 2k+1
5 (2k + 1) 239 (2k + 1)
k=0 k=0

Make a function for each sequence that returns an array with the elements in the sequence. Plot all the
sequences, and find the one that converges fastest toward the limit π.
17. (a) Write a function that take x̄ ∈ R2 and returns

y = 3x31 + 8x22 − 5x1 x2

(b) Write two separate functions for calculating the gradient (ḡ) and hessian matrix (H) of the above
functions, where,
[ 2 ]
9x1 − 5x2
ḡ =
16x2 − 5x1
[ ]
18x1 −5
H =
−5 16

(c) Write a program to test Taylor series expansion:


1 T
f (x̄ + h̄) = f (x̄) + h̄T ḡ + h̄ H h̄ + · · · higher order terms
2!
[ ] [ ]
0 0.2
Take x̄ = , and h̄ = . Check accuracy.
0 −0.3
EE6243/EE637 Assignment 1B Page 4 of 4

18. Suppose we are given a number of discrete points (x1 , y1 ), (x2 , y2 ), …, (xn , yn ) read from a curve y = f (x),
where x is bounded between x1 and xn . We wish to approximate the area under the curve by breaking
up the curves into number of small rectangles and calculating the area of these reactangles (trapezoidal
rule). The appropriate formula is:
( ) ( ) ( )
x2 − x1 x3 − x2 xn − xn−1
A = (y1 + y2 ) + (y2 + y3 ) + . . . + (yn−1 + yn )
2 2 2

Write a program to implement this strategy, using a function to evaluate y = f (x). Use the program to
calculate the area under the curve y = x3 , between the limits x = 1 and x = 4. Solve this problem first
with 16 evenly spaced points, then with 61 points, and finally with 301 points.
19. Problem 18 describes trapezoidal rule to calculate area under a curve y = f (x). If the tabulated values of
x are equally spaced, the equation given in the preceding problem can be simplified to read

h
A= (y1 + 2y2 + 2y3 + 2y4 + · · · + 2yn−1 + yn )
2
where, h is the distance between successive values of x. Write a program to calculate the area under the
curve y = e−x , where, x ∈ [0, 1].
20. A normally distributed random variable x, with mean µ and standard deviation σ, can be generated from
the formula:
∑N
i=1 ri − N
x=µ+σ √ 2
N
12

where, ri is a uniformly distributed random number whose value lies between 0 and 1. Write a program
that will generate a specified number of normally distributed random variates with a given mean and
a given standard deviation. The number of random variates, the mean and the standard deviations are
inputs to the program.

S-ar putea să vă placă și