Use of AI in Programming
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 n readings, the contract can specify
Here, each Pi is in watts, the spacing Δt is in seconds, and the returned energy is in kilojoules. The factor 1000 converts joules to kilojoules because 1 W=1 J/s. For two readings, the interior sum is empty. There are n−1 time intervals, so the record spans (n−1)Δ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.
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,
Multiply by 100 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 at times 0, 5, and 10 s. You request an AI-assisted engineering programming solution that estimates energy using trapezoidal integration and returns kilojoules.
The AI assistant provides:
def energy_kj(power_w, dt_s):
return sum(power_w) * dt_s / 1000.0Assume 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 n readings, the required calculation is
Power multiplied by time gives joules; dividing by 1000 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
The returned quantity is the trapezoidal energy estimate over that 10 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:
The generated function gives full weight to every reading:
The discrepancy is 9.00−6.00=3.00 kJ. Relative to the independent reference,
The generated function overestimates this reference by 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 W spaced 2 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.
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.0Independently calculate the duration of the new trace:
Constant power multiplied by duration gives
The revised function also gives
It returns 6.00 kJ for the original nonconstant trace and 1.80 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 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.
Maximum permitted memory use or execution time
Observable pass-or-fail condition for deciding whether a requirement is met
Required function name, argument types, return structure and units
Authoritative reference for the installed library version and supported calls
Resource constraint
Interface contract
Dependency documentation
Acceptance criterion
The required spring model is F=kx, where stiffness k is in N/m, extension x is in metres, and force F is in newtons. An AI assistant supplies this Python function and test:
def spring_force(k, x):
return k * x * x
assert spring_force(1200, 0.10) == 12.0The test passes. Which response correctly evaluates the evidence and identifies the needed revision?