# Issue in curve fitting

12 ビュー (過去 30 日間)
M. Sarim Ameed Khan 2021 年 10 月 5 日
I am trying hard to fit my data but i am unable. I am using curve fitting app to do so and also change starting points for constant several times.
I am attaching my data. It is giving straight line and I need lorentzian fit. Thanks alot for help in advance and sorry for the inconvenience caused.
My equation : ((I0*G))/((4*pi*pi*((x-f0)^2)+(G*G/4)))

サインインしてコメントする。

### 採用された回答

Image Analyst 2021 年 10 月 5 日
Try this:
% Uses fitnlm() to fit a non-linear model (a Lorentzian curve) through noisy data.
% Requires the Statistics and Machine Learning Toolbox, which is where fitnlm() is contained.
% Initialization steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Create the X coordinates from 0 to 20 every 0.5 units.
X = data(:, 1);
Y = data(:, 2);
% Now we have noisy training data that we can send to fitnlm().
% Plot the noisy initial data.
plot(X, Y, 'b*', 'LineWidth', 2, 'MarkerSize', 15);
grid on;
% Convert X and Y into a table, which is the form fitnlm() likes the input data to be in.
tbl = table(X(:), Y(:));
% Define the model as Y = ((I0*G))/((4*pi*pi*((x-f0)^2)+(G*G/4)))
% Note how this "x" of modelfun is related to big X and big Y.
% x((:, 1) is actually X and x(:, 2) is actually Y - the first and second columns of the table.
modelfun = @(b,x) b(1) * b(2) ./ ((4 * pi^2 * (x(:, 1) - b(3)).^2) + b(2)^2/4);
% Now the next line is where the actual model computation is done.
mdl = fitnlm(tbl, modelfun, beta0);
% Now the model creation is done and the coefficients have been determined.
% YAY!!!!
% Extract the coefficient values from the the model object.
% The actual coefficients are in the "Estimate" column of the "Coefficients" table that's part of the mode.
coefficients = mdl.Coefficients{:, 'Estimate'};
% Get them by the name the poster uses.
I0 = coefficients(1)
G = coefficients(2)
f0 = coefficients(3)
% Let's do a fit, but let's get more points on the fit, beyond just the widely spaced training points,
% so that we'll get a much smoother curve.
X = linspace(4.68, 4.75, 1920); % Let's use 1920 points, which will fit across an HDTV screen about one sample per pixel.
% Create smoothed/regressed data using the model:
b = coefficients;
yFitted = b(1) * b(2) ./ ((4 * pi^2 * (X - b(3)).^2) + b(2)^2/4);
% Now we're done and we can plot the smooth model as a red line going through the noisy blue markers.
hold on;
plot(X, yFitted, 'r-', 'LineWidth', 2);
grid on;
title('Exponential Regression with fitnlm()', 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
legendHandle = legend('Noisy Y', 'Fitted Y', 'Location', 'northeast');
legendHandle.FontSize = 25;
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
I0 =
2.7329242104686
G =
0.04271737176362
f0 =
4.71384625941071
##### 1 件のコメント表示非表示 なし
M. Sarim Ameed Khan 2021 年 10 月 6 日
Thank you very mutch.

サインインしてコメントする。

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by