Dear colleagues,
for years, I have successfully used “leastsq” and, suddenly, it gives wrong results. What had happened?
Heinz
// ------------------------------------------------------------------
// EMG fit (p = [mu, 1/lambda, sigma])
// • Model fitted as Frequency = f(Height)
// ------------------------------------------------------------------
clear; clc; close;
// 1. Load data ----------------------------------------------------
data = csvRead("histogramm_height_RK.csv", ";");
h = data(2:$, 1); // Height (m)
f = data(2:$, 2); // Frequency (counts)
// 2. Normalisation --------------------------------------------------
yy=f/sum(f)/2;
// 3. EMG function --------------------------------------------------
function yhat=pred(p, x)
a = 1 / p(2); // lambda
s = p(3); // sigma
xx = x - p(1); // x - mu
yhat = (a/2) .* exp((a/2) .* ((a*s^2) - 2*xx)) ...
.* erfc((a*s^2 - xx) ./ (s*sqrt(2)));
endfunction
// 4. Initial guess μ, τ=1/λ, σ and nonlinear least-squares fitting
p0=[5.73; 10.76; 75.1]
[fopt,p,gopt] = leastsq( #(p)->(pred(p,h)-yy) , p0);
// 5. Final result --- IS WRONG !
disp(p)
histogramm_height_RK.csv (1.6 KB)