Email: helpdesk@telkomuniversity.ac.id

This Portal for internal use only!

  • My Download
  • Checkout
Application Package Repository Telkom University
All Categories

All Categories

  • IBM
  • Visual Paradigm
  • Adobe
  • Google
  • Matlab
  • Microsoft
    • Microsoft Apps
    • Analytics
    • AI + Machine Learning
    • Compute
    • Database
    • Developer Tools
    • Internet Of Things
    • Learning Services
    • Middleware System
    • Networking
    • Operating System
    • Productivity Tools
    • Security
    • VLS
      • Office
      • Windows
  • Opensource
  • Wordpress
    • Plugin WP
    • Themes WP
  • Others

Search

0 Wishlist

Cart

Categories
  • Microsoft
    • Microsoft Apps
    • Office
    • Operating System
    • VLS
    • Developer Tools
    • Productivity Tools
    • Database
    • AI + Machine Learning
    • Middleware System
    • Learning Services
    • Analytics
    • Networking
    • Compute
    • Security
    • Internet Of Things
  • Adobe
  • Matlab
  • Google
  • Visual Paradigm
  • WordPress
    • Plugin WP
    • Themes WP
  • Opensource
  • Others
More Categories Less Categories
  • Get Pack
    • Product Category
    • Simple Product
    • Grouped Product
    • Variable Product
    • External Product
  • My Account
    • Download
    • Cart
    • Checkout
    • Login
  • About Us
    • Contact
    • Forum
    • Frequently Questions
    • Privacy Policy
  • Forum
    • News
      • Category
      • News Tag

iconTicket Service Desk

  • My Download
  • Checkout
Application Package Repository Telkom University
All Categories

All Categories

  • IBM
  • Visual Paradigm
  • Adobe
  • Google
  • Matlab
  • Microsoft
    • Microsoft Apps
    • Analytics
    • AI + Machine Learning
    • Compute
    • Database
    • Developer Tools
    • Internet Of Things
    • Learning Services
    • Middleware System
    • Networking
    • Operating System
    • Productivity Tools
    • Security
    • VLS
      • Office
      • Windows
  • Opensource
  • Wordpress
    • Plugin WP
    • Themes WP
  • Others

Search

0 Wishlist

Cart

Menu
  • Home
    • Download Application Package Repository Telkom University
    • Application Package Repository Telkom University
    • Download Official License Telkom University
    • Download Installer Application Pack
    • Product Category
    • Simple Product
    • Grouped Product
    • Variable Product
    • External Product
  • All Pack
    • Microsoft
      • Operating System
      • Productivity Tools
      • Developer Tools
      • Database
      • AI + Machine Learning
      • Middleware System
      • Networking
      • Compute
      • Security
      • Analytics
      • Internet Of Things
      • Learning Services
    • Microsoft Apps
      • VLS
    • Adobe
    • Matlab
    • WordPress
      • Themes WP
      • Plugin WP
    • Google
    • Opensource
    • Others
  • My account
    • Download
    • Get Pack
    • Cart
    • Checkout
  • News
    • Category
    • News Tag
  • Forum
  • About Us
    • Privacy Policy
    • Frequently Questions
    • Contact
Home/Matlab/help me to do nonlinear fitting use lsqcurvefit

help me to do nonlinear fitting use lsqcurvefit

/ 2025-01-07
help me to do nonlinear fitting use lsqcurvefit
Matlab

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

​

Tags: matlab

Share this!

Related posts

test one two three four
2025-05-20

test one two three four

Is it possible to make this tiny loop faster?
2025-05-19

Is it possible to make this tiny loop faster?

Solar Wind Battery Hybrid Integration
2025-05-19

Solar Wind Battery Hybrid Integration

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search

Categories

  • Matlab
  • Microsoft
  • News
  • Other
Application Package Repository Telkom University

Tags

matlab microsoft opensources
Application Package Download License

Application Package Download License

Adobe
Google for Education
IBM
Matlab
Microsoft
Wordpress
Visual Paradigm
Opensource

Sign Up For Newsletters

Be the First to Know. Sign up for newsletter today

Application Package Repository Telkom University

Portal Application Package Repository Telkom University, for internal use only, empower civitas academica in study and research.

Information

  • Telkom University
  • About Us
  • Contact
  • Forum Discussion
  • FAQ
  • Helpdesk Ticket

Contact Us

  • Ask: Any question please read FAQ
  • Mail: helpdesk@telkomuniversity.ac.id
  • Call: +62 823-1994-9941
  • WA: +62 823-1994-9943
  • Site: Gedung Panambulai. Jl. Telekomunikasi

Copyright © Telkom University. All Rights Reserved. ch

  • FAQ
  • Privacy Policy
  • Term

This Application Package for internal Telkom University only (students and employee). Chiers... Dismiss