AI-Assisted Engineering Programming

AI-assisted engineering programming uses an AI system to help translate a stated engineering calculation into code. You can ask for a function, an explanation, or proposed tests, but you still need evidence that the result implements the intended model. A program can run successfully and return a plausible number while using the wrong units, assumptions, or numerical method.

Specify the calculation before requesting code

A numerical contract states what a program accepts, what it returns, and which mathematical rules connect them. Include input units, output units, valid input ranges, the required numerical method, and how invalid inputs should be handled. This gives you a basis for evaluating the generated program.


For example, suppose a heater's electrical power is sampled at equally spaced times. Your chosen model estimates energy using trapezoidal integration. For nn readings, the contract can specify


Etrap=Δt1000[P02+∑i=1n−2Pi+Pn−12].E_{\text{trap}}=\frac{\Delta t}{1000}\left[\frac{P_0}{2}+\sum_{i=1}^{n-2}P_i+\frac{P_{n-1}}{2}\right].


Here, each PiP_i is in watts, the spacing Δt\Delta t is in seconds, and the returned energy is in kilojoules. The factor 10001000 converts joules to kilojoules because 1 W=1 J/s1\ \text{W}=1\ \text{J/s}. For two readings, the interior sum is empty. There are n−1n-1 time intervals, so the record spans (n−1)Δt(n-1)\Delta t seconds.


A useful request names this equation and specifies at least two finite readings with positive time spacing. It also states whether negative power is meaningful for the application. Asking for “an energy calculator” leaves these decisions unresolved.



Treat the response as a candidate implementation

An AI response may include code, comments, and tests that agree with one another. Agreement within the response does not establish agreement with your engineering model. Read the calculation itself: check conversion factors, endpoint treatment, and which inputs actually control the result.


Four connected boxes show specification, AI drafting, independent checking, and a decision to accept or revise.


An independent reference is an expected result obtained without relying on the generated implementation's calculation. It might come from a hand calculation, a known special case, or a separately established analytical result. Tests proposed by AI are useful candidates, but their expected values also need checking.


Check

What it can establish

What it leaves unresolved

Code executes

The selected input reaches a result

Whether the model and units are correct

AI-generated tests pass

Code agrees with those test expectations

Whether those expectations are correct

Independent reference agrees

The checked case matches the specified calculation

Whether other cases are implemented correctly

Scaling checks pass

Selected proportional relationships hold

Whether an incorrect formula shares those relationships


For the energy calculation, constant power gives a convenient reference: energy equals power multiplied by the record duration. A nonconstant trace checks endpoint weighting more directly. Both cases add useful evidence because they challenge different features of the implementation.

Decide what the evidence supports

Compare the generated result with the reference using a declared error measure. For a nonzero reference value,


absolute relative error=∣EAI−Eref∣∣Eref∣.\text{absolute relative error}=\frac{|E_{\text{AI}}-E_{\text{ref}}|}{|E_{\text{ref}}|}.


Multiply by 100100 to express this error as a percentage. If the reference is zero, use an absolute difference with units instead. Set an appropriate acceptance tolerance before interpreting the comparison; an arbitrarily generous tolerance can hide a systematic error.



Passing tests supports specific claims about specific cases. It does not prove correctness for every input. Also distinguish implementing the chosen numerical method from accurately representing the physical system: matching a trapezoidal reference verifies that calculation, while sampling adequacy and model suitability require separate evidence.

What to remember

  • Specify the model, units, input conditions, and numerical method before requesting code.
  • Check AI-generated expectations against independent references.
  • Use complementary tests and state exactly what their results establish.

Example: Reviewing an AI-generated energy function

A laboratory heater's power is recorded as [400,600,800] W[400,600,800]\ \text{W} at times 00, 55, and 10 s10\ \text{s}. You request an AI-assisted engineering programming solution that estimates energy using trapezoidal integration and returns kilojoules.


The AI assistant provides:


Python
def energy_kj(power_w, dt_s):
    return sum(power_w) * dt_s / 1000.0


Assume inputs are finite real numbers. Your intended contract accepts at least two nonnegative power readings and a positive time spacing.


a) State the numerical contract, including the formula, output units, and duration represented by the supplied trace.


The input list contains equally spaced power readings in watts, and the spacing is in seconds. For nn readings, the required calculation is


E=Δt1000[P02+∑i=1n−2Pi+Pn−12].E=\frac{\Delta t}{1000}\left[\frac{P_0}{2}+\sum_{i=1}^{n-2}P_i+\frac{P_{n-1}}{2}\right].


Power multiplied by time gives joules; dividing by 10001000 gives kilojoules. The contract requires at least two readings, nonnegative power, and positive spacing. Inputs outside those conditions should be rejected.


Three readings contain ________ intervals, so the supplied trace spans


T=(n−1)Δt=(3−1)(5 s)=10 s.T=(n-1)\Delta t=(3-1)(5\ \text{s})=10\ \text{s}.


The returned quantity is the trapezoidal energy estimate over that 10 s10\ \text{s} record.


b) Calculate an independent expected result. Compare it with the generated function's result and report the absolute relative error in percent.


Use the contract directly rather than copying the generated expression. The endpoint readings receive ________ weight:


Eref=51000(4002+600+8002)=51000(200+600+400)=6.00 kJ.\begin{aligned} E_{\text{ref}}&=\frac{5}{1000}\left(\frac{400}{2}+600+\frac{800}{2}\right)\\ &=\frac{5}{1000}(200+600+400)\\ &=6.00\ \text{kJ}. \end{aligned}


The generated function gives full weight to every reading:


EAI=51000(400+600+800)=9.00 kJ.\begin{aligned} E_{\text{AI}}&=\frac{5}{1000}(400+600+800)\\ &=9.00\ \text{kJ}. \end{aligned}


The discrepancy is 9.00−6.00=3.00 kJ9.00-6.00=3.00\ \text{kJ}. Relative to the independent reference,


absolute relative error=1003.006.00=50.0%.\text{absolute relative error}=100\frac{3.00}{6.00}=50.0\%.


The generated function overestimates this reference by 50.0%50.0\%. Its correct output units do not compensate for incorrect endpoint weighting.


c) Revise the function and check it using a separate constant-power trace: four readings of 300 W300\ \text{W} spaced 2 s2\ \text{s} apart. State what the checks establish.


Implement the specified weights and reject inputs outside the stated contract. The finite-real-number assumption remains in force.


Python
def energy_kj(power_w, dt_s):
    if len(power_w) < 2:
        raise ValueError("At least two readings are required")
    if dt_s <= 0:
        raise ValueError("Time spacing must be positive")
    if any(p < 0 for p in power_w):
        raise ValueError("Power readings must be nonnegative")

    weighted_sum = (
        0.5 * power_w[0]
        + sum(power_w[1:-1])
        + 0.5 * power_w[-1]
    )
    return weighted_sum * dt_s / 1000.0


Independently calculate the duration of the new trace:


T=(4−1)(2 s)=6 s.T=(4-1)(2\ \text{s})=6\ \text{s}.


Constant power multiplied by duration gives


E=(300 W)(6 s)1000=1.80 kJ.E=\frac{(300\ \text{W})(6\ \text{s})}{1000}=1.80\ \text{kJ}.


The revised function also gives


E=21000(150+300+300+150)=1.80 kJ.E=\frac{2}{1000}(150+300+300+150)=1.80\ \text{kJ}.


It returns 6.00 kJ6.00\ \text{kJ} for the original nonconstant trace and 1.80 kJ1.80\ \text{kJ} for the separate constant trace. These comparisons support correct implementation on both cases; they do not prove correctness for all inputs or establish the physical accuracy of the sampling model.

An AI assistant generates a function that accepts the length and width of a rectangular metal plate in millimetres and returns its area in square metres. You independently calculate an acceptance test using a length of 200 mm200\ \text{mm} and a width of 30 mm30\ \text{mm}.


What output should the test expect?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

An engineering team is preparing a prompt for an AI assistant that will write a sensor-processing function. Match each kind of supporting information to its role in specifying the requested implementation.

Match each term
A.

Maximum permitted memory use or execution time

B.

Observable pass-or-fail condition for deciding whether a requirement is met

C.

Required function name, argument types, return structure and units

D.

Authoritative reference for the installed library version and supported calls

Resource constraint

Interface contract

Dependency documentation

Acceptance criterion

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

The required spring model is F=kxF=kx, where stiffness kk is in N/m\text{N/m}, extension xx is in metres, and force FF is in newtons. An AI assistant supplies this Python function and test:


Python
def spring_force(k, x):
    return k * x * x

assert spring_force(1200, 0.10) == 12.0


The test passes. Which response correctly evaluates the evidence and identifies the needed revision?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice