Gains:
- Ability to rely on deterministic output, not verbal guesswork, by having the AI translate a physical calculation into NumPy/SciPy code and run the code itself
- Ability to verify the reliability of a numerical code by testing it with a simple situation whose answer is known and checking its convergence.
- Ability to physically provide the unit and level of the result by reflecting variable units into the code
Many problems in physics cannot be solved by hand: an integral has no analytical solution, the root of a system of equations cannot be found in closed form, or a calculation must be repeated thousands of times. This is where numerical methods come into play: instead of an exact formula, the computer calculates step by step with approximate but controlled accuracy. In this unit, you will learn how to use artificial intelligence (AI) as a code partner for computational physics calculations, how to verify the code it produces, and what traps you should avoid falling into. The physicist's standard tools here will be the Python language and NumPy (library for numerical arrays and fast mathematics) as well as SciPy (library for scientific computing; includes integration, radical finding, optimization).
Why AI + Python, why not by hand?
Performing numerical calculations by hand is both slow and error-prone. AI can translate your physical construct into a running Python code in a few seconds. But the golden rule here is: AI writes the code, you run the code and verify the result with physics. The AI saying "this code gives that" is a prediction; Actually running the code and seeing the output is a deterministic fact. An LLM might "calculate" the output of the code he wrote in his head and say a wrong number; whereas when the same code is run it gives the correct result. So never trust the numerical result the AI verbally gives — run the code.
Basic computational tasks and the role of AI
Quest
Method/tool
physical verification
definite integral
scipy.integrate.quad
Limit states, dimensional analysis
root of equation
scipy.optimize.brentq
Put the root in place and check if it's zero.
System of linear equations
numpy.linalg.solve
Putting the solution back into the system
Derivative (numeric)
Central difference or numpy.gradient
Comparison with analytical derivation
Bulk vector calculation
NumPy arrays
Unit and rank control
AI is very good at suggesting which of these tools is appropriate and getting the syntax (code writing rules) correct. But you decide which method makes physical sense and the accuracy of the result.
Step by step: a verifiable numerical calculation
1. Clarify the problem and units. For example: "Calculate the integral e^(−x²) from 0 to infinity; the result should be √π/2 ≈ 0.8862." Rewriting the expected result (if any) makes verification easier.
2. Ask the AI for the code, but you run the output. Copy the code block and run it in your own environment (Jupyter, Colab, native Python).
3. Test with a known situation. Try the code with a simple situation to which you already know the answer. For example, test an integral code with an example you know by hand, such as ∫₀¹ x dx = 0.5. If the code gives this correctly, your confidence increases.
4. Check for convergence. In numerical methods, when you increase the number of steps, the result should approach a constant value. If it does not approach, the method is not suitable.
5. Check unit and rank. Always provide the numerical result with the physical unit and order.
Tip: When requesting code from the AI, say “also include a test line that compares the result to a known analytical value.” Thus, the code both performs the calculation and tests itself in a known situation. This makes errors immediately visible.
three mini cases
Case 1 — Incorrect oral conclusion. A student had AI write an integral code and AI said "this code gives about 1.77." The student ran the code: the actual output was 0.886. The AI had made a mistake in predicting the output of the code; The code was correct. Lesson: trust the code, don't trust the verbal guess.
Case 2 — Numerical instability. A researcher looked for the root of an equation with a root finding code that AI wrote. The code returned a root, but when the researcher put the root back into the equation, the result was not zero but a very large number. The problem was that the starting range did not contain the root; The AI assumed the appropriate range. The researcher corrected the interval with physical information and found the correct root.
Case 3 — Confirmation gained. An engineer wanted a code that numerically calculated the deflection of a beam under load. The code worked, but the result showed meters of deflection—impossible for a steel beam. The engineer examined the units: the AI had pretended to have the elastic modulus in Pa instead of GPa, missing a factor of 10⁹. With the unit correction, the result decreased to millimeters and was reasonable.
Four copyable templates
1) Numeric calculation with validation line:
Write Python (NumPy/SciPy) code that performs the following physical calculation: [problem]. Declare all variables in SI units and specifying the unit with a comment line. Add a check line (assert or print) at the end of the code that compares the result to a known analytical/simple value. I'll run the code; you don't guess the output, just write the code and the expected rank.
2) Method selection consultation:
I want to solve the following problem numerically: [problem].Which SciPy/NumPy function is appropriate and WHY? Suggest 2 alternative methods, write down the pros/cons of each in terms of accuracy and stability. State which one should be preferred in which physical situation.
3) Convergence check:
Write Python code that shows how the result converges by increasing the number of steps of the following numerical calculation (e.g. N=10, 100, 1000). Print the results in a table. If there is no convergence, explain the physical/numerical reason for this in one sentence. Code: [here]
4) Unit control:
In the code below, comment out the SI unit of each variable and check if the unit of the final result is consistent with the expected unit (e.g. meter, joule). If there is an inconsistency, show which line it is in. Code: [here]
Weak prompt / Strong prompt
Weak: "Say the result of this integral: ∫₀^∞ x²·e^(−x) dx"
Result: The AI spits out a number in its head (maybe right, maybe wrong); nothing executable, nothing verifiable.
Strong: "Write Python code that calculates the integral ∫₀^∞ x²·e^(−x) dx with scipy.integrate.quad. Add an assert line that compares the result to the analytic value of this integral, 2! = 2. I will run the code."
Result: Executable code, built-in validation (comparison with analytical value) and reliable result.
Common mistakes
- Relying on the verbal numerical result of the AI. LLM may incorrectly predict the output of the code in the head. Always run the code.
- Not testing with known situation. Code that does not test with a simple example to which you know the answer may contain hidden errors.
- Not checking for convergence. Sticking with a single step count obscures whether the result is numerically reliable.
- Not reflecting the units in the code. Not commenting the unit of variables invites silent 10ⁿ errors.
- Ignoring library version/assumptions. Sometimes AI may produce a non-existent function name or obsolete syntax; It is noticeable when you run it.
Caution: Before putting a numerical result into a publication, assignment, or design, test the code with an independent known case and provide the result in units/orders. Even working code may be calculating the wrong physics; Just because the code works doesn't mean the physics are correct.
In summary
AI is a powerful partner in translating physical calculations into Python code; But the numbers he gives verbally are estimates, not facts. The safe workflow is clear: define the problem with its units, have the AI write the code but you run the output, test the code with a known situation, check convergence and unit. What is deterministic is the code that is executed; It is not an LLM. In the next unit we will move this numerical foundation to the simulation of time-varying physical systems.
Application task
Choose a physics calculus for which you know the analytical solution (for example, the speed of a free-falling object over a period of time or a simple definite integral). Have the AI write a Python code with template 1; run the code yourself. Compare the result with the analytical value you know. Then observe how the result behaves by changing the number of steps or a parameter. Note in 5-6 sentences: did the output of the code match the verbal prediction, was there a unit/order issue?
checklist
- [ ] I clarified the problem and the unit of all variables.
- [ ] I ran the code written by the AI myself, I did not trust the verbal result.
- [ ] I tested the code with a simple situation for which I knew the answer.
- [ ] I checked convergence or parameter sensitivity.
- [ ] I physically provided the unit and order of the result.
- [ ] I have verified that the library functions I am using are real and up to date.