help me to do nonlinear fitting use lsqcurvefit
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 %sn’, output_file_name);
% Save the Qc_fitted_envelope result to the text file
fid = fopen(results_file, ‘a’); % Open in append mode
fprintf(fid, ‘%st%.4fn’, file_name, Qc_fitted_envelope);
fclose(fid);
% Display results in the command window
fprintf(‘File: %sn’, file_name);
fprintf(‘ Envelope: Fitted Qc = %.4f, RMS Error = %.4fnn’, Qc_fitted_envelope, rms_error_envelope);
endplease 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 %sn’, output_file_name);
% Save the Qc_fitted_envelope result to the text file
fid = fopen(results_file, ‘a’); % Open in append mode
fprintf(fid, ‘%st%.4fn’, file_name, Qc_fitted_envelope);
fclose(fid);
% Display results in the command window
fprintf(‘File: %sn’, file_name);
fprintf(‘ Envelope: Fitted Qc = %.4f, RMS Error = %.4fnn’, Qc_fitted_envelope, rms_error_envelope);
end 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 %sn’, output_file_name);
% Save the Qc_fitted_envelope result to the text file
fid = fopen(results_file, ‘a’); % Open in append mode
fprintf(fid, ‘%st%.4fn’, file_name, Qc_fitted_envelope);
fclose(fid);
% Display results in the command window
fprintf(‘File: %sn’, file_name);
fprintf(‘ Envelope: Fitted Qc = %.4f, RMS Error = %.4fnn’, Qc_fitted_envelope, rms_error_envelope);
end nonlinear fitting, lsqcurvefit, envelope MATLAB Answers — New Questions