Numerical Computing, Past Papers

Numerical Computing BSCS 6th Term Past Paper 2015 UOS

Numerical Computing BSCS 6th term past paper 2015 University of Sargodha

University of Sargodha Numerical Computing (MTH-310) past paper for BS Computer Science 6th term 2015, retyped from the original paper, with solved answers below.

Solved answers

Q1 – Short questions

(i) Numerical method: a step-by-step procedure that finds an approximate numerical solution of a mathematical problem (roots, integrals, systems of equations) using arithmetic operations, usually by iteration on a computer, when an exact analytical solution is hard or impossible.

(ii) Absolute and relative error: absolute error = |true value − approximate value|. Relative error = absolute error / |true value| (often ×100 for percentage).

(iii) Significant digits: the digits of a number that carry meaning about its precision — all non-zero digits, zeros between them, and trailing zeros after a decimal point; leading zeros are not significant (0.004560 has 4 significant digits).

(iv) Propagation of errors: in addition and subtraction the absolute errors add (|Δx| + |Δy|); in multiplication and division the relative errors add approximately. Subtracting nearly equal numbers greatly increases the relative error (loss of significance).

(v) Bisection for x² − 11 on [3, 5]: f(3) = −2, f(5) = 14; midpoint 4: f(4) = 5 > 0. The sign changes between 3 and 4, so the next range is [3, 4] (root √11 ≈ 3.317).

(vi) Geometry of Newton–Raphson: at the current guess xₙ draw the tangent to the curve y = f(x); the point where this tangent cuts the x-axis is the new guess: xₙ₊₁ = xₙ − f(xₙ)/f′(xₙ).

(vii) Drawbacks of Gauss elimination: division by zero if a pivot is zero; large round-off errors with small pivots (needs pivoting); about n³/3 operations, so expensive for large systems; must be repeated for every new right-hand side; fails for singular or ill-conditioned matrices.

(viii) Nature of the eigen-system: for a square matrix A, Ax = λx; the eigenvalues λ are roots of det(A − λI) = 0 (n values, real or complex) and each has an eigenvector x ≠ 0. Symmetric matrices have real eigenvalues and orthogonal eigenvectors.

(ix) Interpolation: estimating the value of a function between known data points by constructing a function (e.g. a polynomial) that passes through those points.

(x) Least-squares fit: finding the curve (e.g. straight line y = a + bx) that best fits data by minimising the sum of squares of the vertical deviations Σ(yᵢ − f(xᵢ))².

(xi) Numerical differentiation: approximating derivatives from function values at discrete points using difference formulas, e.g. f′(x) ≈ [f(x + h) − f(x)]/h or the central difference [f(x + h) − f(x − h)]/2h.

(xii) Usefulness of numerical integration: it evaluates definite integrals when the antiderivative is unknown or very complicated, or when the function is known only as tabulated/experimental data (areas, volumes, work, probabilities).

(xiii) Trapezoidal rule: ∫ₐᵇ f(x)dx ≈ (h/2)[y₀ + 2(y₁ + y₂ + … + yₙ₋₁) + yₙ], where h = (b − a)/n.

(xiv) Monte Carlo method: a method that uses random sampling to estimate numerical results — e.g. an integral is approximated by the average of f at random points times the interval length; useful for high-dimensional problems.

(xv) Polynomial interpolation: finding a polynomial Pₙ(x) = a₀ + a₁x + … + aₙxⁿ of degree ≤ n that passes through n + 1 data points; forms include Lagrange and Newton’s divided-difference/forward-difference forms.

(xvi) Pivoting: rearranging rows (partial pivoting) or rows and columns (complete pivoting) so that the element with the largest absolute value becomes the pivot; it avoids division by zero and reduces round-off errors in Gauss elimination.

Q2 – C++ program to solve AX = B (3×3) by Gauss elimination

#include <iostream>
#include <cmath>
using namespace std;

void readMatrix(double A[3][3]) {
    cout << "Enter matrix A (3x3):\n";
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            cin >> A[i][j];
}

void readVector(double B[3]) {
    cout << "Enter vector B (3x1):\n";
    for (int i = 0; i < 3; i++)
        cin >> B[i];
}

bool solve(double A[3][3], double B[3], double X[3]) {
    const int n = 3;
    for (int k = 0; k < n; k++) {
        int p = k;                                   // partial pivoting
        for (int i = k + 1; i < n; i++)
            if (fabs(A[i][k]) > fabs(A[p][k])) p = i;
        if (fabs(A[p][k]) < 1e-12) return false;     // singular
        for (int j = 0; j < n; j++) swap(A[k][j], A[p][j]);
        swap(B[k], B[p]);
        for (int i = k + 1; i < n; i++) {            // elimination
            double m = A[i][k] / A[k][k];
            for (int j = k; j < n; j++) A[i][j] -= m * A[k][j];
            B[i] -= m * B[k];
        }
    }
    for (int i = n - 1; i >= 0; i--) {               // back substitution
        double s = B[i];
        for (int j = i + 1; j < n; j++) s -= A[i][j] * X[j];
        X[i] = s / A[i][i];
    }
    return true;
}

int main() {
    double A[3][3], B[3], X[3];
    readMatrix(A);
    readVector(B);
    if (solve(A, B, X))
        for (int i = 0; i < 3; i++)
            cout << "x" << i + 1 << " = " << X[i] << "\n";
    else
        cout << "Matrix is singular.\n";
    return 0;
}

Q3 – Algorithm of the bisection method

  1. Input f(x), interval [a, b] and error limit ε (and maximum iterations).
  2. If f(a)·f(b) > 0, print “no root guaranteed in this interval” and stop.
  3. Repeat: c = (a + b)/2.
  4. If f(c) = 0 or (b − a)/2 < ε, output c as the root and stop.
  5. If f(a)·f(c) < 0 then b = c, else a = c.
  6. Go to step 3 (until the error limit or maximum iterations is reached).

The interval halves every step, so the method always converges (linearly) if f is continuous and changes sign.

Q4 – Gauss elimination method

Basic idea: transform the system AX = B into an equivalent upper-triangular system using elementary row operations (forward elimination), and then find the unknowns from the last equation upward (back substitution).

  1. Write the augmented matrix [A | B].
  2. Forward elimination: for each column k, choose the pivot aₖₖ (use partial pivoting — swap in the row with the largest |aᵢₖ|). For every row i below k, compute the multiplier mᵢₖ = aᵢₖ/aₖₖ and replace row i by row i − mᵢₖ × row k, making all entries below the pivot zero.
  3. After n − 1 steps the matrix is upper triangular.
  4. Back substitution: xₙ = bₙ/aₙₙ, then xᵢ = (bᵢ − Σⱼ₌ᵢ₊₁ⁿ aᵢⱼxⱼ)/aᵢᵢ for i = n − 1, …, 1.

Example: 2x + y + z = 5, 4x − 6y = −2, −2x + 7y + 2z = 9 → after elimination x = 1, y = 1, z = 2. Cost ≈ n³/3 multiplications.

Q5 – Trapezoidal rule: statement, derivation and C++ program

Statement: ∫ₐᵇ f(x)dx ≈ (h/2)[y₀ + 2(y₁ + … + yₙ₋₁) + yₙ], h = (b − a)/n.

Derivation: on one strip [x₀, x₁], replace f by the straight line (linear interpolation) through (x₀, y₀) and (x₁, y₁). The area under it is a trapezium: ∫ₓ₀ˣ¹ f(x)dx ≈ (h/2)(y₀ + y₁). Adding the n strips: (h/2)[(y₀ + y₁) + (y₁ + y₂) + … + (yₙ₋₁ + yₙ)] = (h/2)[y₀ + 2(y₁ + … + yₙ₋₁) + yₙ]. The error is −(b − a)h²f″(ξ)/12, so it is exact for linear functions.

#include <iostream>
using namespace std;

int main() {
    int n;
    double a, b;
    cout << "Enter lower limit a, upper limit b and number of intervals n: ";
    cin >> a >> b >> n;
    double h = (b - a) / n;
    double y[1000];
    cout << "Enter the " << n + 1 << " function values y0 ... yn:\n";
    for (int i = 0; i <= n; i++) cin >> y[i];

    double sum = y[0] + y[n];
    for (int i = 1; i < n; i++) sum += 2 * y[i];
    double integral = (h / 2) * sum;

    cout << "Integral = " << integral << "\n";
    return 0;
}

Q6 – Forward differences and forward difference tables

For equally spaced points xᵢ = x₀ + ih with values yᵢ, the first forward difference is Δyᵢ = yᵢ₊₁ − yᵢ; the second is Δ²yᵢ = Δyᵢ₊₁ − Δyᵢ; in general Δᵏyᵢ = Δᵏ⁻¹yᵢ₊₁ − Δᵏ⁻¹yᵢ. Δ is the forward difference operator (Δ = E − 1).

A forward difference table lists x, y and successive differences in columns. Example for y = x³:

xyΔyΔ²yΔ³y
117126
2819186
3273724
46461
5125

For a polynomial of degree n the nth differences are constant (here Δ³y = 6). The table is used in Newton’s forward interpolation formula y = y₀ + pΔy₀ + p(p − 1)Δ²y₀/2! + …, with p = (x − x₀)/h, and in numerical differentiation and integration.

Q7 – Solving a matrix eigen-equation by the polynomial method

  1. Write the eigen-equation Ax = λx as (A − λI)x = 0.
  2. For non-zero x, the matrix must be singular: det(A − λI) = 0.
  3. Expand the determinant to get the characteristic polynomial of degree n in λ.
  4. Find its roots (analytically for small n, or numerically with Newton–Raphson/bisection) — these are the eigenvalues λ₁, …, λₙ.
  5. For each λᵢ solve (A − λᵢI)x = 0 (e.g. by Gauss elimination) to get the eigenvector xᵢ (normalise if required).

Example: A = [[2, 1], [1, 2]] → det(A − λI) = (2 − λ)² − 1 = λ² − 4λ + 3 = 0 → λ = 1, 3. For λ = 3: x = (1, 1); for λ = 1: x = (1, −1). The method is practical only for small matrices; for large ones iterative methods (power method, QR) are used.

Answers prepared by Shining Study for revision. Check them against your class notes and teacher’s guidance.

About Saif R

An educator and technology enthusiast passionate about empowering students through quality education and digital learning. Dedicated to making academic resources and opportunities accessible to everyone.