0% found this document useful (0 votes)
12 views2 pages

Midterm Exam CH2013 Batch1

dcv

Uploaded by

ch23b043
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Midterm Exam CH2013 Batch1

dcv

Uploaded by

ch23b043
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Midterm Exam (18 Jan.

2024)
CH2013: Computational Programming and Simulations Lab

Duration: 2 hours 45 minutes


Max marks: 100

Note: Use of the internet is NOT allowed during the exam time. DO NOT use the inbuilt
functions of MATLAB to solve the problems , unless specified in the question paper. CH2061 class
notes (but no codes) are allowed. Your script should be executable without requiring any
modifications. Name your codes as problem1.m, problem2.m, problem3.m, and problem4.m.
At 4:45 PM, please submit your codes to MATLAB Grader.

(Marks: 10)
1. Write a code to evaluate the sum of first N positive integers for a given N. The code should
check if N is a positive integer and then do the calculation. If the given N is not a positive integer,
the code should give a warning message that “Given N is not a positive integer”.

(Marks: 20)
2. The probability of getting two 6s in one throw of two perfect dice is 1/36. Write a code to
determine it by simulating this experiment on a computer. You can select an integer randomly
using randi([N1 N2]), where N1 and N2 are two integers with N1 < N2. Note that the code should
repeat this simulation a sufficient number of times to get reasonably accurate results.

(Marks: 20)
3. Write a for loop to create an 8X8 matrix (name it as A) such that its entries 𝑎𝑖𝑗 follow the
following rules:
𝑎𝑖𝑗 = 2 𝑖𝑓 𝑖 = 2𝑗
𝑎𝑖𝑗 = 1 𝑖𝑓 𝑗 = 2𝑖
𝑎𝑖𝑗 = 0 𝑒𝑙𝑠𝑒𝑤ℎ𝑒𝑟𝑒
Initialize A as a matrix of zeros, and then start a for loop to update the non-zero entries in the
matrix.

(Marks: 50)
4. Use the Newton-Raphson method to find x such the function f(x) defined below is equal to 0:
3
𝑓(𝑥) = 𝑒 𝑥 − 2𝑥 2 + 𝑥 + 1
Save the latest value of 𝑥 in variable xnew and the values from the previous iteration in
variable xold.
a. Create a for loop that runs for Niter number of times. In that for loop, implement the
Newton-Raphson method by evaluating: xnew = xold - f(xold)/f’(xold), where f’ is the
derivative of f. Keep updating xnew till either i) number of iterations are over, or ii) the
relative error (defined below in d) is less than tol and absolute value of f(xnew) is less than
tol.
b. Develop a function myfun which calculates f(x). The input is x and the output is f(x).
c. Develop a function myfun_der which calculates f’(x). The input is x and the output is f’(x).
d. Develop a function RelativeError which calculates the maximum of the relative error
𝑥𝑛𝑒𝑤−𝑥𝑜𝑙𝑑
defined as 100 | |. The inputs to the function are xnew and xold. Store the output
𝑥𝑛𝑒𝑤
in variable error.
e. The code should run until the tolerance is less than tol for both error and f(xnew), with
an upper limit on the number of iterations, Niter. Even if the tolerance criteria are not
met, the code should not execute more than Niter iterations. These conditions should be
added using a for loop, with a break in the if/else statement that will check if the tolerance
is less than tol.
f. Make sure to call myfun, myfun_der, and RelativeError.
g. After the for loop is complete or the tolerance criteria is met, your code should output
the final values of xnew, f(xnew), and error.
h. For the initial guess, use xold=-1, Niter=30 and tol=10-6.

Structure of the code


declare variables
for number of iterations
call the relevant functions
check if relative error and f(xnew) are lesser than the tolerance; if yes then break
end
print required variables

define functions
myfun (xold)
myfun_der (xold)
RelativeError(xnew,xold)

You might also like