Skip to content

me701/hw_numerical_cpp_fortran

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

ME 701 — Numerical C++ and Modern Fortran

Students must solve at least one problem in C++, at least one in Modern Fortran, and may choose either language for the third.

In addition, students must provide a Makefile that compiles each of the solutions. The targets should be problem1, problem3, and problem3.

Problem 1 — Piecewise Cooling Curve

Write a program that evaluates the temperature of an object over time using the model

$$ T(t) = \begin{cases} T_0 - a t, & 0 \le t < t_c \\ T_0 - a t_c - b (t - t_c)^2, & t \ge t_c \end{cases} $$

Tasks

  1. Input parameters: T0, a, b, t_c, and number of steps N.

  2. Build a 1D array of times: $t_i = i,\Delta t,\quad \Delta t = t_{\max} / (N-1)$

  3. Evaluate T(t) using a function you define.

  4. Print:

    • the minimum temperature,
    • the time at which it occurs,
    • the last five values of the temperature array.

Concepts required: if/else, loops, functions, arrays, C++ pass-by-value/reference/pointer or Fortran intent.


Problem 2 — Vector Statistics with In-Place Normalization

Write a small "statistics library" that works on a 1D array of doubles.

Functions / subroutines to implement

  • mean(x, N)
  • variance(x, N) (sample variance)
  • normalize(x, N) — modifies the existing array so values have zero mean and unit variance

Tasks

  1. Initialize an array as $x_i = \sin(i) + 0.1,i$
  2. Print the mean and variance before normalization.
  3. Normalize the array in place:
    • C++: use reference or pointer semantics
    • Fortran: use intent(inout)
  4. Print the mean and variance after normalization.

Concepts required: arrays, function interfaces, pass-by-reference / intent(inout), numerical loops.


Problem 3 — 2D Mesh + Discrete Laplacian

Construct a 2D uniform grid over $[0,1]\times[0,1]$.

Tasks

  1. Input grid size N.
  2. Allocate:
    • u(N, N)
    • Lu(N, N)
  3. Initialize $u(x,y) = \sin(\pi x)\sin(\pi y)$
  4. Compute the 5-point Laplacian for all interior points: $(Lu){i,j} =\frac{u{i+1,j} + u_{i-1,j} + u_{i,j+1} + u_{i,j-1} - 4u_{i,j}}{h^2}$
  5. Apply Dirichlet boundaries by leaving edges unchanged (i.e., Laplacian = 0 there).
  6. Print:
    • Lu at the center grid point,
    • maximum absolute entry of Lu.

Concepts required: 2D arrays, nested loops, numerical stencils, function/subroutine for Laplacian, C++ flattened arrays or MF native 2D arrays.


About

Basic numerical exercises for C++ and Modern Fortran

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors