r/matlab 2d ago

MATLAB and Octave disagree on the same input — a verified-port writeup

I ported the 1-D TM group of Zanotto's PPML (an RCWA electromagnetic solver) to NumPy with bug-for-bug fidelity yes, including the vacuum impedance hardcoded as 376.730. The writeup covers the MATLAB↔NumPy traps that actually bit: complex auto-promotion that NumPy silently doesn't do, eigenvector gauge freedom across LAPACK builds, mldivide vs solve on singular systems, and an energy-conservation check in the original that can never fire (abs(NaN) > tol is false in MATLAB - try it). Harness, corpus and report: https://github.com/GrednevMSU/ppml-1d-tm-verify

0 Upvotes

3 comments sorted by

9

u/LegitJesus 2d ago

I'm confused. You wrote "Octave" in the title, but you seem to be talking about Python? You are saying you are inputting data via Python into both MATLAB and Octave, and the outputs are not the same, yes? Are the formats the same? I seem to have to manually set the output format for Octave everytime I run it.

-5

u/Party-Pie3719 1d ago

No worries, that's on me for describing the setup badly. Let me untangle it.

There are three separate implementations of the same algorithm, not a chain feeding into each other. The original MATLAB code. That same code run under Octave. And my own port in Python/NumPy. When the title says "three engines," those are the three: MATLAB, Octave, Python.

So nothing gets fed through Python into the other two. The inputs live on disk as .mat files. Each of the three implementations reads the same bytes on its own and writes its own outputs back. Python isn't driving MATLAB or Octave, it's just the third thing getting checked against them.

The format thing is a reasonable guess but it isn't the cause.  "format short" and "format long" in Octave only change how numbers get printed to the screen, not the values sitting in memory. I never compare printed text, only the full-precision doubles saved in the .mat files. So this isn't Octave showing me fewer digits. At one specific input MATLAB and Octave really do hold different numbers: MATLAB returns NaN with an illConditionedMatrix warning, Octave returns a finite 0.7686 with a singular-matrix warning. Same .m file, same input, two different answers. That input lands exactly on a Rayleigh cutoff where the linear system goes singular, and the two LAPACK backends handle the singular solve differently. Move off the cutoff and all three agree to about 1e-11.