help me to do nonlinear fitting use lsqcurvefit
Show older comments
This question was flagged by Mathieu NOE
please help me, I am begginer in matlab and thsi is my first time doing nonlinear fitting, I have problem to fit data with the model non linear fitting (here I use lsqcurvefit), the flow of my code is something like this
- get autocorrelation function(ACF), isolate half of it
- get envelop of ACF,
- fit envelope of ACF with equation E=1/t^2 exp (-2*pi*f*Qc) to get optimum Qc (with f is my freq=3, t is time lapse)
here the ilustration from source that i use

Here is my code, but i don't really trust my self since the Qc value is not match with range (is uppose to get Wc between 100-400) but using my code i get thousand. Please is anyone can help me to clarify my code
clear; clc; %close all;
% Define the folder containing ACF files
folder_path = pwd; % Adjust the path to your ACF files
file_pattern = 'cBPCen3_s6h_ACF_HGSD_HHZ_2023-12_2023-12-01T00_00.txt'; % Match ACF filenames
files = dir(fullfile(folder_path, file_pattern));
% Define the model function for nonlinear fitting
f=3;
m=2;
model = @(Qc, t) (1 ./ t.^m).*exp(-2 * pi * f * t ./ Qc);
% Initial guess for Qc
Qc_initial = 110;
options = optimset('Display', 'off'); % Suppress output
% File to store Qc_fitted_envelope results
results_file = fullfile(folder_path, 'Qc_fitted_envelope_results.txt');
if exist(results_file, 'file')
delete(results_file); % Clear old results if file exists
end
% Loop over each ACF file
for i = 1:length(files)
% Load ACF file
file_name = files(i).name;
file_path = fullfile(folder_path, file_name);
acf = load(file_path);
% Extract the ACF data
y_obs = acf((length(acf)/2):(end))'; % Adjust this as needed
% Compute the squared envelope of the ACF
envel = envelope(y_obs,5,'peak')';
envel = envel(1:round(0.5 * length(envel)));
tenvel = (1:length(envel))';
% Perform nonlinear optimization for envelope
Qc_fitted_envelope = lsqcurvefit(@(Qc, tenvel) model(Qc, tenvel), Qc_initial, tenvel, envel, [], [], options);
y_fitted_envelope = model(Qc_fitted_envelope, tenvel);
rms_error_envelope = sqrt(mean((envel - y_fitted_envelope).^2));
% Plot results for each file - DEACTIVATE FOR BIG LOOPING
figure;
plot(tenvel/100,envel, 'k-', 'DisplayName', 'Envelope (Observed)');
hold on;
plot(tenvel/100, y_fitted_envelope, 'r-', 'LineWidth', 1.5, 'DisplayName', sprintf('Envelope (Fitted, RMS=%.4f)', rms_error_envelope));
xlabel('Time (s)');
ylabel('Amplitude');
title(sprintf('Envelope Fit - Qc = %.4f, RMS Error = %.4f', Qc_fitted_envelope, rms_error_envelope));
legend('Location', 'northeast');
%set(gca, 'YScale', 'log')
grid on;
% Save the envelope to a text file - DO YOU REALLY NEED SAVE THIS??
% output_file_name = ['envel_' file_name];
% output_file_path = fullfile(folder_path, output_file_name);
% save(output_file_path, 'envelope', '-ascii');
% fprintf('Envelope saved to %s\n', output_file_name);
% Save the Qc_fitted_envelope result to the text file
fid = fopen(results_file, 'a'); % Open in append mode
fprintf(fid, '%s\t%.4f\n', file_name, Qc_fitted_envelope);
fclose(fid);
% Display results in the command window
fprintf('File: %s\n', file_name);
fprintf(' Envelope: Fitted Qc = %.4f, RMS Error = %.4f\n\n', Qc_fitted_envelope, rms_error_envelope);
end
6 Comments
Mathieu NOE
on 19 Dec 2024
duplicate post
why not simply continue the first one ?
and what about the suggestions we already made ?
nirwana
on 19 Dec 2024
Mathieu NOE
on 2 Jan 2025
hello again and happy new year !
there are still some points that are unclear to me
- acf time vector : your code assume a 1 second time interval (and also t start at t= 1 , why ?) :
tenvel = (1:length(envel))';
- considering the senond half of data (after the central peak) you still have 2500 samples - so according to the look of your data once plotted , and compared to the posted image (time psan is 0 to 30 seconds) , I doubt that the time interval could be 1 second.
Mathieu NOE
on 2 Jan 2025
a second point is why the first plot (of A) shows a peak amplitude of autocorrelation which is not one ?
equation E=1/t^2 exp (-2*pi*f*Qc) assumes that ACF should be normalized I assume ?
Mathieu NOE
on 2 Jan 2025
maybe you should share the publication so we could see the larger context
nirwana
on 4 Jan 2025
Accepted Answer
More Answers (0)
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






