İzmir Kâtip Çelebi University
Department of Engineering Sciences
IKC-MH.55
Scientific Computing with Python
Take-home Midterm Examination
Nov 24, 2023 14:00 Dec 18, 2023 23:59
Good Luck!
NAME-SURNAME:
SIGNATURE:
ID:
DEPARTMENT:
DURATION: Due to Dec 18, 2023
Answer at least 1 question from each
parts.
Prepare your report/codes.
Copy your files into a directory named
as your ID.
Upload a single file by compressing
this directory to UB YS.
Question Grade Out of
1 25
2 25
3 25
4 25
TOTAL 100
This page is intentionally left blank. Use the space if needed.
Part I
Numerical Techniques: Root
Searching
Choose only o ne question.
A) (25pts) Write a program that finds the roots of t he following functions
in the given intervals using the interval halving, secant and Newton-
Raphson methods:
lnx +
x 2 = 0 [1, 2]
2x e
x
= 0 [0, 1]
B) (25pts) van der Waals E quation The equation of state P V = nRT
for ideal gases approximates the state of real gases. A more accurate
equation for real gases stated by va n der Waals as:
(P +
a
v
2
)(v b) = RT
Here, v = V/n is the molar volume, R = 0 .08207 liter.atm/mol.K is
the ideal gas constant, and a and b parameters change as depending on
the gas. For carbon monoxide (CO), a = 3.592 and b = 0.04267. Write
a program that finds the volume v of 1 mole of CO gas at a temperature
of T = 320 K and at a pressure of P = 2.2 atm and compares it with
the ideal gas equation P v = RT .
Part II
Numerical Techniques: Numerical
Differentiation and Integration
Choose only o ne question.
A) (25pts) Write a program that calculates the 1st derivatives with central-
, forward- and backward-difference approximations at the given points
and compares them with the exact value:
f(x) = xe
x
(at x = 2)
1
B) (25pts) Investigate the effect of the the step length as h = 0.1, 0.01, 0.0 01
on the central-, f orward- and backward-difference approximations of the
2nd derivative of the function f (x) = lnx at the point x = 1. Compare
with the analytic solution of f
′′
(x) = 1/x
2
.
Choose only o ne question.
C) (25pts) Calculate the following integrals with the t r apezoidal formula
at N=4,8,16 points and compare with their exact value.
Z
10
1
dx
x
= ln10
Z
1
0
dx
2 + x
= ln
3
2
D) (25pts) Area of the ellipse. Equation of an ellipse with long axis a
and short axis b is given as:
x
2
a
2
+
y
2
b
2
= 1
By considering the surface area in the first quadrant, the area of this
ellipse is S and
S = 4
Z
a
0
y(x)dx
Write a progra m that calculates this expression using the Trapezoidal
formula for the values a= 2, b=1, and compare it with the exact result,
S = πab.
Part III
Numerical Techniques:
Differential Equations - Initial
Value Problems
Choose only o ne question.
A) (25pts) Write a program that solves the following initial value prob-
lems for the given range, initial condition and step length using the
2
Euler and fourth order Runge-Kutta methods. Compare your results
with the analytical solution.
y
= yc o s x 0 x 1, y(0) = 1 , h = 0.05
y
= cos2x + sin3x 0 x 1, y( 0) = 1, h = 0.01
The analytical solutions of these equations are:
y(x) = xe
sinx
y(x) =
1
2
sin2x
1
3
cos3x +
4
3
B) (25pts) First convert the fo llowing quadratic differential equations into
a linear system of equations and write a program that solves them using
the Runge-Kutta method:
y
′′
= yy
0 x 1, y(0) = 1 , y
(0) = 1; h = 0.1
y
′′
2y
+ y = e
x
0 x 1, y(0) = y
(0) = 0; h = 0.1
C) (25pts) RLC circuits. In the alternating current circuit shown in
the figure, the voltage on each circuit element occurs as follows: Ri on
the resistor R, L(di/dt) on the inductor L and q/C on the capacitor
C. Accordingly, a voltage a pplied between terminals AB would be:
L
di
dt
+ Ri +
q
C
= V
AB
Let’s take the derivative of this equa-
tion according to the va r ia ble t and use
the relation dq/ dt = i:
L
d
2
i
dt
2
+ R
di
dt
+
i
C
=
dV
AB
dt
This differential equation is a driven da mped harmonic equation of
motion.
A voltage of V
AB
= 10sin(ωt) is applied to the circuit with R = 5 ,L =
0.1 H, C = 0.001 F . First convert this equation into a linear system,
then write a program to solve it by using Runge-Kutta method for the
frequency ω = 3 00 Hz. Take the numerical data as h = 0.01, ω = 1
and b = 0.5 and test tha t simple harmonic motion occurs at b = 0
(where b=R/L).
3