Global Fit larger dataset to nonlinear equation with one global variable , and N number of variables, better objective function needed

9 次查看(过去 30 天)
I have a very larger data set (m x N) decays
i need to fit the larger data set to a nonlinear equation y = b_N + e(-i*c*d_N) where c is a global parameter and B_N and d_N are unique to each line in the matrix.
I am looking for a good example to follow or format. The exists examples are for one or two data set but I have over 20 decays.
I have the following code but the fit is very bad. I think the objective function need to change but I am not sure how to improve it.
% Define the number of datasets
N = length(Y_N)
tau = tau_N; % Cell array to store tau_N values for each dataset
Y = Y_N; % Cell array to store Y_N values for each dataset
% function is
% exp((-8*(G^2)*(gamma^2)*tau*(D_zz(omega)*tau)/T_2*(pi^2)*(omega^2))
%exp( (-8*(G^2)*(gamma^2)/(pi^2))*(tau^2*D__zz(i))/(T_2*(omega^2))
%42.577e9 -> (gamma/2pi)
%G is 35 T/M for the PM2
factor_PM2 = 2*(35^2)*((42.577e9)^2); % is equal to values for 8*(G^2)*(gamma^2)/(pi^2))
% where D_zz is param(N+i), param(i) is an Amplitude, and param(end) is the Global T2
%where omega is (1/tau_N_1) which is the first x values of each data set
model = @(params, tau_N, tau_N_1, i) ...
params(i) * exp(-factor_PM2 * params(N+i) * tau_N.^2 / (params(end) * ((1 / tau_N_1)^2)));
% Initial guesses for A_N, B_N, C_N for each dataset
A_guess = 5 * ones(N, 1); % Initial guess for A_N
B_guess = .00000001* ones(N, 1); % Initial guess for B_N
T2_guess = 10; % Initial guess for the global parameter T2
% Combine initial guesses for all datasets into a single parameter vector
initial_params = [A_guess; B_guess; T2_guess];
% Initial guesses for A_N, B_N, C_N for each dataset
A_guess = .1 * ones(N, 1); % Initial guess for A_N
B_guess = 5 * ones(N, 1); % Initial guess for B_N
C_guess = 10e-9 * ones(N, 1); % Initial guess for C_N
T2_guess = 1; % Initial guess for the global parameter D
% Combine initial guesses for all datasets into a single parameter vector
initial_params = [A_guess; B_guess; T2_guess];
% Define the objective function for fitting all datasets
objective_function = @(params) ...
sum(arrayfun(@(i) sum((Y{i} - model(params, tau{i}, tau{i}(1), i)).^2), 1:N));
% Perform the fitting using lsqnonlin (requires Optimization Toolbox)
options = optimoptions('lsqnonlin', 'Display', 'iter'); % Option to display the fitting progress
fitted_params = lsqnonlin(objective_function, initial_params, [], [], options);
% Extract fitted parameters
A_fit = fitted_params(1:N);
B_fit = fitted_params(N+1:2*N);
T2_fit = fitted_params(end);
% Display results
fprintf('Fitted parameters:\n');
for i = 1:N
fprintf('Dataset %d: A = %.4f, B = %.4f\n', i, A_fit(i), B_fit(i));
end
fprintf('Global parameter T2 = %.4f\n', T2_fit);
% Plotting the data and fits
figure;
hold on;
for i = 1:N
% Plot the original data points
plot(tau{i}, Y{i}, 'o', 'DisplayName', sprintf('Data %d', i));
% Calculate the fitted curve using the fitted parameters
fitted_curve = A_fit(i) * exp(-factor_PM2 * B_fit(i) * tau{i}.^2 / (T2_fit * (1 / tau{i}(1)^2)));
% Plot the fitted curve
plot(tau{i}, fitted_curve, '-', 'DisplayName', sprintf('Fit %d', i));
end
xlabel('\tau');
ylabel('Y');
legend show;
PLease advise.
  3 个评论
Velencia
Velencia 2024-9-10
编辑:Velencia 2024-9-15
no the data set is too large to provide and is data athat i cannot share. I can provide assimulated data set would that be useful @Aastha

请先登录,再进行评论。

回答(1 个)

Subhajyoti
Subhajyoti 2024-9-9
To fit a large dataset to a non-linear equation, you can use the lsqcurvefit’ function. It solves nonlinear curve-fitting (data-fitting) problems in least-squares sense.
You can refer to the following link for more information on ‘lsqcurvefit’ function:
You can follow these steps for the given curve-fitting problem:
  1. Prepare the Data: Ensure your data matrix is organized such that each row corresponds to a different decay, and your independent variable (e.g., time) is appropriately defined.
  2. Initial Guess: Provide an initial guess for the parameters. This can significantly affect the convergence of the optimization.
  3. Optimization: Use the optimization function - ‘lsqcurvefit’ to fit the model to your data.
Additionally, you can refer to these resources to learn more about curve-fitting in MATLAB:

类别

Help CenterFile Exchange 中查找有关 Nonlinear Least Squares (Curve Fitting) 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by