Hi @Yogesh,
First download this function from link provided below.
Once, you have downloaded the function, you can name the script as “ test function.m”. To fit a Lorentzian model to the data generated from your simulation, I will provide an updated code snippet that incorporates the necessary modifications. This will ensure you can effectively fit your data and visualize the results. The provided MATLAB code by you simulates optical fiber dynamics, including parameters such as fiber length, pump wavelength, and noise modulation. The end goal is to fit a Lorentzian function to the power spectrum obtained from your signal analysis. The lorentzfit function you included is designed for this purpose but requires proper integration into your simulation workflow. Here's how you can integrate the lorentzfit function into your existing code:
% Add this after calculating Power_FFt figure; plot(nu, 2*n*c*eps0*A*(FFt_EL0t/Nt).^2, 'b-', 'LineWidth', 2); title('Power Spectrum'); xlabel('Frequency (Hz)'); ylabel('Power Spectrum');
% Fit the Lorentzian function [xData, yData] = deal(nu, 2*n*c*eps0*A*(FFt_EL0t/Nt).^2); % x and y data for fitting
% Initial parameter guess: [P1, P2 (center frequency), P3 (width), C (constant)] initialParams = [max(yData), mean(xData), 1e-6, min(yData)];
% Perform the fitting [yFit, params, resnorm, residual] = lorentzfit(xData, yData, initialParams);
% Plotting the fit hold on; plot(xData, yFit, 'r-', 'LineWidth', 2); legend('Original Data', 'Lorentzian Fit'); hold off;
% Displaying parameters fprintf('Fitted Parameters:\n'); fprintf('P1: %.4f\n', params(1)); fprintf('P2: %.4f\n', params(2)); fprintf('P3: %.4f\n', params(3)); fprintf('C: %.4f\n', params(4));
Explanation of Changes
Data Preparation: The xData and yData variables are created to store the frequency and corresponding power spectrum data.
Initial Parameter Guess: An initial guess for the parameters[P1, P2 (center frequency), P3 (width), C (constant)] is provided. These are critical for ensuring that the optimization converges correctly.
Lorentzian Fitting: The lorentzfit function is called with the prepared data and initial parameters. It returns fitted values (yFit) optimized parameters (params) residual norm (resnorm), and residuals.
Visualization: A plot is generated to compare the original power spectrum with the fitted Lorentzian curve. This provides a visual representation of how well the model fits the data.
Parameter Output: The fitted parameters are printed to the console for review.
Please see attached.
Here are some additional insights that I would like to share with you.
Choosing Initial Parameters: Properly choosing initial guesses can significantly affect convergence in non-linear fitting problems. Adjust these values based on your specific dataset characteristics.
Error Handling: Consider implementing error handling around your fitting process to manage cases where fitting might fail or return non-physical parameters.
Validation of Fit: After fitting, evaluate how well your model describes the data using statistical measures like R² or visual inspection of residuals.
By integrating these changes into your MATLAB script, you should be able to fit a Lorentzian function to your simulated power spectrum effectively.
If you encounter issues during fitting or need further assistance with specific aspects of your simulation or analysis, feel free to ask!