Blog
Numerical Computing Past Paper 2016 – BSCS 5th Term UOS (CS-3941)
University of Sargodha Numerical Computing (CS-3941) paper for BS Computer Science, 2016 exam (80 marks, 2:30 hours). The original paper’s header was partly cut, so the term (5th) is taken from the course code. The paper has been retyped, and solved answers are given below for all 16 short questions and all six long questions, with tested C++ programs and calculations checked by computer.
Solved answers
Q1 – Short answers (16 × 2)
i. Error analysis: The study of the kind and size of errors in a numerical result – round-off, truncation and propagated errors – using absolute error |p − p*| and relative error |p − p*|/|p|, to judge how accurate the answer is.
ii. Bisection on [2, 3] for x³ − 2x − 5: f(2) = −1, f(3) = 16. Midpoint 2.5: f(2.5) = 15.625 − 5 − 5 = 5.625 > 0. The sign change is between 2 and 2.5, so the next range is [2, 2.5].
iii. Regula falsi (geometric idea): The new guess is where the straight line (chord) joining (a, f(a)) and (b, f(b)) crosses the x-axis: c = (a f(b) − b f(a))/(f(b) − f(a)). The endpoint with the same sign as f(c) is replaced by c, so the root stays bracketed.
iv. Functional (fixed-point) iteration: Rewrite f(x) = 0 as x = g(x) and compute xn+1 = g(xn) from a starting value. It converges when |g′(x)| < 1 near the root.
v. Drawbacks of Gauss elimination: Division by zero or by very small pivots (needs pivoting), growth of round-off error in large or ill-conditioned systems, about n³/3 operations, and the whole process must be repeated for every new right-hand side.
vi. Nature of an eigen-system: For a square matrix A, the problem Av = λv: the eigenvalues λ are roots of det(A − λI) = 0 and each gives an eigenvector v. A symmetric matrix has real eigenvalues and orthogonal eigenvectors.
vii. Newton–Raphson method: xn+1 = xn − f(xn)/f′(xn): follow the tangent at xn to the x-axis. It converges quadratically near a simple root.
viii. Lagrange polynomial: The interpolating polynomial P(x) = Σ f(xk) Lk(x) through n + 1 points, where Lk(x) = Πi≠k (x − xi)/(xk − xi).
ix. Least squares fit: Choosing the curve (e.g. y = a + bx) that minimises the sum of squared residuals Σ(yi − ŷi)², found by solving the normal equations.
x. Numerical integration: Approximating a definite integral ∫ab f(x) dx by a weighted sum of function values (quadrature), e.g. trapezoidal and Simpson’s rules, when the antiderivative is hard or the function is known only at points.
xi. Trapezoidal rule for ∫0.52π x⁴ dx: T = (b − a)/2 [f(a) + f(b)] = (2π − 0.5)/2 × (0.5⁴ + (2π)⁴) = 2.892 × 1558.61 ≈ 4,506.9 (exact value 1,958.5, so one interval over-estimates heavily). If the upper limit is 1, as in the usual textbook exercise: T = (0.5/2)(0.0625 + 1) = 0.2656 (exact 0.19375).
xii. LU decomposition: Writing A = LU, with L lower triangular and U upper triangular, so Ax = b is solved by forward substitution Ly = b, then back substitution Ux = y.
xiii. Monte Carlo method: A numerical method that uses random sampling to estimate a quantity, e.g. ∫ab f(x) dx ≈ (b − a)·(1/N)Σ f(xi) for random xi. Its error falls like 1/√N.
xiv. Linear interpolation: Estimating a value between two known points by the straight line through them: f(x) ≈ f(x0) + (x − x0)(f(x1) − f(x0))/(x1 − x0).
xv. Propagation error: The error in a result caused by errors already present in the input values or earlier steps, carried forward and possibly magnified through the calculation.
xvi. Main issues of nonlinear systems: There may be no solution, several solutions or none near the guess; convergence depends on a good starting value; derivatives (Jacobian) are needed and may be costly or singular; and iterations can diverge or oscillate.
Q2 – Fixed-point iteration for x⁴ − 3x² − 3 = 0
Rewrite as x⁴ = 3x² + 3, so x = g(x) = (3x² + 3)1/4. On [1, 2], g′(x) = 1.5x(3x² + 3)−3/4 stays below 1, so the iteration converges. Start with x0 = 1 and stop when |xn − xn−1| < 10−2:
| n | xn = g(xn−1) | |xn − xn−1| |
|---|---|---|
| 1 | 1.56508 | 0.56508 |
| 2 | 1.79357 | 0.22849 |
| 3 | 1.88594 | 0.09237 |
| 4 | 1.92285 | 0.03690 |
| 5 | 1.93751 | 0.01466 |
| 6 | 1.94332 | 0.00581 |
So x ≈ 1.943 (the true root is 1.9471). C++ code (tested):
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
// g(x) = (3x^2 + 3)^(1/4) is a rearrangement of x^4 - 3x^2 - 3 = 0
double g(double x) { return pow(3 * x * x + 3, 0.25); }
int main() {
double x0 = 1.0, x1, tol = 1e-2;
int n = 0, maxIter = 50;
cout << fixed << setprecision(5);
do {
x1 = g(x0);
n++;
cout << "Iteration " << n << ": x = " << x1
<< " |x1 - x0| = " << fabs(x1 - x0) << endl;
if (fabs(x1 - x0) < tol) break;
x0 = x1;
} while (n < maxIter);
cout << "Root = " << x1 << " after " << n << " iterations" << endl;
return 0;
}Q3 – False position (regula falsi) in C++
The program below finds a root of the transcendental equation f(x) = x ex − 1 = 0. With a = 0, b = 1 and tolerance 10−6 it gives 0.567143 after 13 iterations (tested).
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
// Transcendental equation: f(x) = x * e^x - 1 = 0 (root near 0.567)
double f(double x) { return x * exp(x) - 1; }
int main() {
double a, b, c, tol;
int maxIter, i;
cout << "Enter interval a and b: ";
cin >> a >> b;
cout << "Enter tolerance and maximum iterations: ";
cin >> tol >> maxIter;
if (f(a) * f(b) > 0) {
cout << "f(a) and f(b) must have opposite signs." << endl;
return 1;
}
cout << fixed << setprecision(6);
for (i = 1; i <= maxIter; i++) {
// point where the chord through (a, f(a)) and (b, f(b)) cuts the x-axis
c = (a * f(b) - b * f(a)) / (f(b) - f(a));
cout << "Iteration " << i << ": c = " << c << " f(c) = " << f(c) << endl;
if (fabs(f(c)) < tol) break;
if (f(a) * f(c) < 0) b = c; // root lies in [a, c]
else a = c; // root lies in [c, b]
}
cout << "Approximate root = " << c << endl;
return 0;
}- Variables: a, b – ends of the interval that brackets the root; c – the false-position estimate; tol – required accuracy; maxIter – limit on iterations; i – iteration counter.
- Facilities used: <iostream> for cin/cout, <cmath> for exp() and fabs(), <iomanip> for setprecision(); a user-defined function f(x); a for loop with break; and a sign check f(a)·f(b) < 0 so the root stays bracketed.
Q4 – Gauss elimination
Basic idea: write the system Ax = b as an augmented matrix [A | b]. Use row operations to eliminate the unknowns below the diagonal column by column, turning A into an upper-triangular matrix. Then find the unknowns by back substitution, starting from the last equation. Partial pivoting (swapping in the row with the largest pivot) reduces round-off errors.
Algorithm (n equations):
- Input n and the augmented matrix a[i][j], i = 1…n, j = 1…n + 1.
- For k = 1 to n − 1 (forward elimination): find the row p ≥ k with the largest |a[p][k]| and swap rows p and k; if a[k][k] = 0, stop (no unique solution).
- For i = k + 1 to n: m = a[i][k]/a[k][k]; for j = k to n + 1: a[i][j] = a[i][j] − m·a[k][j].
- Back substitution: x[n] = a[n][n + 1]/a[n][n].
- For i = n − 1 down to 1: x[i] = (a[i][n + 1] − Σj=i+1..n a[i][j]x[j]) / a[i][i].
- Output x[1], …, x[n].
Q5 – Simpson’s 1/3 rule
Statement: for two equal sub-intervals of width h = (b − a)/2 with x0 = a, x1 = a + h, x2 = b:
∫x0x2 f(x) dx ≈ (h/3)[f(x0) + 4f(x1) + f(x2)], with error −(h⁵/90) f⁽⁴⁾(ξ).
Derivation: replace f by the quadratic through the three points. Put x = x1 + th (t from −1 to 1) and p(t) = f1 + t(f2 − f0)/2 + t²(f2 − 2f1 + f0)/2. Then ∫ f dx ≈ h∫−11 p(t) dt = h[2f1 + 0 + (1/3)(f2 − 2f1 + f0)] = (h/3)(f0 + 4f1 + f2), because the odd term integrates to 0 and ∫t² dt = 2/3.
Composite rule (n even): ∫ab f dx ≈ (h/3)[f0 + 4(f1 + f3 + …) + 2(f2 + f4 + …) + fn], h = (b − a)/n.
Algorithm:
- Input a, b and an even n; h = (b − a)/n.
- sum = f(a) + f(b).
- For i = 1 to n − 1: x = a + ih; if i is odd, sum = sum + 4f(x), else sum = sum + 2f(x).
- Integral = (h/3)·sum; output it.
Q6 – Two-point formula for the first derivative
Statement (forward difference): f′(x0) ≈ [f(x0 + h) − f(x0)]/h, with error −(h/2)f″(ξ). (Backward form uses h < 0.)
Derivation: Taylor series: f(x0 + h) = f(x0) + hf′(x0) + (h²/2)f″(ξ). Rearranging, f′(x0) = [f(x0 + h) − f(x0)]/h − (h/2)f″(ξ). Dropping the last term gives the formula; the error is O(h). (The same result follows from differentiating the linear Lagrange polynomial through x0 and x0 + h.)
Algorithm:
- Input the function f, the point x0 and step size h (small, e.g. 0.01).
- f0 = f(x0); f1 = f(x0 + h).
- d = (f1 − f0)/h.
- Output d as the approximate value of f′(x0). Optionally repeat with h/2 to check the accuracy.
Example: f(x) = ln x, x0 = 1.8, h = 0.1: (ln 1.9 − ln 1.8)/0.1 = 0.5407, exact 1/1.8 = 0.5556.
Q7 – Composite trapezoidal rule for ∫12 x ln x dx
Take n = 4, h = 0.25:
| x | 1 | 1.25 | 1.5 | 1.75 | 2 |
|---|---|---|---|---|---|
| f(x) = x ln x | 0 | 0.278929 | 0.608198 | 0.979328 | 1.386294 |
T = (h/2)[f0 + 2(f1 + f2 + f3) + f4] = 0.125[0 + 2(1.866455) + 1.386294] = 0.639900.
Exact value: ∫ x ln x dx = (x²/2)ln x − x²/4, so from 1 to 2 it is 2 ln 2 − 0.75 = 0.636294. Error ≈ 0.0036 (0.57%).
